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 | * HTML mode |
||
81 | * |
||
82 | * @var boolean |
||
83 | */ |
||
84 | protected $html; |
||
85 | |||
86 | /** |
||
87 | * Element processor constructor |
||
88 | * |
||
89 | * @param bool $html HTML mode |
||
90 | */ |
||
91 | 13 | public function __construct($html = false) |
|
92 | { |
||
93 | 13 | $this->html = boolval($html); |
|
94 | 13 | } |
|
95 | |||
96 | /** |
||
97 | * Process a DOM element's child |
||
98 | * |
||
99 | * @param \DOMElement $element DOM element |
||
100 | * @param ContextInterface $context Context |
||
101 | * @return ContextInterface Context for children |
||
102 | */ |
||
103 | 13 | public function processElementChildren(\DOMElement $element, ContextInterface $context) |
|
104 | { |
||
105 | // Process a child |
||
106 | 13 | return $this->processChild($element, $context); |
|
107 | } |
||
108 | |||
109 | /** |
||
110 | * Create a nested child |
||
111 | * |
||
112 | * @param \DOMElement $element DOM element |
||
113 | * @param ContextInterface $context Context |
||
114 | * @return ContextInterface Context for children |
||
115 | */ |
||
116 | abstract protected function processChild(\DOMElement $element, ContextInterface $context); |
||
117 | |||
118 | /** |
||
119 | * Create a property |
||
120 | * |
||
121 | * @param \DOMElement $element DOM element |
||
122 | * @param ContextInterface $context Inherited Context |
||
123 | * @return ContextInterface Local context for this element |
||
124 | */ |
||
125 | abstract protected function processProperty(\DOMElement $element, ContextInterface $context); |
||
126 | |||
127 | /** |
||
128 | * Split a property into prefix and name |
||
129 | * |
||
130 | * @param string $property Prefixed property |
||
131 | * @return array Prefix and name |
||
132 | */ |
||
133 | 5 | protected function splitProperty($property) |
|
134 | { |
||
135 | 5 | $property = explode(':', $property); |
|
136 | 5 | $name = strval(array_pop($property)); |
|
137 | 5 | $prefix = strval(array_pop($property)); |
|
138 | 5 | return [$prefix, $name]; |
|
139 | } |
||
140 | |||
141 | /** |
||
142 | * Create a property by prefix and name |
||
143 | * |
||
144 | * @param string $prefix Property prefix |
||
145 | * @param string $name Property name |
||
146 | * @param \DOMElement $element DOM element |
||
147 | * @param ContextInterface $context Inherited Context |
||
148 | * @return ContextInterface Local context for this element |
||
149 | */ |
||
150 | 7 | protected function processPropertyPrefixName($prefix, $name, \DOMElement $element, ContextInterface $context) |
|
151 | { |
||
152 | 7 | $vocabulary = $this->getVocabulary($prefix, $context); |
|
153 | 7 | if ($vocabulary instanceof VocabularyInterface) { |
|
154 | 7 | $context = $this->addProperty($element, $context, $name, $vocabulary); |
|
155 | 6 | } |
|
156 | |||
157 | 6 | return $context; |
|
158 | } |
||
159 | |||
160 | /** |
||
161 | * Return a vocabulary by prefix with fallback to the default vocabulary |
||
162 | * |
||
163 | * @param string $prefix Vocabulary prefix |
||
164 | * @param ContextInterface $context Context |
||
165 | * @return VocabularyInterface Vocabulary |
||
166 | */ |
||
167 | abstract protected function getVocabulary($prefix, ContextInterface $context); |
||
168 | |||
169 | /** |
||
170 | * Add a single property |
||
171 | * |
||
172 | * @param \DOMElement $element DOM element |
||
173 | * @param ContextInterface $context Inherited Context |
||
174 | * @param string $name Property name |
||
175 | * @param VocabularyInterface $vocabulary Property vocabulary |
||
176 | * @return ContextInterface Local context for this element |
||
177 | */ |
||
178 | 7 | protected function addProperty( |
|
201 | |||
202 | /** |
||
203 | * Return the resource ID |
||
204 | * |
||
205 | * @param \DOMElement $element DOM element |
||
206 | * @return string|null Resource ID |
||
207 | */ |
||
208 | abstract protected function getResourceId(\DOMElement $element); |
||
209 | |||
210 | /** |
||
211 | * Return a property value (type and tag name dependent) |
||
212 | * |
||
213 | * @param \DOMElement $element DOM element |
||
214 | * @param ContextInterface $context Context |
||
215 | * @return ThingInterface|string Property value |
||
216 | */ |
||
217 | abstract protected function getPropertyValue(\DOMElement $element, ContextInterface $context); |
||
218 | |||
219 | /** |
||
220 | * Return a thing by typeof value |
||
221 | * |
||
222 | * @param string $typeof Thing type |
||
223 | * @param string $resourceId Resource ID |
||
224 | * @param ContextInterface $context Context |
||
225 | * @return Thing Thing |
||
226 | * @throws RuntimeException If the default vocabulary is empty |
||
227 | */ |
||
228 | 13 | protected function getThing($typeof, $resourceId, ContextInterface $context) |
|
254 | |||
255 | /** |
||
256 | * Return a property value (type and tag name dependent) |
||
257 | * |
||
258 | * @param \DOMElement $element DOM element |
||
259 | * @return ThingInterface|string Property value |
||
260 | */ |
||
261 | 7 | protected function getPropertyStringValue(\DOMElement $element) |
|
279 | } |
||
280 |