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\Parser\DOMIterator; |
41
|
|
|
use Jkphl\RdfaLiteMicrodata\Application\Parser\RootThing; |
42
|
|
|
use Jkphl\RdfaLiteMicrodata\Domain\Thing\ThingInterface; |
43
|
|
|
use Jkphl\RdfaLiteMicrodata\Domain\Vocabulary\VocabularyInterface; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Microdata element processor |
47
|
|
|
* |
48
|
|
|
* @package Jkphl\RdfaLiteMicrodata |
49
|
|
|
* @subpackage Jkphl\RdfaLiteMicrodata\Infrastructure |
50
|
|
|
*/ |
51
|
|
|
class MicrodataElementProcessor extends AbstractElementProcessor |
52
|
|
|
{ |
53
|
|
|
/** |
54
|
|
|
* Constructor |
55
|
|
|
*/ |
56
|
3 |
|
public function __construct() |
57
|
|
|
{ |
58
|
3 |
|
$this->setHtml(true); |
59
|
3 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Process a DOM element |
63
|
|
|
* |
64
|
|
|
* @param \DOMElement $element DOM element |
65
|
|
|
* @param ContextInterface $context Inherited Context |
66
|
|
|
* @return ContextInterface Local context for this element |
67
|
|
|
*/ |
68
|
3 |
|
public function processElement(\DOMElement $element, ContextInterface $context) |
69
|
|
|
{ |
70
|
|
|
// Create a property |
71
|
3 |
|
$propertyContext = $this->processProperty($element, $context); |
72
|
|
|
|
73
|
|
|
// Process the element in case it's an anonymous thing with no children |
74
|
3 |
|
if (!$element->childNodes->length) { |
75
|
3 |
|
$this->processChild($element, $context); |
76
|
|
|
} |
77
|
|
|
|
78
|
3 |
|
return $propertyContext; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Create a property |
83
|
|
|
* |
84
|
|
|
* @param \DOMElement $element DOM element |
85
|
|
|
* @param ContextInterface $context Inherited Context |
86
|
|
|
* @return ContextInterface Local context for this element |
87
|
|
|
*/ |
88
|
3 |
|
protected function processProperty(\DOMElement $element, ContextInterface $context) |
89
|
|
|
{ |
90
|
3 |
|
if ($element->hasAttribute('itemprop') && !($context->getParentThing() instanceof RootThing)) { |
91
|
3 |
|
$itemprops = trim($element->getAttribute('itemprop')); |
92
|
3 |
|
$itemprops = strlen($itemprops) ? preg_split('/\s+/', $itemprops) : []; |
93
|
3 |
|
$context = $this->processProperties($itemprops, $element, $context); |
94
|
|
|
} |
95
|
|
|
|
96
|
3 |
|
return $context; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Process properties |
101
|
|
|
* |
102
|
|
|
* @param array $itemprops Properties |
103
|
|
|
* @param \DOMElement $element DOM element |
104
|
|
|
* @param ContextInterface $context Inherited Context |
105
|
|
|
* @return ContextInterface Local context for this element |
106
|
|
|
*/ |
107
|
3 |
|
protected function processProperties(array $itemprops, \DOMElement $element, ContextInterface $context) |
108
|
|
|
{ |
109
|
3 |
|
foreach ($itemprops as $index => $itemprop) { |
110
|
3 |
|
$context = $this->processPropertyPrefixName( |
111
|
3 |
|
null, |
112
|
|
|
$itemprop, |
113
|
|
|
$element, |
114
|
|
|
$context, |
115
|
3 |
|
$index == (count($itemprops) - 1) |
116
|
|
|
); |
117
|
|
|
} |
118
|
|
|
|
119
|
2 |
|
return $context; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Create a nested child |
124
|
|
|
* |
125
|
|
|
* @param \DOMElement $element DOM element |
126
|
|
|
* @param ContextInterface $context Context |
127
|
|
|
* @return ContextInterface Context for children |
128
|
|
|
*/ |
129
|
3 |
|
protected function processChild(\DOMElement $element, ContextInterface $context) |
130
|
|
|
{ |
131
|
3 |
|
if ($element->hasAttribute('itemscope') && empty($element->getAttribute('itemprop'))) { |
132
|
3 |
|
$context = $this->createAndAddChild($element, $context); |
133
|
|
|
} |
134
|
|
|
|
135
|
3 |
|
return $context; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Create and add a nested child |
140
|
|
|
* |
141
|
|
|
* @param \DOMElement $element DOM element |
142
|
|
|
* @param ContextInterface $context Context |
143
|
|
|
* @return ContextInterface Context for children |
144
|
|
|
*/ |
145
|
3 |
|
protected function createAndAddChild(\DOMElement $element, ContextInterface $context) |
146
|
|
|
{ |
147
|
3 |
|
$thing = $this->getThing( |
148
|
3 |
|
trim($element->getAttribute('itemtype')) ?: null, |
149
|
3 |
|
trim($element->getAttribute('itemid')) ?: null, |
150
|
|
|
$context |
151
|
|
|
); |
152
|
|
|
|
153
|
|
|
// Process item references |
154
|
3 |
|
$this->processItemReferences($element, $context, $thing); |
155
|
|
|
|
156
|
|
|
// Add the new thing as a child to the current context |
157
|
|
|
// and set the thing as parent thing for nested iterations |
158
|
3 |
|
return $context->addChild($thing)->setParentThing($thing); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Process item references |
163
|
|
|
* |
164
|
|
|
* @param \DOMElement $element DOM element |
165
|
|
|
* @param ContextInterface $context Context |
166
|
|
|
* @param ThingInterface $thing Thing |
167
|
|
|
* @return ThingInterface Thing |
168
|
|
|
*/ |
169
|
3 |
|
protected function processItemReferences(\DOMElement $element, ContextInterface $context, ThingInterface $thing) |
170
|
|
|
{ |
171
|
|
|
// If the element has item references |
172
|
3 |
|
if ($element->hasAttribute('itemref')) { |
173
|
1 |
|
$itemrefElements = $this->getItemReferenceElements($element); |
174
|
1 |
|
if (count($itemrefElements)) { |
175
|
1 |
|
$this->processItemReferenceElements($itemrefElements, $context, $thing); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
3 |
|
return $thing; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Find all reference elements |
184
|
|
|
* |
185
|
|
|
* @param \DOMElement $element DOM element |
186
|
|
|
* @return \DOMElement[] Reference elements |
187
|
|
|
*/ |
188
|
1 |
|
protected function getItemReferenceElements(\DOMElement $element) |
189
|
|
|
{ |
190
|
1 |
|
$itemrefElements = []; |
191
|
1 |
|
$itemrefs = trim($element->getAttribute('itemref')); |
192
|
1 |
|
$itemrefs = strlen($itemrefs) ? preg_split('/\s+/', $itemrefs) : []; |
193
|
1 |
|
foreach ($itemrefs as $itemref) { |
194
|
1 |
|
$itemrefElement = $element->ownerDocument->getElementById($itemref); |
195
|
1 |
|
if ($itemrefElement instanceof \DOMElement) { |
196
|
1 |
|
$itemrefElements[] = $itemrefElement; |
197
|
|
|
} |
198
|
|
|
} |
199
|
1 |
|
return $itemrefElements; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Process item reference elements |
204
|
|
|
* |
205
|
|
|
* @param \DOMElement[] $itemrefElements Item reference DOM elements |
206
|
|
|
* @param ContextInterface $context Context |
207
|
|
|
* @param ThingInterface $thing Thing |
208
|
|
|
* @return void |
209
|
|
|
*/ |
210
|
1 |
|
protected function processItemReferenceElements( |
211
|
|
|
array $itemrefElements, |
212
|
|
|
ContextInterface $context, |
213
|
|
|
ThingInterface $thing |
214
|
|
|
) { |
215
|
1 |
|
$iterator = new DOMIterator( |
216
|
|
|
$itemrefElements, |
217
|
1 |
|
$context->setParentThing($thing), |
218
|
1 |
|
new MicrodataElementProcessor() |
219
|
|
|
); |
220
|
|
|
|
221
|
|
|
// Iterate through all $node |
222
|
1 |
|
foreach ($iterator->getRecursiveIterator() as $node) { |
223
|
1 |
|
$node || true; |
224
|
|
|
} |
225
|
1 |
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Return the resource ID |
229
|
|
|
* |
230
|
|
|
* @param \DOMElement $element DOM element |
231
|
|
|
* @return string|null Resource ID |
232
|
|
|
*/ |
233
|
3 |
|
protected function getResourceId(\DOMElement $element) |
234
|
|
|
{ |
235
|
3 |
|
return trim($element->getAttribute('itemid')) ?: null; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Return a property child value |
240
|
|
|
* |
241
|
|
|
* @param \DOMElement $element DOM element |
242
|
|
|
* @param ContextInterface $context Context |
243
|
|
|
* @return ThingInterface|null Property child value |
244
|
|
|
*/ |
245
|
3 |
|
protected function getPropertyChildValue(\DOMElement $element, ContextInterface $context) |
246
|
|
|
{ |
247
|
|
|
// If the property creates a new type: Return the element itself |
248
|
3 |
|
if ($element->hasAttribute('itemscope')) { |
249
|
2 |
|
$thing = $this->getThing($element->getAttribute('itemtype'), null, $context); |
250
|
2 |
|
return $this->processItemReferences($element, $context, $thing); |
251
|
|
|
} |
252
|
|
|
|
253
|
3 |
|
return null; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Return a vocabulary by prefix with fallback to the default vocabulary |
258
|
|
|
* |
259
|
|
|
* @param string $prefix Vocabulary prefix |
260
|
|
|
* @param ContextInterface $context Context |
261
|
|
|
* @return VocabularyInterface Vocabulary |
262
|
|
|
*/ |
263
|
3 |
|
protected function getVocabulary($prefix, ContextInterface $context) |
264
|
|
|
{ |
265
|
|
|
/** @var VocabularyInterface $vocabulary */ |
266
|
3 |
|
$vocabulary = $prefix ?: $context->getDefaultVocabulary(); |
267
|
3 |
|
return $vocabulary; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Split a value into a vocabulary prefix and a name |
272
|
|
|
* |
273
|
|
|
* @param string $prefixName Prefixed name |
274
|
|
|
* @return array Prefix and name |
275
|
|
|
*/ |
276
|
2 |
|
protected function getPrefixName($prefixName) |
277
|
|
|
{ |
278
|
2 |
|
return [null, $prefixName]; |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
|