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 | * 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 | 8 | 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 | 7 | 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 | 7 | protected function parseDocument($jsonLDDocSource) |
|
161 | |||
162 | /** |
||
163 | * Parse a JSON-LD root node |
||
164 | * |
||
165 | * @param \stdClass $jsonLDRoot JSON-LD root node |
||
166 | */ |
||
167 | 5 | 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 | 5 | 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 | 5 | protected function parseNodeType(NodeInterface $node): array |
|
240 | |||
241 | /** |
||
242 | * Parse the properties of a JSON-LD node |
||
243 | * |
||
244 | * @param NodeInterface $node Node |
||
245 | * |
||
246 | * @return array Item properties |
||
247 | */ |
||
248 | 5 | protected function parseNodeProperties(NodeInterface $node) |
|
268 | |||
269 | /** |
||
270 | * Initialize a JSON-LD node property (if necessary) |
||
271 | * |
||
272 | * @param string $name Property name |
||
273 | * @param array $properties Item properties |
||
274 | */ |
||
275 | 4 | protected function initializeNodeProperty($name, array &$properties) |
|
282 | |||
283 | /** |
||
284 | * Process a property value |
||
285 | * |
||
286 | * @param string $name Property name |
||
287 | * @param \stdClass|array|string $value Property value |
||
288 | * @param array $properties Item properties |
||
289 | */ |
||
290 | 4 | protected function processNodeProperty($name, $value, array &$properties) |
|
307 | |||
308 | /** |
||
309 | * Process a property value object |
||
310 | * |
||
311 | * @param string $name Property name |
||
312 | * @param \stdClass $value Property value |
||
313 | * @param array $properties Properties |
||
314 | */ |
||
315 | 3 | protected function processNodePropertyObject($name, $value, array &$properties) |
|
325 | |||
326 | /** |
||
327 | * Parse a JSON-LD fragment |
||
328 | * |
||
329 | * @param NodeInterface|LanguageTaggedString|TypedValue|array $jsonLD JSON-LD fragment |
||
330 | * |
||
331 | * @return \stdClass|string|array Parsed fragment |
||
332 | */ |
||
333 | 4 | protected function parse($jsonLD) |
|
351 | |||
352 | /** |
||
353 | * Parse a language tagged string |
||
354 | * |
||
355 | * @param LanguageTaggedString $value Language tagged string |
||
356 | * |
||
357 | * @return \stdClass Value |
||
358 | */ |
||
359 | 1 | protected function parseLanguageTaggedString(LanguageTaggedString $value) |
|
363 | |||
364 | /** |
||
365 | * Parse a typed value |
||
366 | * |
||
367 | * @param TypedValue $value Typed value |
||
368 | * |
||
369 | * @return string Value |
||
370 | */ |
||
371 | 3 | protected function parseTypedValue(TypedValue $value) |
|
375 | |||
376 | 7 | private function sanitizeJsonSource($jsonLDDocSource) |
|
386 | } |
||
387 |