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])))*\*+/)#'; |
||
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 | * Array for keeping track of the hierarchy of objects, to prevent recursion |
||
88 | * |
||
89 | * @var NodeInterface[] |
||
90 | */ |
||
91 | protected $chain = []; |
||
92 | |||
93 | /** |
||
94 | * JSON-LD parser constructor |
||
95 | * |
||
96 | * @param UriInterface $uri Base URI |
||
97 | * @param LoggerInterface $logger Logger |
||
98 | */ |
||
99 | 6 | public function __construct(UriInterface $uri, LoggerInterface $logger) |
|
105 | |||
106 | /** |
||
107 | * Parse a DOM document |
||
108 | * |
||
109 | * @param \DOMDocument $dom DOM Document |
||
110 | * |
||
111 | * @return ParsingResultInterface Micro information items |
||
112 | * @throws \ReflectionException |
||
113 | */ |
||
114 | 5 | public function parseDom(\DOMDocument $dom) |
|
133 | |||
134 | /** |
||
135 | * Parse a JSON-LD document |
||
136 | * |
||
137 | * @param string $jsonLDDocSource JSON-LD document |
||
138 | * |
||
139 | * @return array Items |
||
140 | */ |
||
141 | 5 | protected function parseDocument($jsonLDDocSource) |
|
161 | |||
162 | /** |
||
163 | * Parse a JSON-LD root node |
||
164 | * |
||
165 | * @param \stdClass $jsonLDRoot JSON-LD root node |
||
166 | */ |
||
167 | 2 | protected function parseRootNode($jsonLDRoot) |
|
186 | |||
187 | /** |
||
188 | * Parse a JSON-LD node |
||
189 | * |
||
190 | * @param NodeInterface $node Node |
||
191 | * |
||
192 | * @return \stdClass|string Item or string ID |
||
193 | */ |
||
194 | 2 | protected function parseNode(NodeInterface $node) |
|
214 | |||
215 | /** |
||
216 | * Parse the type of a JSON-LD node |
||
217 | * |
||
218 | * @param NodeInterface $node Node |
||
219 | * |
||
220 | * @return array Item type |
||
221 | */ |
||
222 | 2 | protected function parseNodeType(NodeInterface $node): array |
|
241 | |||
242 | /** |
||
243 | * Parse the properties of a JSON-LD node |
||
244 | * |
||
245 | * @param NodeInterface $node Node |
||
246 | * |
||
247 | * @return array Item properties |
||
248 | */ |
||
249 | 2 | protected function parseNodeProperties(NodeInterface $node) |
|
269 | |||
270 | /** |
||
271 | * Initialize a JSON-LD node property (if necessary) |
||
272 | * |
||
273 | * @param string $name Property name |
||
274 | * @param array $properties Item properties |
||
275 | */ |
||
276 | 2 | protected function initializeNodeProperty($name, array &$properties) |
|
283 | |||
284 | /** |
||
285 | * Process a property value |
||
286 | * |
||
287 | * @param string $name Property name |
||
288 | * @param \stdClass|array|string $value Property value |
||
289 | * @param array $properties Item properties |
||
290 | */ |
||
291 | 2 | protected function processNodeProperty($name, $value, array &$properties) |
|
292 | { |
||
293 | // If this is a nested item |
||
294 | 2 | if (is_object($value)) { |
|
295 | 1 | $this->processNodePropertyObject($name, $value, $properties); |
|
296 | |||
297 | // Else: If this is a value list |
||
298 | 2 | } elseif (is_array($value)) { |
|
299 | foreach ($value as $listValue) { |
||
300 | $this->processNodeProperty($name, $listValue, $properties); |
||
301 | } |
||
302 | |||
303 | // Else: If the value is not empty |
||
304 | 2 | } elseif ($value) { |
|
305 | 2 | $properties[$name]->values[] = $value; |
|
306 | } |
||
307 | 2 | } |
|
308 | |||
309 | /** |
||
310 | * Process a property value object |
||
311 | * |
||
312 | * @param string $name Property name |
||
313 | * @param \stdClass $value Property value |
||
314 | * @param array $properties Properties |
||
315 | */ |
||
316 | 1 | protected function processNodePropertyObject($name, $value, array &$properties) |
|
326 | |||
327 | /** |
||
328 | * Parse a JSON-LD fragment |
||
329 | * |
||
330 | * @param NodeInterface|LanguageTaggedString|TypedValue|array $jsonLD JSON-LD fragment |
||
331 | * |
||
332 | * @return \stdClass|string|array Parsed fragment |
||
333 | */ |
||
334 | 2 | protected function parse($jsonLD) |
|
335 | { |
||
336 | // If it's a node object |
||
337 | 2 | if ($jsonLD instanceof NodeInterface) { |
|
338 | 2 | return $this->parseNode($jsonLD); |
|
339 | |||
340 | // Else if it's a language tagged string |
||
341 | 1 | } elseif ($jsonLD instanceof LanguageTaggedString) { |
|
342 | return $this->parseLanguageTaggedString($jsonLD); |
||
343 | |||
344 | // Else if it's a typed value |
||
345 | 1 | } elseif ($jsonLD instanceof TypedValue) { |
|
346 | 1 | return $this->parseTypedValue($jsonLD); |
|
347 | } |
||
348 | |||
349 | // Else if it's a list of items |
||
350 | return array_map([$this, 'parse'], $jsonLD); |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * Parse a language tagged string |
||
355 | * |
||
356 | * @param LanguageTaggedString $value Language tagged string |
||
357 | * |
||
358 | * @return \stdClass Value |
||
359 | */ |
||
360 | protected function parseLanguageTaggedString(LanguageTaggedString $value) |
||
361 | { |
||
362 | return (object)['value' => $value->getValue(), 'lang' => $value->getLanguage()]; |
||
363 | } |
||
364 | |||
365 | /** |
||
366 | * Parse a typed value |
||
367 | * |
||
368 | * @param TypedValue $value Typed value |
||
369 | * |
||
370 | * @return string Value |
||
371 | */ |
||
372 | 1 | protected function parseTypedValue(TypedValue $value) |
|
376 | |||
377 | 5 | private function sanitizeJsonSource($jsonLDDocSource) |
|
378 | { |
||
379 | 5 | $jsonLDDocSource = trim($jsonLDDocSource); |
|
414 | } |
||
415 |