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 IterableTreeNode[] Collection of tree node children */ |
16
|
|
|
public $children = []; |
17
|
|
|
|
18
|
|
|
/** @var string Tree node identifier */ |
19
|
|
|
public $identifier; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Convert tree node to associative array. |
23
|
|
|
* |
24
|
|
|
* @return array Tree structure as hashed array |
25
|
|
|
*/ |
26
|
|
|
public function toArray(): array |
27
|
|
|
{ |
28
|
|
|
$result = []; |
29
|
|
|
|
30
|
|
|
// Render @self item for tests |
31
|
|
|
if ($this->identifier !== '') { |
32
|
|
|
$result[StringConditionTree::SELF_NAME] = $this->identifier; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string $key |
37
|
|
|
* @var IterableTreeNode $child |
38
|
|
|
*/ |
39
|
|
|
foreach ($this as $key => $child) { |
40
|
|
|
$result[$key] = $child->toArray(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return $result; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Return the current element |
48
|
|
|
* @link http://php.net/manual/en/iterator.current.php |
49
|
|
|
* @return mixed Can return any type. |
50
|
|
|
* @since 5.0.0 |
51
|
|
|
*/ |
52
|
|
|
public function current() |
53
|
|
|
{ |
54
|
|
|
return current($this->children); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Move forward to next element |
59
|
|
|
* @link http://php.net/manual/en/iterator.next.php |
60
|
|
|
* @return void Any returned value is ignored. |
61
|
|
|
* @since 5.0.0 |
62
|
|
|
*/ |
63
|
|
|
public function next() |
64
|
|
|
{ |
65
|
|
|
next($this->children); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Return the key of the current element |
70
|
|
|
* @link http://php.net/manual/en/iterator.key.php |
71
|
|
|
* @return mixed scalar on success, or null on failure. |
72
|
|
|
* @since 5.0.0 |
73
|
|
|
*/ |
74
|
|
|
public function key() |
75
|
|
|
{ |
76
|
|
|
return key($this->children); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Checks if current position is valid |
81
|
|
|
* @link http://php.net/manual/en/iterator.valid.php |
82
|
|
|
* @return boolean The return value will be casted to boolean and then evaluated. |
83
|
|
|
* Returns true on success or false on failure. |
84
|
|
|
* @since 5.0.0 |
85
|
|
|
*/ |
86
|
|
|
public function valid() |
87
|
|
|
{ |
88
|
|
|
$key = key($this->children); |
89
|
|
|
|
90
|
|
|
return ($key !== null && $key !== false); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Rewind the Iterator to the first element |
95
|
|
|
* @link http://php.net/manual/en/iterator.rewind.php |
96
|
|
|
* @return void Any returned value is ignored. |
97
|
|
|
* @since 5.0.0 |
98
|
|
|
*/ |
99
|
|
|
public function rewind() |
100
|
|
|
{ |
101
|
|
|
reset($this->children); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|