Skip to content

Commit c65084c

Browse files
committed
added LinkedData info
1 parent 956745a commit c65084c

File tree

7 files changed

+98
-0
lines changed

7 files changed

+98
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ $info->providerIcon; //The icon choosen as main icon
7373

7474
$info->publishedDate; //The published date of the resource
7575
$info->license; //The license url of the resource
76+
$info->linkedData; //The linked-data info (http://json-ld.org/)
7677
```
7778

7879
## Customization

src/Adapters/Adapter.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,19 @@ public function getLicense()
475475
return Utils::getFirstValue(Utils::getData($this->providers, 'license', $this->request));
476476
}
477477

478+
/**
479+
* {@inheritdoc}
480+
*/
481+
public function getLinkedData()
482+
{
483+
$data = [];
484+
foreach ($this->providers as $provider) {
485+
$data = array_merge($data, $provider->getLinkedData());
486+
}
487+
488+
return $data;
489+
}
490+
478491
/**
479492
* Get images info.
480493
*

src/DataInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,11 @@ public function getPublishedTime();
129129
* @return string|null
130130
*/
131131
public function getLicense();
132+
133+
/**
134+
* Returns all linked data found.
135+
*
136+
* @return array
137+
*/
138+
public function getLinkedData();
132139
}

src/Providers/Html.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,36 @@ public function getLicense()
183183
return $this->bag->get('copyright');
184184
}
185185

186+
/**
187+
* {@inheritdoc}
188+
*/
189+
public function getLinkedData()
190+
{
191+
if (!($html = $this->request->getHtmlContent())) {
192+
return false;
193+
}
194+
195+
$data = [];
196+
197+
foreach ($html->getElementsByTagName('script') as $script) {
198+
if ($script->hasAttribute('type') && strtolower($script->getAttribute('type')) === 'application/ld+json') {
199+
$value = trim($script->nodeValue);
200+
201+
if (empty($value)) {
202+
continue;
203+
}
204+
205+
try {
206+
$data[] = json_decode($value);
207+
} catch (\Exception $exception) {
208+
continue;
209+
}
210+
}
211+
}
212+
213+
return $data;
214+
}
215+
186216
/**
187217
* Extract information from the <link> elements.
188218
*

src/Providers/Provider.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,12 @@ public function getPublishedTime()
148148
public function getLicense()
149149
{
150150
}
151+
152+
/**
153+
* {@inheritdoc}
154+
*/
155+
public function getLinkedData()
156+
{
157+
return [];
158+
}
151159
}

tests/AbancaTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
class AbancaTest extends TestCaseBase
4+
{
5+
public function testOne()
6+
{
7+
$this->assertEmbed(
8+
'https://www.abanca.com/gl',
9+
[
10+
'linkedData' => [
11+
(object)[
12+
'@context' => 'http://schema.org',
13+
'@type' => 'Organization',
14+
'name' => 'ABANCA',
15+
'url' => 'https://www.abanca.com',
16+
'logo' => 'https://www.abanca.com/img/logo-social.jpg',
17+
'sameAs' => [
18+
'https://www.facebook.com/SomosABANCA',
19+
'https://twitter.com/somosABANCA',
20+
'https://www.youtube.com/user/somosABANCAtv',
21+
'https://www.flickr.com/photos/125188945@N05/',
22+
],
23+
'contactPoint' => [
24+
(object)[
25+
'@type' => 'ContactPoint',
26+
'telephone' => '+34-981 910 522',
27+
'contactType' => 'customer service',
28+
]
29+
],
30+
]
31+
]
32+
]
33+
);
34+
}
35+
}

tests/TestCaseBase.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ protected function assertEmbed($url, array $info, array $config = array())
6161
$this->assertSame($value, $i->$name);
6262
break;
6363

64+
case 'linkedData':
65+
$this->assertEquals($value, $i->$name);
66+
break;
67+
6468
default:
6569
throw new InvalidArgumentException("No valid {$name} assertion");
6670
}

0 commit comments

Comments
 (0)