1 | <?php |
||
55 | abstract class AbstractElementProcessor implements ElementProcessorInterface |
||
56 | { |
||
57 | /** |
||
58 | * Tag name / attribute map |
||
59 | * |
||
60 | * @var array |
||
61 | */ |
||
62 | protected static $tagNameAttributes = [ |
||
63 | 'META' => 'content', |
||
64 | 'AUDIO' => 'src', |
||
65 | 'EMBED' => 'src', |
||
66 | 'IFRAME' => 'src', |
||
67 | 'IMG' => 'src', |
||
68 | 'SOURCE' => 'src', |
||
69 | 'TRACK' => 'src', |
||
70 | 'VIDEO' => 'src', |
||
71 | 'A' => 'href', |
||
72 | 'AREA' => 'href', |
||
73 | 'LINK' => 'href', |
||
74 | 'OBJECT' => 'data', |
||
75 | 'DATA' => 'value', |
||
76 | 'METER' => 'value', |
||
77 | 'TIME' => 'datetime' |
||
78 | ]; |
||
79 | /** |
||
80 | * Property cache |
||
81 | * |
||
82 | * @var array |
||
83 | */ |
||
84 | protected static $propertyCache = []; |
||
85 | /** |
||
86 | * HTML mode |
||
87 | * |
||
88 | * @var boolean |
||
89 | */ |
||
90 | protected $html; |
||
91 | |||
92 | /** |
||
93 | * Enable / disable HTML element value resolution |
||
94 | * |
||
95 | * @param bool $html Enable HTML element value resolution |
||
96 | * @return ElementProcessorInterface Self reference |
||
97 | */ |
||
98 | 11 | public function setHtml($html) |
|
103 | |||
104 | /** |
||
105 | * Process a DOM element's child |
||
106 | * |
||
107 | * @param \DOMElement $element DOM element |
||
108 | * @param ContextInterface $context Context |
||
109 | * @return ContextInterface Context for children |
||
110 | */ |
||
111 | 14 | public function processElementChildren(\DOMElement $element, ContextInterface $context) |
|
116 | |||
117 | /** |
||
118 | * Create a nested child |
||
119 | * |
||
120 | * @param \DOMElement $element DOM element |
||
121 | * @param ContextInterface $context Context |
||
122 | * @return ContextInterface Context for children |
||
123 | */ |
||
124 | abstract protected function processChild(\DOMElement $element, ContextInterface $context); |
||
125 | |||
126 | /** |
||
127 | * Create a property |
||
128 | * |
||
129 | * @param \DOMElement $element DOM element |
||
130 | * @param ContextInterface $context Inherited Context |
||
131 | * @return ContextInterface Local context for this element |
||
132 | */ |
||
133 | abstract protected function processProperty(\DOMElement $element, ContextInterface $context); |
||
134 | |||
135 | /** |
||
136 | * Create a property by prefix and name |
||
137 | * |
||
138 | * @param string $prefix Property prefix |
||
139 | * @param string $name Property name |
||
140 | * @param \DOMElement $element DOM element |
||
141 | * @param ContextInterface $context Inherited Context |
||
142 | * @param boolean $last Last property |
||
143 | * @return ContextInterface Local context for this element |
||
144 | */ |
||
145 | 8 | protected function processPropertyPrefixName($prefix, $name, \DOMElement $element, ContextInterface $context, $last) |
|
154 | |||
155 | /** |
||
156 | * Return a vocabulary by prefix with fallback to the default vocabulary |
||
157 | * |
||
158 | * @param string $prefix Vocabulary prefix |
||
159 | * @param ContextInterface $context Context |
||
160 | * @return VocabularyInterface Vocabulary |
||
161 | */ |
||
162 | abstract protected function getVocabulary($prefix, ContextInterface $context); |
||
163 | |||
164 | /** |
||
165 | * Add a single property |
||
166 | * |
||
167 | * @param \DOMElement $element DOM element |
||
168 | * @param ContextInterface $context Inherited Context |
||
169 | * @param string $name Property name |
||
170 | * @param VocabularyInterface $vocabulary Property vocabulary |
||
171 | * @param boolean $last Last property |
||
172 | * @return ContextInterface Local context for this element |
||
173 | */ |
||
174 | 8 | protected function addProperty( |
|
198 | |||
199 | /** |
||
200 | * Return the resource ID |
||
201 | * |
||
202 | * @param \DOMElement $element DOM element |
||
203 | * @return string|null Resource ID |
||
204 | */ |
||
205 | abstract protected function getResourceId(\DOMElement $element); |
||
206 | |||
207 | /** |
||
208 | * Return a property value (type and tag name dependent) |
||
209 | * |
||
210 | * @param \DOMElement $element DOM element |
||
211 | * @param ContextInterface $context Context |
||
212 | * @return ThingInterface|string Property value |
||
213 | */ |
||
214 | 8 | protected function getPropertyValue(\DOMElement $element, ContextInterface $context) |
|
229 | |||
230 | /** |
||
231 | * Return a cached property value (if available) |
||
232 | * |
||
233 | * @param \DOMElement $element Element |
||
234 | * @return mixed|null Property value |
||
235 | */ |
||
236 | 8 | protected function getPropertyCache(\DOMElement $element) |
|
241 | |||
242 | /** |
||
243 | * Return a property child value |
||
244 | * |
||
245 | * @param \DOMElement $element DOM element |
||
246 | * @param ContextInterface $context Context |
||
247 | * @return ThingInterface|null Property child value |
||
248 | */ |
||
249 | abstract protected function getPropertyChildValue(\DOMElement $element, ContextInterface $context); |
||
250 | |||
251 | /** |
||
252 | * Cache a property value |
||
253 | * |
||
254 | * @param \DOMElement $element DOM element |
||
255 | * @param mixed $value Value |
||
256 | * @return mixed $value Value |
||
257 | */ |
||
258 | 8 | protected function setPropertyCache(\DOMElement $element, $value) |
|
262 | |||
263 | /** |
||
264 | * Return a property value (type and tag name dependent) |
||
265 | * |
||
266 | * @param \DOMElement $element DOM element |
||
267 | * @return ThingInterface|string Property value |
||
268 | */ |
||
269 | 8 | protected function getPropertyStringValue(\DOMElement $element) |
|
287 | |||
288 | /** |
||
289 | * Return a thing by typeof value |
||
290 | * |
||
291 | * @param string|null $typeof Thing type |
||
292 | * @param string|null $resourceId Resource ID |
||
293 | * @param ContextInterface $context Context |
||
294 | * @return Thing Thing |
||
295 | * @throws RuntimeException If the default vocabulary is empty |
||
296 | */ |
||
297 | 14 | protected function getThing($typeof, $resourceId, ContextInterface $context) |
|
309 | |||
310 | /** |
||
311 | * Instanciate a type |
||
312 | * |
||
313 | * @param string $prefixedType Prefixed type |
||
314 | * @param ContextInterface $context Context |
||
315 | * @return TypeInterface Type |
||
316 | */ |
||
317 | 13 | protected function getType($prefixedType, ContextInterface $context) |
|
331 | |||
332 | /** |
||
333 | * Split a value into a vocabulary prefix and a name |
||
334 | * |
||
335 | * @param string $prefixName Prefixed name |
||
336 | * @return array Prefix and name |
||
337 | */ |
||
338 | abstract protected function getPrefixName($prefixName); |
||
339 | } |
||
340 |