1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* Created by Vitaly Iegorov <[email protected]>. |
4
|
|
|
* on 05.04.17 at 11:43 |
5
|
|
|
*/ |
6
|
|
|
namespace samsonframework\stringconditiontree; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class IterableTreeNode |
10
|
|
|
* |
11
|
|
|
* @author Vitaly Egorov <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class IterableTreeNode implements \Iterator |
14
|
|
|
{ |
15
|
|
|
/** @var self[] Collection of tree node children */ |
16
|
|
|
public $children = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Return the current element |
20
|
|
|
* @link http://php.net/manual/en/iterator.current.php |
21
|
|
|
* @return mixed Can return any type. |
22
|
|
|
* @since 5.0.0 |
23
|
|
|
*/ |
24
|
|
|
public function current() |
25
|
|
|
{ |
26
|
|
|
return current($this->children); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Move forward to next element |
31
|
|
|
* @link http://php.net/manual/en/iterator.next.php |
32
|
|
|
* @return void Any returned value is ignored. |
33
|
|
|
* @since 5.0.0 |
34
|
|
|
*/ |
35
|
|
|
public function next() |
36
|
|
|
{ |
37
|
|
|
next($this->children); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Return the key of the current element |
42
|
|
|
* @link http://php.net/manual/en/iterator.key.php |
43
|
|
|
* @return mixed scalar on success, or null on failure. |
44
|
|
|
* @since 5.0.0 |
45
|
|
|
*/ |
46
|
|
|
public function key() |
47
|
|
|
{ |
48
|
|
|
return key($this->children); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Checks if current position is valid |
53
|
|
|
* @link http://php.net/manual/en/iterator.valid.php |
54
|
|
|
* @return boolean The return value will be casted to boolean and then evaluated. |
55
|
|
|
* Returns true on success or false on failure. |
56
|
|
|
* @since 5.0.0 |
57
|
|
|
*/ |
58
|
|
|
public function valid() |
59
|
|
|
{ |
60
|
|
|
$key = key($this->children); |
61
|
|
|
|
62
|
|
|
return ($key !== null && $key !== false); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Rewind the Iterator to the first element |
67
|
|
|
* @link http://php.net/manual/en/iterator.rewind.php |
68
|
|
|
* @return void Any returned value is ignored. |
69
|
|
|
* @since 5.0.0 |
70
|
|
|
*/ |
71
|
|
|
public function rewind() |
72
|
|
|
{ |
73
|
|
|
reset($this->children); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|