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 |
|
213 | |||
214 | /** |
||
215 | * Parse the properties of a JSON-LD node |
||
216 | * |
||
217 | * @param NodeInterface $node Node |
||
218 | * |
||
219 | * @return array Item properties |
||
220 | */ |
||
221 | 3 | protected function parseNodeProperties(NodeInterface $node) |
|
241 | |||
242 | /** |
||
243 | * Initialize a JSON-LD node property (if necessary) |
||
244 | * |
||
245 | * @param string $name Property name |
||
246 | * @param array $properties Item properties |
||
247 | */ |
||
248 | 2 | protected function initializeNodeProperty($name, array &$properties) |
|
255 | |||
256 | /** |
||
257 | * Process a property value |
||
258 | * |
||
259 | * @param string $name Property name |
||
260 | * @param \stdClass|array|string $value Property value |
||
261 | * @param array $properties Item properties |
||
262 | */ |
||
263 | 2 | protected function processNodeProperty($name, $value, array &$properties) |
|
280 | |||
281 | /** |
||
282 | * Process a property value object |
||
283 | * |
||
284 | * @param string $name Property name |
||
285 | * @param \stdClass $value Property value |
||
286 | * @param array $properties Properties |
||
287 | */ |
||
288 | 2 | protected function processNodePropertyObject($name, $value, array &$properties) |
|
298 | |||
299 | /** |
||
300 | * Parse a JSON-LD fragment |
||
301 | * |
||
302 | * @param NodeInterface|LanguageTaggedString|TypedValue|array $jsonLD JSON-LD fragment |
||
303 | * |
||
304 | * @return \stdClass|string|array Parsed fragment |
||
305 | */ |
||
306 | 2 | protected function parse($jsonLD) |
|
324 | |||
325 | /** |
||
326 | * Parse a language tagged string |
||
327 | * |
||
328 | * @param LanguageTaggedString $value Language tagged string |
||
329 | * |
||
330 | * @return \stdClass Value |
||
331 | */ |
||
332 | 1 | protected function parseLanguageTaggedString(LanguageTaggedString $value) |
|
336 | |||
337 | /** |
||
338 | * Parse a typed value |
||
339 | * |
||
340 | * @param TypedValue $value Typed value |
||
341 | * |
||
342 | * @return string Value |
||
343 | */ |
||
344 | 2 | protected function parseTypedValue(TypedValue $value) |
|
348 | |||
349 | 5 | private function sanitizeJsonSource($jsonLDDocSource) |
|
359 | } |
||
360 |