1 | <?php |
||
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) |
|
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) |
|
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( |
|
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( |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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 |