1 | <?php |
||
52 | class RdfaLiteElementProcessor extends AbstractElementProcessor |
||
53 | { |
||
54 | /** |
||
55 | * Process a DOM element |
||
56 | * |
||
57 | * @param \DOMElement $element DOM element |
||
58 | * @param ContextInterface $context Inherited Context |
||
59 | * @return ContextInterface Local context for this element |
||
60 | */ |
||
61 | 13 | public function processElement(\DOMElement $element, ContextInterface $context) |
|
72 | |||
73 | /** |
||
74 | * Process changes of the default vocabulary |
||
75 | * |
||
76 | * @param \DOMElement $element DOM element |
||
77 | * @param ContextInterface $context Inherited Context |
||
78 | * @return ContextInterface Local context for this element |
||
79 | */ |
||
80 | 13 | protected function processVocab(\DOMElement $element, ContextInterface $context) |
|
89 | |||
90 | /** |
||
91 | * Process vocabulary prefixes |
||
92 | * |
||
93 | * @param \DOMElement $element DOM element |
||
94 | * @param ContextInterface $context Inherited Context |
||
95 | * @return ContextInterface Local context for this element |
||
96 | */ |
||
97 | 13 | protected function processPrefix(\DOMElement $element, ContextInterface $context) |
|
110 | |||
111 | /** |
||
112 | * Create a property |
||
113 | * |
||
114 | * @param \DOMElement $element DOM element |
||
115 | * @param ContextInterface $context Inherited Context |
||
116 | * @return ContextInterface Local context for this element |
||
117 | */ |
||
118 | 13 | protected function processProperty(\DOMElement $element, ContextInterface $context) |
|
119 | { |
||
120 | 13 | if ($element->hasAttribute('property') && !($context->getParentThing() instanceof RootThing)) { |
|
121 | 7 | $properties = preg_split('/\s+/', $element->getAttribute('property')); |
|
122 | 7 | foreach ($properties as $index => $property) { |
|
123 | 7 | list($prefix, $name) = $this->getPrefixName($property); |
|
124 | 7 | $context = $this->processPropertyPrefixName( |
|
125 | $prefix, |
||
126 | $name, |
||
127 | $element, |
||
128 | $context, |
||
129 | 7 | $index == (count($properties) - 1) |
|
130 | ); |
||
131 | } |
||
132 | } |
||
133 | |||
134 | 13 | return $context; |
|
135 | } |
||
136 | |||
137 | /** |
||
138 | * Split a value into a vocabulary prefix and a name |
||
139 | * |
||
140 | * @param string $prefixName Prefixed name |
||
141 | * @return array Prefix and name |
||
142 | */ |
||
143 | 13 | protected function getPrefixName($prefixName) |
|
150 | |||
151 | /** |
||
152 | * Create a nested child |
||
153 | * |
||
154 | * @param \DOMElement $element DOM element |
||
155 | * @param ContextInterface $context Context |
||
156 | * @return ContextInterface Context for children |
||
157 | */ |
||
158 | 13 | protected function processChild(\DOMElement $element, ContextInterface $context) |
|
168 | |||
169 | /** |
||
170 | * Create and add a nested child |
||
171 | * |
||
172 | * @param \DOMElement $element DOM element |
||
173 | * @param ContextInterface $context Context |
||
174 | * @return ContextInterface Context for children |
||
175 | */ |
||
176 | 13 | protected function createAndAddChild(\DOMElement $element, ContextInterface $context) |
|
177 | { |
||
178 | 13 | $thing = $this->getThing( |
|
179 | 13 | $element->getAttribute('typeof'), |
|
180 | 13 | trim($element->getAttribute('resource')) ?: null, |
|
181 | 13 | $context |
|
182 | ); |
||
183 | |||
184 | // Add the new thing as a child to the current context |
||
185 | // and set the thing as parent thing for nested iterations |
||
186 | 7 | return $context->addChild($thing)->setParentThing($thing); |
|
187 | } |
||
188 | |||
189 | |||
190 | /** |
||
191 | * Return a property child value |
||
192 | * |
||
193 | * @param \DOMElement $element DOM element |
||
194 | * @param ContextInterface $context Context |
||
195 | * @return ThingInterface|null Property child value |
||
196 | */ |
||
197 | 7 | protected function getPropertyChildValue(\DOMElement $element, ContextInterface $context) |
|
198 | { |
||
199 | // If the property creates a new thing |
||
200 | 7 | if ($element->hasAttribute('typeof')) { |
|
201 | 4 | return $this->getThing( |
|
202 | 4 | $element->getAttribute('typeof'), |
|
203 | 4 | trim($element->getAttribute('resource')) ?: null, |
|
204 | 4 | $context |
|
205 | ); |
||
206 | } |
||
207 | |||
208 | 7 | return null; |
|
209 | } |
||
210 | |||
211 | /** |
||
212 | * Return the resource ID |
||
213 | * |
||
214 | * @param \DOMElement $element DOM element |
||
215 | * @return string|null Resource ID |
||
216 | */ |
||
217 | 7 | protected function getResourceId(\DOMElement $element) |
|
221 | |||
222 | /** |
||
223 | * Return a vocabulary by prefix with fallback to the default vocabulary |
||
224 | * |
||
225 | * @param string $prefix Vocabulary prefix |
||
226 | * @param ContextInterface $context Context |
||
227 | * @return VocabularyInterface Vocabulary |
||
228 | */ |
||
229 | 13 | protected function getVocabulary($prefix, ContextInterface $context) |
|
234 | } |
||
235 |