|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* rdfa-lite-microdata |
|
5
|
|
|
* |
|
6
|
|
|
* @category Jkphl |
|
7
|
|
|
* @package Jkphl\RdfaLiteMicrodata |
|
8
|
|
|
* @subpackage Jkphl\RdfaLiteMicrodata\Infrastructure |
|
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
|
10
|
|
|
* @copyright Copyright © 2017 Joschi Kuphal <[email protected]> / @jkphl |
|
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
/*********************************************************************************** |
|
15
|
|
|
* The MIT License (MIT) |
|
16
|
|
|
* |
|
17
|
|
|
* Copyright © 2017 Joschi Kuphal <[email protected]> / @jkphl |
|
18
|
|
|
* |
|
19
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
|
20
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
|
21
|
|
|
* the Software without restriction, including without limitation the rights to |
|
22
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
|
23
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
|
24
|
|
|
* subject to the following conditions: |
|
25
|
|
|
* |
|
26
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
27
|
|
|
* copies or substantial portions of the Software. |
|
28
|
|
|
* |
|
29
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
30
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
|
31
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
|
32
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
|
33
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
|
34
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
35
|
|
|
***********************************************************************************/ |
|
36
|
|
|
|
|
37
|
|
|
namespace Jkphl\RdfaLiteMicrodata\Infrastructure\Parser; |
|
38
|
|
|
|
|
39
|
|
|
use Jkphl\RdfaLiteMicrodata\Application\Context\ContextInterface; |
|
40
|
|
|
use Jkphl\RdfaLiteMicrodata\Application\Contract\ElementProcessorInterface; |
|
41
|
|
|
use Jkphl\RdfaLiteMicrodata\Domain\Property\Property; |
|
42
|
|
|
use Jkphl\RdfaLiteMicrodata\Domain\Thing\Thing; |
|
43
|
|
|
use Jkphl\RdfaLiteMicrodata\Domain\Thing\ThingInterface; |
|
44
|
|
|
use Jkphl\RdfaLiteMicrodata\Domain\Type\Type; |
|
45
|
|
|
use Jkphl\RdfaLiteMicrodata\Domain\Type\TypeInterface; |
|
46
|
|
|
use Jkphl\RdfaLiteMicrodata\Domain\Vocabulary\VocabularyInterface; |
|
47
|
|
|
use Jkphl\RdfaLiteMicrodata\Infrastructure\Exceptions\OutOfBoundsException; |
|
48
|
|
|
use Jkphl\RdfaLiteMicrodata\Infrastructure\Exceptions\RuntimeException; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Abstract element processor |
|
52
|
|
|
* |
|
53
|
|
|
* @package Jkphl\RdfaLiteMicrodata |
|
54
|
|
|
* @subpackage Jkphl\RdfaLiteMicrodata\Infrastructure |
|
55
|
|
|
*/ |
|
56
|
|
|
abstract class AbstractElementProcessor implements ElementProcessorInterface |
|
57
|
|
|
{ |
|
58
|
|
|
/** |
|
59
|
|
|
* First property |
|
60
|
|
|
* |
|
61
|
|
|
* @var int |
|
62
|
|
|
*/ |
|
63
|
|
|
const PROPERTY_FIRST = 1; |
|
64
|
|
|
/** |
|
65
|
|
|
* Last property |
|
66
|
|
|
* |
|
67
|
|
|
* @var int |
|
68
|
|
|
*/ |
|
69
|
|
|
const PROPERTY_LAST = 2; |
|
70
|
|
|
/** |
|
71
|
|
|
* Tag name / attribute map |
|
72
|
|
|
* |
|
73
|
|
|
* @var array |
|
74
|
|
|
*/ |
|
75
|
|
|
protected static $tagNameAttributes = [ |
|
76
|
|
|
'META' => 'content', |
|
77
|
|
|
'AUDIO' => 'src', |
|
78
|
|
|
'EMBED' => 'src', |
|
79
|
|
|
'IFRAME' => 'src', |
|
80
|
|
|
'IMG' => 'src', |
|
81
|
|
|
'SOURCE' => 'src', |
|
82
|
|
|
'TRACK' => 'src', |
|
83
|
|
|
'VIDEO' => 'src', |
|
84
|
|
|
'A' => 'href', |
|
85
|
|
|
'AREA' => 'href', |
|
86
|
|
|
'LINK' => 'href', |
|
87
|
|
|
'OBJECT' => 'data', |
|
88
|
|
|
'DATA' => 'value', |
|
89
|
|
|
'METER' => 'value', |
|
90
|
|
|
'TIME' => 'datetime' |
|
91
|
|
|
]; |
|
92
|
|
|
/** |
|
93
|
|
|
* Property cache |
|
94
|
|
|
* |
|
95
|
|
|
* @var array |
|
96
|
|
|
*/ |
|
97
|
|
|
protected static $propertyCache = []; |
|
98
|
|
|
/** |
|
99
|
|
|
* HTML mode |
|
100
|
|
|
* |
|
101
|
|
|
* @var boolean |
|
102
|
|
|
*/ |
|
103
|
|
|
protected $html; |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Element processor constructor |
|
107
|
|
|
* |
|
108
|
|
|
* @param bool $html HTML mode |
|
109
|
|
|
*/ |
|
110
|
13 |
|
public function __construct($html = false) |
|
111
|
|
|
{ |
|
112
|
13 |
|
$this->html = boolval($html); |
|
113
|
13 |
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Process a DOM element's child |
|
117
|
|
|
* |
|
118
|
|
|
* @param \DOMElement $element DOM element |
|
119
|
|
|
* @param ContextInterface $context Context |
|
120
|
|
|
* @return ContextInterface Context for children |
|
121
|
|
|
*/ |
|
122
|
13 |
|
public function processElementChildren(\DOMElement $element, ContextInterface $context) |
|
123
|
|
|
{ |
|
124
|
|
|
// Process a child |
|
125
|
13 |
|
return $this->processChild($element, $context); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Create a nested child |
|
130
|
|
|
* |
|
131
|
|
|
* @param \DOMElement $element DOM element |
|
132
|
|
|
* @param ContextInterface $context Context |
|
133
|
|
|
* @return ContextInterface Context for children |
|
134
|
|
|
*/ |
|
135
|
|
|
abstract protected function processChild(\DOMElement $element, ContextInterface $context); |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Create a property |
|
139
|
|
|
* |
|
140
|
|
|
* @param \DOMElement $element DOM element |
|
141
|
|
|
* @param ContextInterface $context Inherited Context |
|
142
|
|
|
* @return ContextInterface Local context for this element |
|
143
|
|
|
*/ |
|
144
|
|
|
abstract protected function processProperty(\DOMElement $element, ContextInterface $context); |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Create a property by prefix and name |
|
148
|
|
|
* |
|
149
|
|
|
* @param string $prefix Property prefix |
|
150
|
|
|
* @param string $name Property name |
|
151
|
|
|
* @param \DOMElement $element DOM element |
|
152
|
|
|
* @param ContextInterface $context Inherited Context |
|
153
|
|
|
* @param int $mode Property mode |
|
154
|
|
|
* @return ContextInterface Local context for this element |
|
155
|
|
|
*/ |
|
156
|
7 |
|
protected function processPropertyPrefixName( |
|
157
|
|
|
$prefix, |
|
158
|
|
|
$name, |
|
159
|
|
|
\DOMElement $element, |
|
160
|
|
|
ContextInterface $context, |
|
161
|
|
|
$mode |
|
162
|
|
|
) { |
|
163
|
7 |
|
$vocabulary = $this->getVocabulary($prefix, $context); |
|
164
|
7 |
|
if ($vocabulary instanceof VocabularyInterface) { |
|
165
|
7 |
|
$context = $this->addProperty($element, $context, $name, $vocabulary, $mode); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
6 |
|
return $context; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Return a vocabulary by prefix with fallback to the default vocabulary |
|
173
|
|
|
* |
|
174
|
|
|
* @param string $prefix Vocabulary prefix |
|
175
|
|
|
* @param ContextInterface $context Context |
|
176
|
|
|
* @return VocabularyInterface Vocabulary |
|
177
|
|
|
*/ |
|
178
|
|
|
abstract protected function getVocabulary($prefix, ContextInterface $context); |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Add a single property |
|
182
|
|
|
* |
|
183
|
|
|
* @param \DOMElement $element DOM element |
|
184
|
|
|
* @param ContextInterface $context Inherited Context |
|
185
|
|
|
* @param string $name Property name |
|
186
|
|
|
* @param VocabularyInterface $vocabulary Property vocabulary |
|
187
|
|
|
* @param int $mode Property mode |
|
188
|
|
|
* @return ContextInterface Local context for this element |
|
189
|
|
|
*/ |
|
190
|
7 |
|
protected function addProperty( |
|
191
|
|
|
\DOMElement $element, |
|
192
|
|
|
ContextInterface $context, |
|
193
|
|
|
$name, |
|
194
|
|
|
VocabularyInterface $vocabulary, |
|
195
|
|
|
$mode |
|
196
|
|
|
) { |
|
197
|
7 |
|
$resourceId = $this->getResourceId($element); |
|
198
|
|
|
|
|
199
|
|
|
// Get the property value |
|
200
|
7 |
|
$propertyValue = $this->getPropertyValue($element, $context); |
|
201
|
7 |
|
$property = new Property($name, $vocabulary, $propertyValue, $resourceId); |
|
202
|
|
|
|
|
203
|
|
|
// Add the property to the current parent thing |
|
204
|
6 |
|
$context->getParentThing()->addProperty($property); |
|
205
|
|
|
|
|
206
|
|
|
// If the property value is a thing and this is the element's last property |
|
207
|
6 |
|
if (($propertyValue instanceof ThingInterface) && ($mode & self::PROPERTY_LAST)) { |
|
208
|
|
|
// Set the thing as parent thing for nested iterations |
|
209
|
3 |
|
$context = $context->setParentThing($propertyValue); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
6 |
|
return $context; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Return the resource ID |
|
217
|
|
|
* |
|
218
|
|
|
* @param \DOMElement $element DOM element |
|
219
|
|
|
* @return string|null Resource ID |
|
220
|
|
|
*/ |
|
221
|
|
|
abstract protected function getResourceId(\DOMElement $element); |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Return a property value (type and tag name dependent) |
|
225
|
|
|
* |
|
226
|
|
|
* @param \DOMElement $element DOM element |
|
227
|
|
|
* @param ContextInterface $context Context |
|
228
|
|
|
* @return ThingInterface|string Property value |
|
229
|
|
|
*/ |
|
230
|
7 |
|
protected function getPropertyValue(\DOMElement $element, ContextInterface $context) |
|
231
|
|
|
{ |
|
232
|
7 |
|
$value = $this->getPropertyCache($element); |
|
233
|
7 |
|
if ($value !== null) { |
|
234
|
1 |
|
return $value; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
7 |
|
$propertyChild = $this->getPropertyChildValue($element, $context); |
|
238
|
7 |
|
if ($propertyChild instanceof ThingInterface) { |
|
239
|
3 |
|
return $this->setPropertyCache($element, $propertyChild); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
// Return a string property value |
|
243
|
7 |
|
return $this->setPropertyCache($element, $this->getPropertyStringValue($element)); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* Return a cached property value (if available) |
|
248
|
|
|
* |
|
249
|
|
|
* @param \DOMElement $element Element |
|
250
|
|
|
* @return mixed|null Property value |
|
251
|
|
|
*/ |
|
252
|
7 |
|
protected function getPropertyCache(\DOMElement $element) |
|
253
|
|
|
{ |
|
254
|
7 |
|
$elementHash = spl_object_hash($element); |
|
255
|
7 |
|
return isset(self::$propertyCache[$elementHash]) ? self::$propertyCache[$elementHash] : null; |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* Return a property child value |
|
260
|
|
|
* |
|
261
|
|
|
* @param \DOMElement $element DOM element |
|
262
|
|
|
* @param ContextInterface $context Context |
|
263
|
|
|
* @return ThingInterface|null Property child value |
|
264
|
|
|
*/ |
|
265
|
|
|
abstract protected function getPropertyChildValue(\DOMElement $element, ContextInterface $context); |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* Cache a property value |
|
269
|
|
|
* |
|
270
|
|
|
* @param \DOMElement $element DOM element |
|
271
|
|
|
* @param mixed $value Value |
|
272
|
|
|
* @return mixed $value Value |
|
273
|
|
|
*/ |
|
274
|
7 |
|
protected function setPropertyCache(\DOMElement $element, $value) |
|
275
|
|
|
{ |
|
276
|
7 |
|
return self::$propertyCache[spl_object_hash($element)] = $value; |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* Return a property value (type and tag name dependent) |
|
281
|
|
|
* |
|
282
|
|
|
* @param \DOMElement $element DOM element |
|
283
|
|
|
* @return ThingInterface|string Property value |
|
284
|
|
|
*/ |
|
285
|
7 |
|
protected function getPropertyStringValue(\DOMElement $element) |
|
286
|
|
|
{ |
|
287
|
|
|
// If HTML mode is active |
|
288
|
7 |
|
if ($this->html) { |
|
289
|
6 |
|
$tagName = strtoupper($element->tagName); |
|
290
|
|
|
|
|
291
|
|
|
// Map to an attribute (if applicable) |
|
292
|
6 |
|
if (array_key_exists($tagName, self::$tagNameAttributes)) { |
|
293
|
5 |
|
$value = strval($element->getAttribute(self::$tagNameAttributes[$tagName])); |
|
294
|
5 |
|
if (($tagName != 'TIME') || !empty($value)) { |
|
295
|
5 |
|
return $value; |
|
296
|
|
|
} |
|
297
|
|
|
} |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
// Return the text content |
|
301
|
7 |
|
return $element->textContent; |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
/** |
|
305
|
|
|
* Return a thing by typeof value |
|
306
|
|
|
* |
|
307
|
|
|
* @param string $typeof Thing type |
|
308
|
|
|
* @param string $resourceId Resource ID |
|
309
|
|
|
* @param ContextInterface $context Context |
|
310
|
|
|
* @return Thing Thing |
|
311
|
|
|
* @throws RuntimeException If the default vocabulary is empty |
|
312
|
|
|
*/ |
|
313
|
13 |
|
protected function getThing($typeof, $resourceId, ContextInterface $context) |
|
314
|
|
|
{ |
|
315
|
|
|
/** @var TypeInterface[] $types */ |
|
316
|
13 |
|
$types = []; |
|
317
|
13 |
|
foreach (preg_split('/\s+/', $typeof) as $prefixedType) { |
|
318
|
13 |
|
$types[] = $this->getType($prefixedType, $context); |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
7 |
|
return new Thing($types, $resourceId); |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
|
|
/** |
|
325
|
|
|
* Instanciate a type |
|
326
|
|
|
* |
|
327
|
|
|
* @param string $prefixedType Prefixed type |
|
328
|
|
|
* @param ContextInterface $context Context |
|
329
|
|
|
* @return TypeInterface Type |
|
330
|
|
|
*/ |
|
331
|
13 |
|
protected function getType($prefixedType, ContextInterface $context) |
|
332
|
|
|
{ |
|
333
|
13 |
|
list($prefix, $typeName) = $this->getPrefixName($prefixedType); |
|
334
|
13 |
|
$vocabulary = $this->getVocabulary($prefix, $context); |
|
335
|
10 |
|
if ($vocabulary instanceof VocabularyInterface) { |
|
336
|
7 |
|
return new Type($typeName, $vocabulary); |
|
337
|
|
|
} |
|
338
|
|
|
|
|
339
|
|
|
// If the default vocabulary is empty |
|
340
|
3 |
|
if (empty($prefix)) { |
|
341
|
3 |
|
throw new RuntimeException( |
|
342
|
3 |
|
RuntimeException::EMPTY_DEFAULT_VOCABULARY_STR, |
|
343
|
3 |
|
RuntimeException::EMPTY_DEFAULT_VOCABULARY |
|
344
|
|
|
); |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
|
throw new OutOfBoundsException( |
|
348
|
|
|
sprintf(OutOfBoundsException::UNKNOWN_VOCABULARY_PREFIX_STR, $prefix), |
|
349
|
|
|
OutOfBoundsException::UNKNOWN_VOCABULARY_PREFIX |
|
350
|
|
|
); |
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
|
|
/** |
|
354
|
|
|
* Split a value into a vocabulary prefix and a name |
|
355
|
|
|
* |
|
356
|
|
|
* @param string $prefixName Prefixed name |
|
357
|
|
|
* @return array Prefix and name |
|
358
|
|
|
*/ |
|
359
|
|
|
abstract protected function getPrefixName($prefixName); |
|
360
|
|
|
} |
|
361
|
|
|
|