95% of 810OPs |
96% of 161Lines |
91% of 171Branches |
18% of 222Paths |
# | |
---|---|
1 |
<?php |
2 |
|
3 |
namespace mageekguy\atoum\php\tokenizer; |
4 |
|
5 |
use |
6 |
mageekguy\atoum\exceptions, |
7 |
mageekguy\atoum\php\tokenizer\iterator |
8 |
; |
9 |
|
10 |
class iterator extends iterator\value |
11 |
{ |
12 |
protected $key = null; |
13 |
protected $size = 0; |
14 |
protected $values = array(); |
15 |
protected $skipedValues = array(); |
16 |
|
17 |
public function __toString()100% |
18 |
{ |
19 |
$key = $this->key(); |
20 |
|
21 |
$string = join('', iterator_to_array($this)); |
22 |
|
23 |
if ($key !== null) |
24 |
{ |
25 |
$this->seek($key); |
26 |
} |
27 |
|
28 |
return $string; |
29 |
} |
30 |
|
31 |
public function valid()100% |
32 |
{ |
33 |
return (current($this->values) !== false); |
34 |
} |
35 |
|
36 |
public function current()100% |
37 |
{ |
38 |
$value = null; |
39 |
|
40 |
if ($this->valid() === true) |
41 |
{ |
42 |
$value = current($this->values)->current(); |
43 |
} |
44 |
|
45 |
return $value; |
46 |
} |
47 |
|
48 |
public function key()100% |
49 |
{ |
50 |
return $this->key < 0 || $this->key >= $this->size ? null : $this->key; |
51 |
} |
52 |
|
53 |
public function prev($offset = 1)100% |
54 |
{ |
55 |
while (($valid = $this->valid()) === true && $offset > 0) |
56 |
{ |
57 |
$currentValue = current($this->values); |
58 |
|
59 |
$currentValue->prev(); |
60 |
|
61 |
while ($currentValue->valid() === false && $valid === true) |
62 |
{ |
63 |
prev($this->values); |
64 |
|
65 |
if (($valid = $this->valid()) === true) |
66 |
{ |
67 |
$currentValue = current($this->values); |
68 |
$currentValue->end(); |
69 |
} |
70 |
} |
71 |
|
72 |
if ($valid === true) |
73 |
{ |
74 |
while (in_array($this->current(), $this->skipedValues) === true && $this->valid() === true) |
75 |
{ |
76 |
$this->prev(); |
77 |
} |
78 |
} |
79 |
|
80 |
$this->key--; |
81 |
|
82 |
$offset--; |
83 |
} |
84 |
|
85 |
return $this; |
86 |
} |
87 |
|
88 |
public function next($offset = 1)100% |
89 |
{ |
90 |
while (($valid = $this->valid()) === true && $offset > 0) |
91 |
{ |
92 |
$currentValue = current($this->values); |
93 |
|
94 |
$currentValue->next(); |
95 |
|
96 |
while ($currentValue->valid() === false && $valid === true) |
97 |
{ |
98 |
next($this->values); |
99 |
|
100 |
if (($valid = $this->valid()) === true) |
101 |
{ |
102 |
$currentValue = current($this->values); |
103 |
$currentValue->rewind(); |
104 |
} |
105 |
} |
106 |
|
107 |
if ($valid === true) |
108 |
{ |
109 |
while (in_array($this->current(), $this->skipedValues) === true && $this->valid() === true) |
110 |
{ |
111 |
$this->next(); |
112 |
} |
113 |
} |
114 |
|
115 |
$this->key++; |
116 |
|
117 |
$offset--; |
118 |
} |
119 |
|
120 |
return $this; |
121 |
} |
122 |
|
123 |
public function rewind()71% |
124 |
{ |
125 |
if ($this->size > 0) |
126 |
{ |
127 |
reset($this->values); |
128 |
|
129 |
$currentValue = current($this->values); |
130 |
|
131 |
$valid = true; |
132 |
|
133 |
while ($currentValue->rewind()->valid() == false && $valid === true) |
134 |
{ |
135 |
next($this->values); |
136 |
|
137 |
if (($valid = $this->valid()) === true) |
138 |
{ |
139 |
$currentValue = current($this->values); |
140 |
} |
141 |
} |
142 |
|
143 |
$this->key = 0; |
144 |
|
145 |
if ($valid === true) |
146 |
{ |
147 |
while (in_array($this->current(), $this->skipedValues) === true && $this->valid() === true) |
148 |
{ |
149 |
$this->next(); |
150 |
} |
151 |
} |
152 |
} |
153 |
|
154 |
return $this; |
155 |
} |
156 |
|
157 |
public function end()100% |
158 |
{ |
159 |
if ($this->size > 0) |
160 |
{ |
161 |
end($this->values); |
162 |
|
163 |
$currentValue = current($this->values); |
164 |
|
165 |
$valid = true; |
166 |
|
167 |
while ($currentValue->end()->valid() == false && $valid === true) |
168 |
{ |
169 |
prev($this->values); |
170 |
|
171 |
if (($valid = $this->valid()) === true) |
172 |
{ |
173 |
$currentValue = current($this->values); |
174 |
} |
175 |
} |
176 |
|
177 |
$this->key = $this->size - 1; |
178 |
|
179 |
if ($valid === true) |
180 |
{ |
181 |
while (in_array($this->current(), $this->skipedValues) === true && $this->valid() === true) |
182 |
{ |
183 |
$this->prev(); |
184 |
} |
185 |
} |
186 |
} |
187 |
|
188 |
return $this; |
189 |
} |
190 |
|
191 |
public function append(iterator\value $value)100% |
192 |
{ |
193 |
if ($value->parent !== null) |
194 |
{ |
195 |
throw new exceptions\runtime('Unable to append value because it has already a parent'); |
196 |
} |
197 |
|
198 |
$value->parent = $this; |
199 |
|
200 |
$this->values[] = $value; |
201 |
|
202 |
if ($this->key === null) |
203 |
{ |
204 |
$this->key = 0; |
205 |
} |
206 |
|
207 |
$size = sizeof($value); |
208 |
|
209 |
if ($size > 0) |
210 |
{ |
211 |
$value->rewind(); |
212 |
|
213 |
$this->size += $size; |
214 |
|
215 |
$parent = $this->parent; |
216 |
|
217 |
while ($parent !== null) |
218 |
{ |
219 |
$parent->size += $size; |
220 |
|
221 |
$parent = $parent->parent; |
222 |
} |
223 |
} |
224 |
|
225 |
|
226 |
return $this; |
227 |
} |
228 |
|
229 |
public function count()100% |
230 |
{ |
231 |
return $this->size; |
232 |
} |
233 |
|
234 |
public function skipValue($value)100% |
235 |
{ |
236 |
if (in_array($value, $this->skipedValues) === false) |
237 |
{ |
238 |
$this->skipedValues[] = $value; |
239 |
} |
240 |
|
241 |
return $this; |
242 |
} |
243 |
|
244 |
public function getSkipedValues()100% |
245 |
{ |
246 |
return $this->skipedValues; |
247 |
} |
248 |
|
249 |
public function reset()100% |
250 |
{ |
251 |
$this->key = null; |
252 |
$this->size = 0; |
253 |
$this->values = array(); |
254 |
$this->parent = null; |
255 |
$this->skipedValues = array(); |
256 |
|
257 |
return $this; |
258 |
} |
259 |
|
260 |
public function getValue()100% |
261 |
{ |
262 |
return (current($this->values) ?: null); |
263 |
} |
264 |
|
265 |
public function seek($key)100% |
266 |
{ |
267 |
if ($key > sizeof($this) / 2) |
268 |
{ |
269 |
$this->end(); |
270 |
} |
271 |
else if ($this->valid() === false) |
272 |
{ |
273 |
$this->rewind(); |
274 |
} |
275 |
|
276 |
if ($key > $this->key) |
277 |
{ |
278 |
$this->next($key - $this->key); |
279 |
} |
280 |
else |
281 |
{ |
282 |
$this->prev($this->key - $key); |
283 |
} |
284 |
|
285 |
return $this; |
286 |
} |
287 |
|
288 |
public function findTag($tag)100% |
289 |
{ |
290 |
foreach ($this as $key => $token) |
291 |
{ |
292 |
if ($token->getTag() === $tag) |
293 |
{ |
294 |
return $key; |
295 |
} |
296 |
} |
297 |
|
298 |
return null; |
299 |
} |
300 |
|
301 |
public function goToNextTagWhichIsNot(array $tags)100% |
302 |
{ |
303 |
$this->next(); |
304 |
|
305 |
$token = $this->current(); |
306 |
|
307 |
while ($token !== null && in_array($token->getTag(), $tags) === true) |
308 |
{ |
309 |
$this->next(); |
310 |
|
311 |
$token = $this->current(); |
312 |
} |
313 |
|
314 |
return $this; |
315 |
} |
316 |
} |