|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bdf\Form\Aggregate\Collection; |
|
4
|
|
|
|
|
5
|
|
|
use ArrayIterator; |
|
6
|
|
|
use Iterator; |
|
7
|
|
|
use IteratorAggregate; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* The dependency tree level |
|
11
|
|
|
* |
|
12
|
|
|
* @internal |
|
13
|
|
|
*/ |
|
14
|
|
|
final class Level implements IteratorAggregate |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var int |
|
18
|
|
|
*/ |
|
19
|
|
|
private $number; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var Level|null |
|
23
|
|
|
*/ |
|
24
|
|
|
private $prev; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var Level|null |
|
28
|
|
|
*/ |
|
29
|
|
|
private $next; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var Level|null |
|
33
|
|
|
*/ |
|
34
|
|
|
private $last; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Array of elements dependencies |
|
38
|
|
|
* |
|
39
|
|
|
* @var string[][] |
|
40
|
|
|
*/ |
|
41
|
|
|
private $elements = []; |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Level constructor. |
|
46
|
|
|
* |
|
47
|
|
|
* @param Level|null $prev |
|
48
|
|
|
* @param int $number |
|
49
|
|
|
*/ |
|
50
|
33 |
|
public function __construct(?Level $prev = null, $number = 0) |
|
51
|
|
|
{ |
|
52
|
33 |
|
$this->prev = $prev; |
|
53
|
33 |
|
$this->number = $number; |
|
54
|
33 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Add dependencies to the level |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $name The element name |
|
60
|
|
|
* @param array $dependencies The element dependencies |
|
61
|
|
|
* |
|
62
|
|
|
* @return int[] Associative array, with element name as key, and element level as value |
|
63
|
|
|
*/ |
|
64
|
32 |
|
public function add($name, array $dependencies) |
|
65
|
|
|
{ |
|
66
|
|
|
$result = [ |
|
67
|
32 |
|
$name => $this->number |
|
68
|
|
|
]; |
|
69
|
|
|
|
|
70
|
32 |
|
$this->elements[$name] = $dependencies; |
|
71
|
|
|
|
|
72
|
32 |
|
foreach ($dependencies as $dependency) { |
|
73
|
23 |
|
$result = array_merge($result, $this->shift($dependency)); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
32 |
|
return $result; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Check if the level contains the element |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $element The element name |
|
83
|
|
|
* |
|
84
|
|
|
* @return bool |
|
85
|
|
|
*/ |
|
86
|
25 |
|
public function has($element) |
|
87
|
|
|
{ |
|
88
|
25 |
|
return isset($this->elements[$element]); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Move an element to the next level (the element becomes a dependency) |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $element The element name |
|
95
|
|
|
* |
|
96
|
|
|
* @return int[] The result of add() |
|
97
|
|
|
*/ |
|
98
|
24 |
|
public function shift($element) |
|
99
|
|
|
{ |
|
100
|
24 |
|
if ($this->next === null) { |
|
101
|
24 |
|
$this->next = new self($this, $this->number + 1); |
|
102
|
24 |
|
$this->last = $this->next; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
24 |
|
if ($this->has($element)) { |
|
106
|
16 |
|
$dependencies = $this->elements[$element]; |
|
107
|
16 |
|
unset($this->elements[$element]); |
|
108
|
|
|
} else { |
|
109
|
14 |
|
$dependencies = []; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
24 |
|
$result = $this->next->add($element, $dependencies); |
|
113
|
|
|
|
|
114
|
24 |
|
if ($this->next->last !== null) { |
|
115
|
4 |
|
$this->last = $this->next->last; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
24 |
|
return $result; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @return int |
|
123
|
|
|
*/ |
|
124
|
17 |
|
public function number() |
|
125
|
|
|
{ |
|
126
|
17 |
|
return $this->number; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Get the previous level (lvl n-1) |
|
131
|
|
|
* |
|
132
|
|
|
* @return Level|null |
|
133
|
|
|
*/ |
|
134
|
9 |
|
public function prev() |
|
135
|
|
|
{ |
|
136
|
9 |
|
return $this->prev; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Get the last level (queue of the list) |
|
141
|
|
|
* Can return NULL if the current level is the last element |
|
142
|
|
|
* |
|
143
|
|
|
* @return Level|null |
|
144
|
|
|
*/ |
|
145
|
24 |
|
public function last() |
|
146
|
|
|
{ |
|
147
|
24 |
|
return $this->last; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Get the next level (lvl n+1) |
|
152
|
|
|
* Can return NULL if the current level is the last element |
|
153
|
|
|
* |
|
154
|
|
|
* @return Level|null |
|
155
|
|
|
*/ |
|
156
|
12 |
|
public function next() |
|
157
|
|
|
{ |
|
158
|
12 |
|
return $this->next; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* {@inheritdoc} |
|
163
|
|
|
* |
|
164
|
|
|
* @return Iterator<string, string[]> |
|
165
|
|
|
*/ |
|
166
|
12 |
|
public function getIterator(): Iterator |
|
167
|
|
|
{ |
|
168
|
12 |
|
return new ArrayIterator($this->elements); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Reset the dependencies of the element |
|
173
|
|
|
* |
|
174
|
|
|
* @param string $name The element name |
|
175
|
|
|
*/ |
|
176
|
2 |
|
public function reset($name): void |
|
177
|
|
|
{ |
|
178
|
2 |
|
if ($this->has($name)) { |
|
179
|
2 |
|
$this->elements[$name] = []; |
|
180
|
|
|
} |
|
181
|
2 |
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Remove an element from the index |
|
185
|
|
|
* |
|
186
|
|
|
* @param string $name |
|
187
|
|
|
*/ |
|
188
|
3 |
|
public function remove($name): void |
|
189
|
|
|
{ |
|
190
|
3 |
|
unset($this->elements[$name]); |
|
191
|
3 |
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|