1 | <?php |
||
60 | class JsonLD extends AbstractParser |
||
61 | { |
||
62 | /** |
||
63 | * Format |
||
64 | * |
||
65 | * @var int |
||
66 | */ |
||
67 | const FORMAT = Format::JSON_LD; |
||
68 | /** |
||
69 | * Regex pattern for matching leading comments in a JSON string |
||
70 | * |
||
71 | * @var string |
||
72 | */ |
||
73 | const JSON_COMMENT_PATTERN = '#(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|([\s\t]//.*)|(^//.*)#'; |
||
74 | /** |
||
75 | * Vocabulary cache |
||
76 | * |
||
77 | * @var VocabularyCache |
||
78 | */ |
||
79 | protected $vocabularyCache; |
||
80 | /** |
||
81 | * Context loader |
||
82 | * |
||
83 | * @var CachingContextLoader |
||
84 | */ |
||
85 | protected $contextLoader; |
||
86 | |||
87 | /** |
||
88 | * JSON-LD parser constructor |
||
89 | * |
||
90 | * @param UriInterface $uri Base URI |
||
91 | * @param LoggerInterface $logger Logger |
||
92 | */ |
||
93 | 6 | public function __construct(UriInterface $uri, LoggerInterface $logger) |
|
99 | |||
100 | /** |
||
101 | * Parse a DOM document |
||
102 | * |
||
103 | * @param \DOMDocument $dom DOM Document |
||
104 | * |
||
105 | * @return ParsingResultInterface Micro information items |
||
106 | * @throws \ReflectionException |
||
107 | */ |
||
108 | 5 | public function parseDom(\DOMDocument $dom) |
|
127 | |||
128 | /** |
||
129 | * Parse a JSON-LD document |
||
130 | * |
||
131 | * @param string $jsonLDDocSource JSON-LD document |
||
132 | * |
||
133 | * @return array Items |
||
134 | */ |
||
135 | 5 | protected function parseDocument($jsonLDDocSource) |
|
155 | |||
156 | /** |
||
157 | * Parse a JSON-LD root node |
||
158 | * |
||
159 | * @param \stdClass $jsonLDRoot JSON-LD root node |
||
160 | */ |
||
161 | 3 | protected function parseRootNode($jsonLDRoot) |
|
180 | |||
181 | /** |
||
182 | * Parse a JSON-LD node |
||
183 | * |
||
184 | * @param NodeInterface $node Node |
||
185 | * |
||
186 | * @return \stdClass Item |
||
187 | */ |
||
188 | 3 | protected function parseNode(NodeInterface $node) |
|
196 | |||
197 | /** |
||
198 | * Parse the type of a JSON-LD node |
||
199 | * |
||
200 | * @param NodeInterface $node Node |
||
201 | * |
||
202 | * @return array Item type |
||
203 | */ |
||
204 | 3 | protected function parseNodeType(NodeInterface $node): array |
|
205 | { |
||
206 | 3 | ||
207 | 1 | /** @var NodeInterface|NodeInterface[] $itemTypes */ |
|
208 | $itemTypes = $node->getType(); |
||
209 | $itemTypes = is_array($itemTypes) ? $itemTypes : [$itemTypes]; |
||
210 | $itemTypes = array_filter($itemTypes); |
||
211 | 3 | ||
212 | 3 | if (empty($itemTypes)) { |
|
213 | 3 | return []; |
|
214 | } |
||
215 | 3 | ||
216 | 2 | $types = []; |
|
217 | foreach ($itemTypes as $itemType) { |
||
218 | $types[] = $this->vocabularyCache->expandIRI($itemType->getId()); |
||
219 | 3 | } |
|
220 | 3 | ||
221 | 3 | return $types; |
|
222 | } |
||
223 | |||
224 | 3 | /** |
|
225 | * Parse the properties of a JSON-LD node |
||
226 | * |
||
227 | * @param NodeInterface $node Node |
||
228 | * |
||
229 | * @return array Item properties |
||
230 | */ |
||
231 | protected function parseNodeProperties(NodeInterface $node) |
||
232 | { |
||
233 | $properties = []; |
||
234 | 3 | ||
235 | // Run through all node properties |
||
236 | 3 | foreach ($node->getProperties() as $name => $property) { |
|
237 | // Skip the node type |
||
238 | if ($name === Node::TYPE) { |
||
239 | 3 | continue; |
|
240 | } |
||
241 | 3 | ||
242 | 3 | // Initialize the property (if necessary) |
|
243 | $this->initializeNodeProperty($name, $properties); |
||
244 | |||
245 | // Parse and process the property value |
||
246 | 2 | $this->processNodeProperty($name, $this->parse($property), $properties); |
|
247 | } |
||
248 | |||
249 | 2 | return $properties; |
|
250 | } |
||
251 | |||
252 | 3 | /** |
|
253 | * Initialize a JSON-LD node property (if necessary) |
||
254 | * |
||
255 | * @param string $name Property name |
||
256 | * @param array $properties Item properties |
||
257 | */ |
||
258 | protected function initializeNodeProperty($name, array &$properties) |
||
259 | { |
||
260 | if (empty($properties[$name])) { |
||
261 | 2 | $properties[$name] = $this->vocabularyCache->expandIRI($name); |
|
262 | $properties[$name]->values = []; |
||
263 | 2 | } |
|
264 | 2 | } |
|
265 | 2 | ||
266 | /** |
||
267 | 2 | * Process a property value |
|
268 | * |
||
269 | * @param string $name Property name |
||
270 | * @param \stdClass|array|string $value Property value |
||
271 | * @param array $properties Item properties |
||
272 | */ |
||
273 | protected function processNodeProperty($name, $value, array &$properties) |
||
274 | { |
||
275 | // If this is a nested item |
||
276 | 2 | if (is_object($value)) { |
|
277 | $this->processNodePropertyObject($name, $value, $properties); |
||
278 | |||
279 | 2 | // Else: If this is a value list |
|
280 | 2 | } elseif (is_array($value)) { |
|
281 | foreach ($value as $listValue) { |
||
282 | $this->processNodeProperty($name, $listValue, $properties); |
||
283 | 2 | } |
|
284 | 2 | ||
285 | 2 | // Else: If the value is not empty |
|
286 | } elseif ($value) { |
||
287 | $properties[$name]->values[] = $value; |
||
288 | } |
||
289 | 2 | } |
|
290 | 2 | ||
291 | /** |
||
292 | 2 | * Process a property value object |
|
293 | * |
||
294 | * @param string $name Property name |
||
295 | * @param \stdClass $value Property value |
||
296 | * @param array $properties Properties |
||
297 | */ |
||
298 | protected function processNodePropertyObject($name, $value, array &$properties) |
||
299 | { |
||
300 | if (!empty($value->type) || !empty($value->lang)) { |
||
301 | 2 | $properties[$name]->values[] = $value; |
|
302 | |||
303 | 2 | // @type = @id |
|
304 | 2 | } elseif (!empty($value->id)) { |
|
305 | $properties[$name]->values[] = $value->id; |
||
306 | } |
||
307 | 2 | } |
|
308 | 2 | ||
309 | /** |
||
310 | 2 | * Parse a JSON-LD fragment |
|
311 | * |
||
312 | * @param NodeInterface|LanguageTaggedString|TypedValue|array $jsonLD JSON-LD fragment |
||
313 | * |
||
314 | * @return \stdClass|string|array Parsed fragment |
||
315 | */ |
||
316 | protected function parse($jsonLD) |
||
317 | { |
||
318 | // If it's a node object |
||
319 | 2 | if ($jsonLD instanceof NodeInterface) { |
|
320 | return $this->parseNode($jsonLD); |
||
321 | |||
322 | 2 | // Else if it's a language tagged string |
|
323 | 2 | } elseif ($jsonLD instanceof LanguageTaggedString) { |
|
324 | return $this->parseLanguageTaggedString($jsonLD); |
||
325 | |||
326 | 2 | // Else if it's a typed value |
|
327 | 1 | } elseif ($jsonLD instanceof TypedValue) { |
|
328 | return $this->parseTypedValue($jsonLD); |
||
329 | } |
||
330 | 2 | ||
331 | 2 | // Else if it's a list of items |
|
332 | return array_map([$this, 'parse'], $jsonLD); |
||
333 | } |
||
334 | |||
335 | 2 | /** |
|
336 | * Parse a language tagged string |
||
337 | * |
||
338 | * @param LanguageTaggedString $value Language tagged string |
||
339 | * |
||
340 | * @return \stdClass Value |
||
341 | */ |
||
342 | protected function parseLanguageTaggedString(LanguageTaggedString $value) |
||
343 | { |
||
344 | return (object)['value' => $value->getValue(), 'lang' => $value->getLanguage()]; |
||
345 | 1 | } |
|
346 | |||
347 | 1 | /** |
|
348 | * Parse a typed value |
||
349 | * |
||
350 | * @param TypedValue $value Typed value |
||
351 | * |
||
352 | * @return string Value |
||
353 | */ |
||
354 | protected function parseTypedValue(TypedValue $value) |
||
355 | { |
||
356 | return $value->getValue(); |
||
357 | 2 | } |
|
358 | |||
359 | 2 | private function sanitizeJsonSource($jsonLDDocSource) |
|
372 | } |
||
373 |