1 | <?php declare(strict_types=1); |
||
2 | |||
3 | namespace DaveRandom\Jom; |
||
4 | |||
5 | use DaveRandom\Jom\Exceptions\InvalidKeyException; |
||
6 | use DaveRandom\Jom\Exceptions\InvalidReferenceNodeException; |
||
7 | use DaveRandom\Jom\Exceptions\InvalidSubjectNodeException; |
||
8 | use DaveRandom\Jom\Exceptions\WriteOperationForbiddenException; |
||
9 | |||
10 | abstract class VectorNode extends Node implements \Countable, \IteratorAggregate, \ArrayAccess |
||
11 | { |
||
12 | /** @var Node|null */ |
||
13 | protected $firstChild; |
||
14 | |||
15 | /** @var Node|null */ |
||
16 | protected $lastChild; |
||
17 | |||
18 | /** @var Node[] */ |
||
19 | protected $children = []; |
||
20 | |||
21 | /** @var int */ |
||
22 | protected $activeIteratorCount = 0; |
||
23 | |||
24 | /** |
||
25 | * @throws WriteOperationForbiddenException |
||
26 | */ |
||
27 | 163 | private function checkWritable(): void |
|
28 | { |
||
29 | 163 | if ($this->activeIteratorCount !== 0) { |
|
30 | 9 | throw new WriteOperationForbiddenException('Cannot modify a vector with an active iterator'); |
|
31 | } |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * @throws InvalidSubjectNodeException |
||
36 | */ |
||
37 | 153 | private function checkSubjectNodeIsOrphan(Node $node): void |
|
38 | { |
||
39 | 153 | if ($node->parent !== null) { |
|
40 | 16 | throw new InvalidSubjectNodeException('Node already present in the document'); |
|
41 | } |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * @throws InvalidSubjectNodeException |
||
46 | */ |
||
47 | 163 | private function checkSubjectNodeHasSameOwner(Node $node): void |
|
48 | { |
||
49 | 163 | if ($node->ownerDocument !== $this->ownerDocument) { |
|
50 | 21 | throw new InvalidSubjectNodeException('Node belongs to a different document'); |
|
51 | } |
||
52 | |||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @throws InvalidSubjectNodeException |
||
57 | */ |
||
58 | 20 | private function checkSubjectNodeIsChild(Node $node): void |
|
59 | { |
||
60 | 20 | if ($node->parent !== $this) { |
|
61 | throw new InvalidSubjectNodeException('Node not present in children of this node'); |
||
62 | } |
||
63 | |||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @throws InvalidReferenceNodeException |
||
68 | */ |
||
69 | 23 | private function checkReferenceNodeIsChild(Node $node): void |
|
70 | { |
||
71 | 23 | if ($node->parent !== $this) { |
|
72 | 2 | throw new InvalidReferenceNodeException('Reference node not present in children of this node'); |
|
73 | } |
||
74 | |||
75 | } |
||
76 | |||
77 | 151 | private function updateFirstChildIfChanged(?Node $newNode, ?Node $oldNode): void |
|
78 | { |
||
79 | 151 | if ($this->firstChild === $oldNode) { |
|
80 | 151 | $this->firstChild = $newNode; |
|
81 | } |
||
82 | } |
||
83 | |||
84 | 27 | private function updateLastChildIfChanged(?Node $newNode, ?Node $oldNode): void |
|
85 | { |
||
86 | 27 | if ($this->lastChild === $oldNode) { |
|
87 | 24 | $this->lastChild = $newNode; |
|
88 | } |
||
89 | } |
||
90 | |||
91 | 27 | private function setNodePreviousSibling(?Node $node, ?Node $newSiblingNode): void |
|
92 | { |
||
93 | 27 | if ($node !== null) { |
|
94 | 12 | $node->previousSibling = $newSiblingNode; |
|
95 | } |
||
96 | } |
||
97 | |||
98 | 151 | private function setNodeNextSibling(?Node $node, ?Node $newSiblingNode): void |
|
99 | { |
||
100 | 151 | if ($node !== null) { |
|
101 | 102 | $node->nextSibling = $newSiblingNode; |
|
102 | } |
||
103 | } |
||
104 | |||
105 | private function resetChildReferenceProperties(): void |
||
106 | { |
||
107 | $this->firstChild = null; |
||
108 | $this->lastChild = null; |
||
109 | $this->children = []; |
||
110 | $this->activeIteratorCount = 0; |
||
111 | } |
||
112 | |||
113 | private function appendClonedChildNodes(array $nodes): void |
||
114 | { |
||
115 | foreach ($nodes as $key => $node) { |
||
116 | try { |
||
117 | $this->appendNode(clone $node, $key); |
||
118 | //@codeCoverageIgnoreStart |
||
119 | } catch (\Exception $e) { |
||
120 | throw unexpected($e); |
||
121 | } |
||
122 | //@codeCoverageIgnoreEnd |
||
123 | } |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * @param Node|string|int $nodeOrKey |
||
128 | * @throws InvalidKeyException |
||
129 | */ |
||
130 | 21 | protected function resolveNode($nodeOrKey): Node |
|
131 | { |
||
132 | 21 | if ($nodeOrKey instanceof Node) { |
|
133 | 21 | return $nodeOrKey; |
|
134 | } |
||
135 | |||
136 | if (isset($this->children[$nodeOrKey])) { |
||
137 | return $this->children[$nodeOrKey]; |
||
138 | } |
||
139 | |||
140 | throw new InvalidKeyException("{$nodeOrKey} does not reference a valid child node"); |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * @param string|int $key |
||
145 | * @throws WriteOperationForbiddenException |
||
146 | * @throws InvalidSubjectNodeException |
||
147 | */ |
||
148 | 159 | protected function appendNode(Node $node, $key): Node |
|
149 | { |
||
150 | // Prevent modifying a collection with an active iterator |
||
151 | 159 | $this->checkWritable(); |
|
152 | |||
153 | // Validate arguments |
||
154 | 159 | $this->checkSubjectNodeHasSameOwner($node); |
|
155 | 151 | $this->checkSubjectNodeIsOrphan($node); |
|
156 | |||
157 | // Update first/last child pointers |
||
158 | 151 | $this->updateFirstChildIfChanged($node, null); |
|
159 | 151 | $previousSibling = $this->lastChild; |
|
160 | 151 | $this->lastChild = $node; |
|
161 | |||
162 | // Update next sibling pointer of old $lastChild (no next sibling node to update) |
||
163 | 151 | $this->setNodeNextSibling($previousSibling, $node); |
|
164 | |||
165 | // Add the child to the key map |
||
166 | 151 | $this->children[$key] = $node; |
|
167 | |||
168 | // Set references on new child |
||
169 | 151 | $node->setReferences($this, $key, $previousSibling, null); |
|
170 | |||
171 | 151 | return $node; |
|
172 | } |
||
173 | |||
174 | /** |
||
175 | * @param string|int $key |
||
176 | * @throws WriteOperationForbiddenException |
||
177 | * @throws InvalidSubjectNodeException |
||
178 | * @throws InvalidReferenceNodeException |
||
179 | */ |
||
180 | 34 | protected function insertNode(Node $node, $key, ?Node $before = null): Node |
|
181 | { |
||
182 | // A null $before reference means push the node on to the end of the list |
||
183 | 34 | if ($before === null) { |
|
184 | 13 | return $this->appendNode($node, $key); |
|
185 | } |
||
186 | |||
187 | // Prevent modifying a collection with an active iterator |
||
188 | 30 | $this->checkWritable(); |
|
189 | |||
190 | // Validate arguments |
||
191 | 27 | $this->checkSubjectNodeHasSameOwner($node); |
|
192 | 21 | $this->checkSubjectNodeIsOrphan($node); |
|
193 | 15 | $this->checkReferenceNodeIsChild($before); |
|
194 | |||
195 | // Update first child pointer (last child pointer is not affected) |
||
196 | 14 | $this->updateFirstChildIfChanged($node, $before); |
|
197 | |||
198 | // Update next sibling pointer of previous sibling of $before |
||
199 | 14 | $this->setNodeNextSibling($before->previousSibling, $node); |
|
200 | |||
201 | // Replace the child in the key map |
||
202 | 14 | $this->children[$key] = $node; |
|
203 | |||
204 | // Set references on new child |
||
205 | 14 | $node->setReferences($this, $key, $before->previousSibling, $before); |
|
206 | |||
207 | // Update references on ref child |
||
208 | 14 | $before->setReferences($before->parent, $before->key, $node, $before->nextSibling); |
|
209 | |||
210 | 14 | return $node; |
|
211 | } |
||
212 | |||
213 | /** |
||
214 | * @throws WriteOperationForbiddenException |
||
215 | * @throws InvalidSubjectNodeException |
||
216 | * @throws InvalidReferenceNodeException |
||
217 | */ |
||
218 | 21 | protected function replaceNode(Node $newNode, Node $oldNode): Node |
|
219 | { |
||
220 | // Prevent modifying a collection with an active iterator |
||
221 | 21 | $this->checkWritable(); |
|
222 | |||
223 | // Validate arguments |
||
224 | 19 | $this->checkSubjectNodeHasSameOwner($newNode); |
|
225 | 13 | $this->checkSubjectNodeIsOrphan($newNode); |
|
226 | 8 | $this->checkReferenceNodeIsChild($oldNode); |
|
227 | |||
228 | // Update first/last child pointers |
||
229 | 7 | $this->updateFirstChildIfChanged($newNode, $oldNode); |
|
230 | 7 | $this->updateLastChildIfChanged($newNode, $oldNode); |
|
231 | |||
232 | // Update sibling pointers of sibling nodes |
||
233 | 7 | $this->setNodeNextSibling($oldNode->previousSibling, $newNode); |
|
234 | 7 | $this->setNodePreviousSibling($oldNode->nextSibling, $newNode); |
|
235 | |||
236 | // Replace the node in the key map |
||
237 | 7 | $this->children[$oldNode->key] = $newNode; |
|
238 | |||
239 | // Copy references from old node to new node |
||
240 | 7 | $newNode->setReferences($oldNode->parent, $oldNode->key, $oldNode->previousSibling, $oldNode->nextSibling); |
|
241 | |||
242 | // Clear references from old node |
||
243 | 7 | $oldNode->setReferences(null, null, null, null); |
|
244 | |||
245 | 7 | return $oldNode; |
|
246 | } |
||
247 | |||
248 | /** |
||
249 | * @throws WriteOperationForbiddenException |
||
250 | * @throws InvalidSubjectNodeException |
||
251 | */ |
||
252 | 22 | protected function removeNode(Node $node): Node |
|
253 | { |
||
254 | // Prevent modifying a collection with an active iterator |
||
255 | 22 | $this->checkWritable(); |
|
256 | |||
257 | // Validate arguments |
||
258 | 20 | $this->checkSubjectNodeIsChild($node); |
|
259 | |||
260 | // Update first/last child pointers |
||
261 | 20 | $this->updateFirstChildIfChanged($node->nextSibling, $node); |
|
262 | 20 | $this->updateLastChildIfChanged($node->previousSibling, $node); |
|
263 | |||
264 | // Update sibling pointers of sibling nodes |
||
265 | 20 | $this->setNodeNextSibling($node->previousSibling, $node->nextSibling); |
|
266 | 20 | $this->setNodePreviousSibling($node->nextSibling, $node->previousSibling); |
|
267 | |||
268 | // Remove the node from the key map |
||
269 | 20 | unset($this->children[$node->key]); |
|
270 | |||
271 | // Clear references from node |
||
272 | 20 | $node->setReferences(null, null, null, null); |
|
273 | |||
274 | 20 | return $node; |
|
275 | } |
||
276 | |||
277 | final public function __clone() |
||
278 | { |
||
279 | parent::__clone(); |
||
280 | |||
281 | $originalParent = $this->firstChild !== null |
||
282 | ? $this->firstChild->parent |
||
283 | : null; |
||
284 | |||
285 | // Get an iterator for the original collection's child nodes |
||
286 | $children = $originalParent !== null |
||
287 | ? $originalParent->getIterator() |
||
288 | : []; |
||
289 | |||
290 | $this->resetChildReferenceProperties(); |
||
291 | $this->appendClonedChildNodes($children); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
292 | } |
||
293 | |||
294 | final public function hasChildren(): bool |
||
295 | { |
||
296 | return !empty($this->children); |
||
297 | } |
||
298 | |||
299 | 27 | public function containsChild(Node $child): bool |
|
300 | { |
||
301 | do { |
||
302 | 27 | if ($child->parent === $this) { |
|
303 | 27 | return true; |
|
304 | } |
||
305 | 21 | } while (null !== $child = $child->parent); |
|
306 | |||
307 | return false; |
||
308 | } |
||
309 | |||
310 | 18 | final public function getFirstChild(): ?Node |
|
311 | { |
||
312 | 18 | return $this->firstChild; |
|
313 | } |
||
314 | |||
315 | 12 | final public function getLastChild(): ?Node |
|
316 | { |
||
317 | 12 | return $this->lastChild; |
|
318 | } |
||
319 | |||
320 | final public function clear(): void |
||
321 | { |
||
322 | try { |
||
323 | while ($this->lastChild !== null) { |
||
324 | $this->removeNode($this->lastChild); |
||
325 | } |
||
326 | //@codeCoverageIgnoreStart |
||
327 | } catch (\Exception $e) { |
||
328 | throw unexpected($e); |
||
329 | } |
||
330 | //@codeCoverageIgnoreEnd |
||
331 | } |
||
332 | |||
333 | final public function getIterator(): NodeListIterator |
||
334 | { |
||
335 | 9 | return new NodeListIterator($this->firstChild, function($state) { |
|
336 | 9 | $this->activeIteratorCount += $state; |
|
337 | 9 | \assert($this->activeIteratorCount >= 0, new \Error('Vector node active iterator count is negative')); |
|
338 | 9 | }); |
|
339 | } |
||
340 | |||
341 | final public function count(): int |
||
342 | { |
||
343 | return \count($this->children); |
||
344 | } |
||
345 | |||
346 | final public function jsonSerialize(): array |
||
347 | { |
||
348 | return \iterator_to_array($this->getIterator()); |
||
349 | } |
||
350 | |||
351 | final public function offsetExists($key): bool |
||
352 | { |
||
353 | return isset($this->children[$key]); |
||
354 | } |
||
355 | |||
356 | /** |
||
357 | * @param string|int $key |
||
358 | * @throws WriteOperationForbiddenException |
||
359 | * @throws InvalidSubjectNodeException |
||
360 | */ |
||
361 | final public function offsetUnset($key): void |
||
362 | { |
||
363 | if (isset($this->children[$key])) { |
||
364 | $this->removeNode($this->children[$key]); |
||
365 | } |
||
366 | } |
||
367 | |||
368 | abstract public function toArray(): array; |
||
369 | abstract public function offsetGet($index): Node; |
||
370 | abstract public function offsetSet($index, $value): void; |
||
371 | } |
||
372 |