|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Vine; |
|
4
|
|
|
|
|
5
|
|
|
use Vine\Commands\Shake; |
|
6
|
|
|
use Vine\Commands\Flatten; |
|
7
|
|
|
use Vine\Commands\Inflate; |
|
8
|
|
|
use Vine\Commands\Remove; |
|
9
|
|
|
use Vine\Commands\Slice; |
|
10
|
|
|
use Vine\Queries\Find; |
|
11
|
|
|
|
|
12
|
|
|
class NodeCollection implements \ArrayAccess, \Countable, \IteratorAggregate |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var Node[] |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $nodes; |
|
18
|
|
|
|
|
19
|
174 |
|
public function __construct(Node ...$nodeCollection) |
|
20
|
|
|
{ |
|
21
|
174 |
|
$this->nodes = $nodeCollection; |
|
22
|
174 |
|
} |
|
23
|
|
|
|
|
24
|
138 |
|
public function all() |
|
25
|
|
|
{ |
|
26
|
138 |
|
return $this->nodes; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
45 |
|
public function first() |
|
30
|
|
|
{ |
|
31
|
45 |
|
if($this->isEmpty()) return null; |
|
32
|
|
|
|
|
33
|
45 |
|
return reset($this->nodes); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
90 |
|
public function isEmpty(): bool |
|
37
|
|
|
{ |
|
38
|
90 |
|
return empty($this->nodes); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Add one / many nodes to this collection |
|
43
|
|
|
* |
|
44
|
|
|
* @param Node[] ...$nodes |
|
45
|
|
|
* @return $this |
|
46
|
|
|
*/ |
|
47
|
66 |
|
public function add(Node ...$nodes) |
|
48
|
|
|
{ |
|
49
|
66 |
|
$this->nodes = array_merge($this->nodes, $nodes); |
|
|
|
|
|
|
50
|
|
|
|
|
51
|
66 |
|
return $this; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Merge a collection into current one. |
|
56
|
|
|
* |
|
57
|
|
|
* @param NodeCollection $nodeCollection |
|
58
|
|
|
* @return $this |
|
59
|
|
|
*/ |
|
60
|
126 |
|
public function merge(self $nodeCollection) |
|
61
|
|
|
{ |
|
62
|
126 |
|
$this->nodes = array_merge($this->nodes, $nodeCollection->all()); |
|
63
|
|
|
|
|
64
|
126 |
|
return $this; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Remove the child node |
|
69
|
|
|
* |
|
70
|
|
|
* Please note that all descendants of this |
|
71
|
|
|
* child node will be removed as well. |
|
72
|
|
|
* |
|
73
|
|
|
* @param Node $child |
|
74
|
|
|
* @return $this |
|
75
|
|
|
*/ |
|
76
|
36 |
|
public function remove(Node $child) |
|
77
|
|
|
{ |
|
78
|
36 |
|
return (new Remove())($this,$child); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Return flattened list of all nodes in this collection |
|
83
|
|
|
* |
|
84
|
|
|
* @return NodeCollection |
|
85
|
|
|
*/ |
|
86
|
6 |
|
public function flatten() |
|
87
|
|
|
{ |
|
88
|
6 |
|
return (new Flatten())($this); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Inflate a flattened collection back to its original structure |
|
93
|
|
|
* |
|
94
|
|
|
* @return NodeCollection |
|
95
|
|
|
*/ |
|
96
|
6 |
|
public function inflate() |
|
97
|
|
|
{ |
|
98
|
6 |
|
return (new Inflate())($this); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Slice one or more nodes out of the collection |
|
103
|
|
|
* |
|
104
|
|
|
* @param Node[] ...$nodes |
|
105
|
|
|
* @return mixed |
|
106
|
|
|
*/ |
|
107
|
3 |
|
public function slice(Node ...$nodes) |
|
108
|
|
|
{ |
|
109
|
3 |
|
return (new Slice())($this, ...$nodes); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Find many nodes by attribute value |
|
114
|
|
|
* |
|
115
|
|
|
* @param $key |
|
116
|
|
|
* @param array $values |
|
117
|
|
|
* @return NodeCollection |
|
118
|
|
|
*/ |
|
119
|
3 |
|
public function findMany($key, array $values): self |
|
120
|
|
|
{ |
|
121
|
3 |
|
return (new Find)($this,$key,$values); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Find specific node by attribute value |
|
126
|
|
|
* |
|
127
|
|
|
* @param $key |
|
128
|
|
|
* @param $value |
|
129
|
|
|
* @return Node|null |
|
130
|
|
|
*/ |
|
131
|
9 |
|
public function find($key, $value) |
|
132
|
|
|
{ |
|
133
|
9 |
|
return (new Find)($this,$key,[$value])->first(); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
21 |
|
public function offsetExists($offset) |
|
137
|
|
|
{ |
|
138
|
21 |
|
if(!is_string($offset) && !is_int($offset)) return false; |
|
139
|
|
|
|
|
140
|
21 |
|
return array_key_exists($offset, $this->nodes); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
33 |
|
public function offsetGet($offset) |
|
144
|
|
|
{ |
|
145
|
33 |
|
return $this->nodes[$offset]; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
21 |
|
public function offsetSet($offset, $value) |
|
149
|
|
|
{ |
|
150
|
21 |
|
if(is_null($offset)) $this->nodes[] = $value; |
|
151
|
|
|
|
|
152
|
21 |
|
else $this->nodes[$offset] = $value; |
|
153
|
21 |
|
} |
|
154
|
|
|
|
|
155
|
39 |
|
public function offsetUnset($offset) |
|
156
|
|
|
{ |
|
157
|
39 |
|
unset($this->nodes[$offset]); |
|
158
|
39 |
|
} |
|
159
|
|
|
|
|
160
|
66 |
|
public function count() |
|
161
|
|
|
{ |
|
162
|
66 |
|
return count($this->nodes); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
90 |
|
public function getIterator() |
|
166
|
|
|
{ |
|
167
|
90 |
|
return new \ArrayIterator($this->nodes); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..