-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathJsonLdReader.php
More file actions
295 lines (244 loc) · 8.3 KB
/
JsonLdReader.php
File metadata and controls
295 lines (244 loc) · 8.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php
declare(strict_types=1);
namespace Brick\StructuredData\Reader;
use Brick\StructuredData\Item;
use Brick\StructuredData\Reader;
use stdClass;
use DOMDocument;
use DOMNode;
use DOMXPath;
use Sabre\Uri\InvalidUriException;
use function Sabre\Uri\build;
use function Sabre\Uri\parse;
use function Sabre\Uri\resolve;
/**
* Reads JSON-LD documents embedded into a HTML document.
*
* This first implementation is a rudimentary parser that only implements a subset of the JSON-LD spec, only allows a
* string in `@context`, and considers this string a vocabulary identifier and not an external context file.
*
* This may look like it's missing a lot (it is), like it will make mistakes (it will), but this should be enough to
* parse most of the web pages embedding schema.org data, as long as they follow the simple syntax used in the examples.
*
* https://json-ld.org/spec/latest/json-ld/
*/
class JsonLdReader implements Reader
{
/**
* @var string[]
*/
private $iriProperties;
/**
* JsonLdReader constructor.
*
* Because this naive implementation cannot parse contexts, it accepts a hardcoded list of properties whose values
* will be considered as IRIs and resolved relative to the current URL.
*
* Example: ['http://schema.org/image', 'http://schema.org/url']
*
* @param string[] $iriProperties
*/
public function __construct(array $iriProperties = [])
{
$this->iriProperties = $iriProperties;
}
/**
* @inheritDoc
*/
public function read(DOMDocument $document, string $url) : array
{
$xpath = new DOMXPath($document);
$nodes = $xpath->query('//script[@type="application/ld+json"]');
$nodes = iterator_to_array($nodes);
if (! $nodes) {
return [];
}
$items = array_map(function(DOMNode $node) use ($url) {
return $this->readJson($node->textContent, $url);
}, $nodes);
return array_merge(...$items);
}
/**
* Reads a list of items from a JSON-LD string.
*
* If the JSON is not valid, an empty array is returned.
*
* @param string $json The JSON string.
* @param string $url The URL the document was retrieved from, for relative URL resolution.
*
* @return Item[]
*/
private function readJson(string $json, string $url) : array
{
$data = json_decode($json);
if ($data === null) {
return [];
}
if (is_object($data)) {
if (isset($data->{'@graph'}) && is_array($data->{'@graph'})) {
$data = $data->{'@graph'};
} else {
$item = $this->readItem($data, $url, null);
return [$item];
}
}
if (is_array($data)) {
$items = array_map(function($item) use ($url) {
return is_object($item) ? $this->readItem($item, $url, null) : null;
}, $data);
$items = array_filter($items);
$items = array_values($items);
return $items;
}
return [];
}
/**
* Reads a single item.
*
* @param stdClass $item A decoded JSON object representing an item, or null if invalid.
* @param string $url The URL the document was retrieved from, for relative URL resolution.
* @param string|null $vocabulary The currently vocabulary URL, if any.
*
* @return Item
*/
private function readItem(stdClass $item, string $url, ?string $vocabulary) : Item
{
if (isset($item->{'@context'}) && is_string($item->{'@context'})) {
$vocabulary = $this->checkVocabularyUrl($item->{'@context'}); // ugh
}
$id = null;
if (isset($item->{'@id'}) && is_string($item->{'@id'})) {
try {
$id = resolve($url, $item->{'@id'}); // always relative to the document URL, no support for @base
} catch (InvalidUriException $e) {
// ignore
}
}
$types = [];
if (isset($item->{'@type'})) {
$type = $item->{'@type'};
if (is_string($type)) {
$type = $this->resolveTerm($type, $vocabulary);
$types = [$type];
} elseif (is_array($type)) {
$types = array_map(function($type) use ($vocabulary) {
return is_string($type) ? $this->resolveTerm($type, $vocabulary) : null;
}, $type);
$types = array_filter($types);
$types = array_values($types);
}
}
$result = new Item($id, ...$types);
foreach ($item as $name => $value) {
if ($name === '' || $name[0] === '@') {
continue;
}
$name = $this->resolveTerm($name, $vocabulary);
if (is_array($value)) {
// Flatten the array: not sure if this is required by the JSON-LD standard, but some websites output
// nested arrays such as "offer": [[ { ... } ]], and Google Structured Data Testing Tool does recognize
// this syntax, so we're doing the same here.
$value = $this->flattenArray($value);
foreach ($value as $theValue) {
$theValue = $this->getPropertyValue($name, $theValue, $url, $vocabulary);
if ($theValue !== null) {
$result->addProperty($name, $theValue);
}
}
} else {
$value = $this->getPropertyValue($name, $value, $url, $vocabulary);
if ($value !== null) {
$result->addProperty($name, $value);
}
}
}
return $result;
}
/**
* Flattens a potentially multidimensional array.
*
* The result array contains no nested arrays.
*
* @param array $array
*
* @return array
*/
private function flattenArray(array $array) : array
{
$result = [];
array_walk_recursive($array, function($a) use (& $result) {
$result[] = $a;
});
return $result;
}
/**
* @param string $term
* @param string|null $vocabulary
*
* @return string
*/
private function resolveTerm(string $term, ?string $vocabulary)
{
if ($vocabulary !== null) {
return $vocabulary . $term;
}
return $term;
}
/**
* @param string $name The property name.
* @param mixed $value The property value. Any JSON type.
* @param string $url The URL the document was retrieved from, for relative URL resolution.
* @param string|null $vocabulary The currently vocabulary URL, if any.
*
* @return Item|string|null The value, or NULL if the input value is NULL or an array.
*/
private function getPropertyValue(string $name, $value, string $url, ?string $vocabulary)
{
if (is_string($value)) {
if (in_array($name, $this->iriProperties, true)) {
try {
$value = resolve($url, $value);
} catch (InvalidUriException $e) {
// ignore
}
}
}
if (is_bool($value)) {
return $value ? 'true' : 'false';
}
if (is_scalar($value)) {
return (string) $value;
}
if (is_object($value)) {
return $this->readItem($value, $url, $vocabulary);
}
return null;
}
/**
* Ensures that the vocabulary URL is a valid absolute URL, and ensure that it has a path.
*
* Example: http://schema.org would return http://schema.org/
*
* @param string $url
*
* @return string|null An absolute URL, or null if the input is not valid.
*/
private function checkVocabularyUrl(string $url) : ?string
{
try {
$parts = parse($url);
} catch (InvalidUriException $e) {
return null;
}
if ($parts['scheme'] === null) {
return null;
}
if ($parts['host'] === null) {
return null;
}
if ($parts['path'] === null) {
$parts['path'] = '/';
}
return build($parts);
}
}