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 |
|
222 | |||
223 | /** |
||
224 | * Parse the properties of a JSON-LD node |
||
225 | * |
||
226 | * @param NodeInterface $node Node |
||
227 | * |
||
228 | * @return array Item properties |
||
229 | */ |
||
230 | 3 | protected function parseNodeProperties(NodeInterface $node) |
|
250 | |||
251 | /** |
||
252 | * Initialize a JSON-LD node property (if necessary) |
||
253 | * |
||
254 | * @param string $name Property name |
||
255 | * @param array $properties Item properties |
||
256 | */ |
||
257 | 2 | protected function initializeNodeProperty($name, array &$properties) |
|
264 | |||
265 | /** |
||
266 | * Process a property value |
||
267 | * |
||
268 | * @param string $name Property name |
||
269 | * @param \stdClass|array|string $value Property value |
||
270 | * @param array $properties Item properties |
||
271 | */ |
||
272 | 2 | protected function processNodeProperty($name, $value, array &$properties) |
|
289 | |||
290 | /** |
||
291 | * Process a property value object |
||
292 | * |
||
293 | * @param string $name Property name |
||
294 | * @param \stdClass $value Property value |
||
295 | * @param array $properties Properties |
||
296 | */ |
||
297 | 2 | protected function processNodePropertyObject($name, $value, array &$properties) |
|
307 | |||
308 | /** |
||
309 | * Parse a JSON-LD fragment |
||
310 | * |
||
311 | * @param NodeInterface|LanguageTaggedString|TypedValue|array $jsonLD JSON-LD fragment |
||
312 | * |
||
313 | * @return \stdClass|string|array Parsed fragment |
||
314 | */ |
||
315 | 2 | protected function parse($jsonLD) |
|
333 | |||
334 | /** |
||
335 | * Parse a language tagged string |
||
336 | * |
||
337 | * @param LanguageTaggedString $value Language tagged string |
||
338 | * |
||
339 | * @return \stdClass Value |
||
340 | */ |
||
341 | 1 | protected function parseLanguageTaggedString(LanguageTaggedString $value) |
|
345 | |||
346 | /** |
||
347 | * Parse a typed value |
||
348 | * |
||
349 | * @param TypedValue $value Typed value |
||
350 | * |
||
351 | * @return string Value |
||
352 | */ |
||
353 | 2 | protected function parseTypedValue(TypedValue $value) |
|
357 | |||
358 | 5 | private function sanitizeJsonSource($jsonLDDocSource) |
|
368 | } |
||
369 |