1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace DOMWrap\Collections; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Node List |
7
|
|
|
* |
8
|
|
|
* @package DOMWrap\Collections |
9
|
|
|
* @license http://opensource.org/licenses/BSD-3-Clause BSD 3 Clause |
10
|
|
|
*/ |
11
|
|
|
class NodeCollection implements \Countable, \ArrayAccess, \RecursiveIterator |
12
|
|
|
{ |
13
|
|
|
/** @var iterable */ |
14
|
|
|
protected $nodes = []; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param iterable $nodes |
18
|
|
|
*/ |
19
|
138 |
|
public function __construct(iterable $nodes = null) { |
20
|
138 |
|
if (!is_iterable($nodes)) { |
21
|
1 |
|
$nodes = []; |
22
|
|
|
} |
23
|
|
|
|
24
|
138 |
|
foreach ($nodes as $node) { |
|
|
|
|
25
|
134 |
|
$this->nodes[] = $node; |
26
|
|
|
} |
27
|
138 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @see \Countable::count() |
31
|
|
|
* |
32
|
|
|
* @return int |
33
|
|
|
*/ |
34
|
76 |
|
public function count(): int { |
35
|
76 |
|
return count($this->nodes); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @see \ArrayAccess::offsetExists() |
40
|
|
|
* |
41
|
|
|
* @param mixed $offset |
42
|
|
|
* |
43
|
|
|
* @return bool |
44
|
|
|
*/ |
45
|
|
|
public function offsetExists($offset): bool { |
46
|
|
|
return isset($this->nodes[$offset]); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @see \ArrayAccess::offsetGet() |
51
|
|
|
* |
52
|
|
|
* @param mixed $offset |
53
|
|
|
* |
54
|
|
|
* @return mixed |
55
|
|
|
*/ |
56
|
20 |
|
public function offsetGet($offset) { |
57
|
20 |
|
return isset($this->nodes[$offset]) ? $this->nodes[$offset] : null; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @see \ArrayAccess::offsetSet() |
62
|
|
|
* |
63
|
|
|
* @param mixed $offset |
64
|
|
|
* @param mixed $value |
65
|
|
|
*/ |
66
|
81 |
|
public function offsetSet($offset, $value): void { |
67
|
81 |
|
if (is_null($offset)) { |
68
|
81 |
|
$this->nodes[] = $value; |
69
|
|
|
} else { |
70
|
|
|
$this->nodes[$offset] = $value; |
71
|
|
|
} |
72
|
81 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @see \ArrayAccess::offsetUnset() |
76
|
|
|
* |
77
|
|
|
* @param mixed $offset |
78
|
|
|
*/ |
79
|
|
|
public function offsetUnset($offset): void { |
80
|
|
|
unset($this->nodes[$offset]); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @see \RecursiveIterator::RecursiveIteratorIterator() |
85
|
|
|
* |
86
|
|
|
* @return \RecursiveIteratorIterator |
87
|
|
|
*/ |
88
|
|
|
public function getRecursiveIterator(): \RecursiveIteratorIterator { |
89
|
|
|
return new \RecursiveIteratorIterator($this, \RecursiveIteratorIterator::SELF_FIRST); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @see \RecursiveIterator::getChildren() |
94
|
|
|
* |
95
|
|
|
* @return \RecursiveIterator |
96
|
|
|
*/ |
97
|
|
|
public function getChildren(): \RecursiveIterator { |
98
|
|
|
$nodes = []; |
99
|
|
|
|
100
|
|
|
if ($this->valid()) { |
101
|
|
|
$nodes = $this->current()->childNodes; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return new static($nodes); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @see \RecursiveIterator::hasChildren() |
109
|
|
|
* |
110
|
|
|
* @return bool |
111
|
|
|
*/ |
112
|
|
|
public function hasChildren(): bool { |
113
|
|
|
if ($this->valid()) { |
114
|
|
|
return $this->current()->hasChildNodes(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return false; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @see \RecursiveIterator::current() |
122
|
|
|
* @see \Iterator::current() |
123
|
|
|
* |
124
|
|
|
* @return mixed |
125
|
|
|
*/ |
126
|
130 |
|
public function current() { |
127
|
130 |
|
return current($this->nodes); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @see \RecursiveIterator::key() |
132
|
|
|
* @see \Iterator::key() |
133
|
|
|
* |
134
|
|
|
* @return mixed |
135
|
|
|
*/ |
136
|
1 |
|
public function key() { |
137
|
1 |
|
return key($this->nodes); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @see \RecursiveIterator::next() |
142
|
|
|
* @see \Iterator::next() |
143
|
|
|
* |
144
|
|
|
* @return mixed |
145
|
|
|
*/ |
146
|
130 |
|
public function next() { |
147
|
130 |
|
return next($this->nodes); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @see \RecursiveIterator::rewind() |
152
|
|
|
* @see \Iterator::rewind() |
153
|
|
|
* |
154
|
|
|
* @return mixed |
155
|
|
|
*/ |
156
|
133 |
|
public function rewind() { |
157
|
133 |
|
return reset($this->nodes); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @see \RecursiveIterator::valid() |
162
|
|
|
* @see \Iterator::valid() |
163
|
|
|
* |
164
|
|
|
* @return bool |
165
|
|
|
*/ |
166
|
131 |
|
public function valid(): bool { |
167
|
131 |
|
return key($this->nodes) !== null; |
168
|
|
|
} |
169
|
|
|
} |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.