-
Notifications
You must be signed in to change notification settings - Fork 0
/
NodePrototype.php
310 lines (211 loc) · 5.44 KB
/
NodePrototype.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<?php
namespace Pronode;
abstract class NodePrototype {
/**
* Wrapped Object / Array / Scalar
*
* @var mixed
*/
public $data;
/**
* Parent Node
* eg. after "article,12.title" (article,12) is parent of (title)
*
* @var Node|NULL
*/
public ?Node $parent = null;
/**
* Array of child Nodes
* (title) is child of (article,12)
* Also, its the first child if the first executed branch was "article,12.title"
* Children are used for caching the results of branch executing.
*
* @var Node[]
*/
public array $children = [];
/**
* Resource name in the context of parent Node.
*
* Assuming that Parent node holds an User object, then creating a branch user.age causes
* a creation of Child Node (int age) and the resource name of that Node is "age".
*
* @var string
*/
public string $resourceName;
/**
* Checks whether given child exists among ->children[] and is not null
*
* @param $resourceName
*
* @return bool
*/
public function childExists(string $resourceName) : bool {
return isset($this->children[$resourceName]); // NULL-sensitive
}
/**
* Returns an array of every Node from Root to current one.
* First element is always Root.
*
* @return Node[]
*/
public function getPath() : array {
$path = [];
$node = $this;
do {
$path[] = $node;
} while ($node = $node->parent);
return $path;
}
/**
* Returns an array of all branches of the Tree
*
* @return Node[][]
*/
public function getBranches() : array {
$results = [];
$leaves = $this->getLeaves();
foreach ($leaves as $leaf) {
$results[] = $leaf->getBranch();
}
return $results;
}
/**
* Get descendant Node with given Command or NULL if descendant doesn't exists.
*
* @param Command $command
* @return Node|NULL Descendant Node
*/
public function getDescendant(string $path) : ?Node {
$command = new Command($path);
$node = $this;
do {
if (!$node->childExists($command->resourceName)) return null;
$node = $node->children[$command->resourceName];
$command = $command->next();
} while ($node && $command);
return $node;
}
/**
* Returns an array of all descendant Nodes of current Node
*
* @return Node[]
*/
public function getDescendants() : array {
$results = [];
$children = $this->children;
foreach ($children as $child) {
$results[] = $child;
$results = array_merge($results, $child->getDescendants());
}
return $results;
}
/**
* Returns an array of all child-free nodes in a tree starting from current Node.
*
* @return Node[]
*/
public function getLeaves() : array {
$results = [];
foreach ($this->children as $child) {
if ($child->isLeaf()) {
$results[] = $child;
} else {
$results = array_merge($results, $child->getLeaves());
}
}
return $results;
}
/**
* Returns an array of all child-free children of current Node.
*
* @return Node[]
*/
public function getLeavesOwn() : array {
$results = [];
foreach ($this->children as $child) {
if ($child->isLeaf()) {
$results[] = $child;
}
}
return $results;
}
/**
* Gets child of given $resourceName from ->children array.
* Child must exist - otherwise undefined index error will occur.
*/
protected function getChild($resourceName) : Node {
return $this->children[$resourceName];
}
/**
* Finds and returns the final parent of the current Node.
*/
public function root() : Node {
if ($this->isRoot()) return $this;
return $this->parent->root();
}
/**
* Checks whether the Node is also the Root
*/
protected function isRoot() : bool {
return !isset($this->parent);
}
/**
* Check whether the Node is leaf at the moment of checking.
* Leaf is a Node with no children.
*/
protected function isLeaf() : bool {
return empty($this->children);
}
/**
* Selects all descendant Nodes which match given types.
* Ex. ['View', 'string'] returns all Nodes of data-type 'Node' or 'string'.
*
* Note that supported types are:
* boolean|integer|double|string|array|resource|NULL|object|__CLASS__
*
* Use 'scalar' to select 'integer', 'string', 'float' and 'bool';
* Use 'Namespace/Class' to select objects of given class.
*
* @param string[] $types
*/
public function select(array $types = []) {
$descendants = $this->getDescendants();
if (empty($types) || in_array('*', $types)) {
return $descendants;
}
$results = [];
if (in_array('scalar', $types)) {
$types = array_merge($types, ['integer', 'float', 'bool', 'string', 'null']);
}
foreach ($descendants as $node) {
$type = get_type($node->data);
if (in_array($type, $types)) {
$results[] = $node;
continue;
}
if (is_object($node->data) && in_array('object', $types)) {
$results[] = $node;
}
}
return $results;
}
/**
* Adopts given node by setting parent and child relation.
*
* @param Node $node Node to addopt
* @param string $resourceName
* @return bool
*/
protected function adopt(Node &$node, string $resourceName) : bool {
$node->parent = $this;
$this->children[$resourceName] = $node;
return true;
}
/**
* Destroys current Node.
*/
public function destroy() : void {
$this->data = null;
unset($this->parent->children[$this->resourceName]);
}
}