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 | 5 | 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 | 4 | public function parseDom(\DOMDocument $dom) |
|
109 | { |
||
110 | 4 | $this->logger->info('Running parser: '.(new \ReflectionClass(__CLASS__))->getShortName()); |
|
111 | 4 | $items = []; |
|
112 | |||
113 | // Find and process all JSON-LD documents |
||
114 | 4 | $xpath = new \DOMXPath($dom); |
|
115 | 4 | $jsonLDDocs = $xpath->query('//*[local-name(.) = "script"][@type = "application/ld+json"]'); |
|
116 | 4 | $this->logger->debug('Processing '.$jsonLDDocs->length.' JSON-LD documents'); |
|
117 | |||
118 | // Run through all JSON-LD documents |
||
119 | 4 | foreach ($jsonLDDocs as $jsonLDDoc) { |
|
120 | 4 | $jsonLDDocSource = preg_replace(self::JSON_COMMENT_PATTERN, '', $jsonLDDoc->textContent); |
|
121 | 4 | $i = $this->parseDocument($jsonLDDocSource); |
|
122 | 3 | $items = array_merge($items, $i); |
|
123 | } |
||
124 | |||
125 | 3 | return new ParsingResult(self::FORMAT, $items); |
|
126 | } |
||
127 | |||
128 | /** |
||
129 | * Parse a JSON-LD document |
||
130 | * |
||
131 | * @param string $jsonLDDocSource JSON-LD document |
||
132 | * |
||
133 | * @return array Items |
||
134 | */ |
||
135 | 4 | protected function parseDocument($jsonLDDocSource) |
|
136 | { |
||
137 | 4 | $jsonLDDocSource = $this->sanitizeJsonSource($jsonLDDocSource); |
|
138 | |||
139 | // Unserialize the JSON-LD document |
||
140 | 4 | $jsonLDDoc = @json_decode($jsonLDDocSource); |
|
141 | |||
142 | // If this is not a valid JSON document: Return |
||
143 | 4 | if (!is_object($jsonLDDoc) && !is_array($jsonLDDoc)) { |
|
144 | 3 | $this->logger->error('Skipping invalid JSON-LD document'); |
|
145 | |||
146 | 2 | return []; |
|
147 | } |
||
148 | |||
149 | // Parse the document |
||
150 | 2 | return array_filter( |
|
151 | 2 | is_array($jsonLDDoc) ? |
|
152 | 2 | array_map([$this, 'parseRootNode'], $jsonLDDoc) : [$this->parseRootNode($jsonLDDoc)] |
|
153 | ); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * Parse a JSON-LD root node |
||
158 | * |
||
159 | * @param \stdClass $jsonLDRoot JSON-LD root node |
||
160 | */ |
||
161 | 2 | protected function parseRootNode($jsonLDRoot) |
|
162 | { |
||
163 | 2 | $item = null; |
|
164 | |||
165 | try { |
||
166 | 2 | $jsonDLDocument = JsonLDParser::getDocument($jsonLDRoot, ['documentLoader' => $this->contextLoader]); |
|
167 | |||
168 | // Run through all nodes to parse the first one |
||
169 | /** @var Node $node */ |
||
170 | 2 | foreach ($jsonDLDocument->getGraph()->getNodes() as $node) { |
|
171 | 2 | $item = $this->parseNode($node); |
|
172 | 2 | break; |
|
173 | } |
||
174 | } catch (JsonLdException $exception) { |
||
175 | $this->logger->error($exception->getMessage(), ['exception' => $exception]); |
||
176 | } |
||
177 | |||
178 | 2 | return $item; |
|
179 | } |
||
180 | |||
181 | /** |
||
182 | * Parse a JSON-LD node |
||
183 | * |
||
184 | * @param NodeInterface $node Node |
||
185 | * |
||
186 | * @return \stdClass Item |
||
187 | */ |
||
188 | 2 | protected function parseNode(NodeInterface $node) |
|
189 | { |
||
190 | return (object)[ |
||
191 | 2 | 'type' => $this->parseNodeType($node), |
|
192 | 2 | 'id' => $node->getId() ?: null, |
|
193 | 2 | 'properties' => $this->parseNodeProperties($node), |
|
194 | ]; |
||
195 | } |
||
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 | 2 | protected function parseNodeType(NodeInterface $node) |
|
205 | { |
||
206 | /** @var Node $itemType */ |
||
207 | 2 | return ($itemType = $node->getType()) ? [$this->vocabularyCache->expandIRI($itemType->getId())] : []; |
|
208 | } |
||
209 | |||
210 | /** |
||
211 | * Parse the properties of a JSON-LD node |
||
212 | * |
||
213 | * @param NodeInterface $node Node |
||
214 | * |
||
215 | * @return array Item properties |
||
216 | */ |
||
217 | 2 | protected function parseNodeProperties(NodeInterface $node) |
|
218 | { |
||
219 | 2 | $properties = []; |
|
220 | |||
221 | // Run through all node properties |
||
222 | 2 | foreach ($node->getProperties() as $name => $property) { |
|
223 | // Skip the node type |
||
224 | 2 | if ($name === Node::TYPE) { |
|
225 | 2 | continue; |
|
226 | } |
||
227 | |||
228 | // Initialize the property (if necessary) |
||
229 | 2 | $this->initializeNodeProperty($name, $properties); |
|
230 | |||
231 | // Parse and process the property value |
||
232 | 2 | $this->processNodeProperty($name, $this->parse($property), $properties); |
|
233 | } |
||
234 | |||
235 | 2 | return $properties; |
|
236 | } |
||
237 | |||
238 | /** |
||
239 | * Initialize a JSON-LD node property (if necessary) |
||
240 | * |
||
241 | * @param string $name Property name |
||
242 | * @param array $properties Item properties |
||
243 | */ |
||
244 | 2 | protected function initializeNodeProperty($name, array &$properties) |
|
245 | { |
||
246 | 2 | if (empty($properties[$name])) { |
|
247 | 2 | $properties[$name] = $this->vocabularyCache->expandIRI($name); |
|
248 | 2 | $properties[$name]->values = []; |
|
249 | } |
||
250 | 2 | } |
|
251 | |||
252 | /** |
||
253 | * Process a property value |
||
254 | * |
||
255 | * @param string $name Property name |
||
256 | * @param \stdClass|array|string $value Property value |
||
257 | * @param array $properties Item properties |
||
258 | */ |
||
259 | 2 | protected function processNodeProperty($name, $value, array &$properties) |
|
260 | { |
||
261 | // If this is a nested item |
||
262 | 2 | if (is_object($value)) { |
|
263 | 2 | $this->processNodePropertyObject($name, $value, $properties); |
|
264 | |||
265 | // Else: If this is a value list |
||
266 | 2 | } elseif (is_array($value)) { |
|
267 | 2 | foreach ($value as $listValue) { |
|
268 | 2 | $this->processNodeProperty($name, $listValue, $properties); |
|
269 | } |
||
270 | |||
271 | // Else: If the value is not empty |
||
272 | 2 | } elseif ($value) { |
|
273 | 2 | $properties[$name]->values[] = $value; |
|
274 | } |
||
275 | 2 | } |
|
276 | |||
277 | /** |
||
278 | * Process a property value object |
||
279 | * |
||
280 | * @param string $name Property name |
||
281 | * @param \stdClass $value Property value |
||
282 | * @param array $properties Properties |
||
283 | */ |
||
284 | 2 | protected function processNodePropertyObject($name, $value, array &$properties) |
|
285 | { |
||
286 | 2 | if (!empty($value->type) || !empty($value->lang)) { |
|
287 | 2 | $properties[$name]->values[] = $value; |
|
288 | |||
289 | // @type = @id |
||
290 | 2 | } elseif (!empty($value->id)) { |
|
291 | 2 | $properties[$name]->values[] = $value->id; |
|
292 | } |
||
293 | 2 | } |
|
294 | |||
295 | /** |
||
296 | * Parse a JSON-LD fragment |
||
297 | * |
||
298 | * @param NodeInterface|LanguageTaggedString|TypedValue|array $jsonLD JSON-LD fragment |
||
299 | * |
||
300 | * @return \stdClass|string|array Parsed fragment |
||
301 | */ |
||
302 | 2 | protected function parse($jsonLD) |
|
303 | { |
||
304 | // If it's a node object |
||
305 | 2 | if ($jsonLD instanceof NodeInterface) { |
|
306 | 2 | return $this->parseNode($jsonLD); |
|
307 | |||
308 | // Else if it's a language tagged string |
||
309 | 2 | } elseif ($jsonLD instanceof LanguageTaggedString) { |
|
310 | 1 | return $this->parseLanguageTaggedString($jsonLD); |
|
311 | |||
312 | // Else if it's a typed value |
||
313 | 2 | } elseif ($jsonLD instanceof TypedValue) { |
|
314 | 2 | return $this->parseTypedValue($jsonLD); |
|
315 | } |
||
316 | |||
317 | // Else if it's a list of items |
||
318 | 2 | return array_map([$this, 'parse'], $jsonLD); |
|
319 | } |
||
320 | |||
321 | /** |
||
322 | * Parse a language tagged string |
||
323 | * |
||
324 | * @param LanguageTaggedString $value Language tagged string |
||
325 | * |
||
326 | * @return \stdClass Value |
||
327 | */ |
||
328 | 1 | protected function parseLanguageTaggedString(LanguageTaggedString $value) |
|
332 | |||
333 | /** |
||
334 | * Parse a typed value |
||
335 | * |
||
336 | * @param TypedValue $value Typed value |
||
337 | * |
||
338 | * @return string Value |
||
339 | */ |
||
340 | 2 | protected function parseTypedValue(TypedValue $value) |
|
344 | |||
345 | 4 | private function sanitizeJsonSource($jsonLDDocSource) |
|
346 | { |
||
347 | 4 | $jsonLDDocSource = trim($jsonLDDocSource); |
|
348 | |||
349 | 4 | if (substr($jsonLDDocSource, -1) === ';') { |
|
350 | $jsonLDDocSource = substr_replace($jsonLDDocSource, '', -1); |
||
351 | } |
||
352 | |||
353 | 4 | return $jsonLDDocSource; |
|
354 | } |
||
355 | } |
||
356 |