4
4
5
5
namespace drupol \phptree \Node ;
6
6
7
+ use drupol \phptree \Storage \NodeStorage ;
8
+ use drupol \phptree \Storage \StorageInterface ;
9
+
7
10
/**
8
11
* Class Node.
9
12
*/
@@ -12,9 +15,9 @@ class Node implements NodeInterface
12
15
/**
13
16
* The storage property.
14
17
*
15
- * @var array
18
+ * @var \drupol\phptree\Storage\NodeStorageInterface
16
19
*/
17
- protected $ storage ;
20
+ private $ storage ;
18
21
19
22
/**
20
23
* Node constructor.
@@ -23,10 +26,8 @@ class Node implements NodeInterface
23
26
*/
24
27
public function __construct (NodeInterface $ parent = null )
25
28
{
26
- $ this ->storage = [
27
- 'parent ' => $ parent ,
28
- 'children ' => new \ArrayObject (),
29
- ];
29
+ $ this ->storage = new NodeStorage ();
30
+ $ this ->storage ()->setParent ($ parent );
30
31
}
31
32
32
33
/**
@@ -35,7 +36,7 @@ public function __construct(NodeInterface $parent = null)
35
36
public function add (NodeInterface ...$ nodes ): NodeInterface
36
37
{
37
38
foreach ($ nodes as $ node ) {
38
- $ this ->storage [ ' children ' ] ->append (
39
+ $ this ->storage ()-> getChildren () ->append (
39
40
$ node ->setParent ($ this )
40
41
);
41
42
}
@@ -45,10 +46,12 @@ public function add(NodeInterface ...$nodes): NodeInterface
45
46
46
47
/**
47
48
* {@inheritdoc}
49
+ *
50
+ * @return \drupol\phptree\Node\NodeInterface|\Traversable
48
51
*/
49
52
public function children (): \Traversable
50
53
{
51
- yield from $ this ->storage [ ' children ' ] ;
54
+ yield from $ this ->storage ()-> getChildren () ;
52
55
}
53
56
54
57
/**
@@ -71,7 +74,7 @@ public function count(): int
71
74
*/
72
75
public function degree (): int
73
76
{
74
- return $ this ->storage [ ' children ' ] ->count ();
77
+ return $ this ->storage ()-> getChildren () ->count ();
75
78
}
76
79
77
80
/**
@@ -107,18 +110,18 @@ public function getIterator()
107
110
*/
108
111
public function getParent (): ?NodeInterface
109
112
{
110
- return $ this ->storage [ ' parent ' ] ;
113
+ return $ this ->storage ()-> getParent () ;
111
114
}
112
115
113
116
/**
114
117
* {@inheritdoc}
115
118
*/
116
119
public function getSibblings (): \Traversable
117
120
{
118
- $ parent = $ this ->storage [ ' parent ' ] ;
121
+ $ parent = $ this ->storage ()-> getParent () ;
119
122
120
123
if (null === $ parent ) {
121
- return new \ ArrayIterator ([]);
124
+ return $ this -> storage ()-> getChildren ()-> exchangeArray ([]);
122
125
}
123
126
124
127
foreach ($ parent ->children () as $ child ) {
@@ -149,31 +152,31 @@ public function height(): int
149
152
*/
150
153
public function isLeaf (): bool
151
154
{
152
- return 0 === $ this ->storage [ ' children ' ]-> count ();
155
+ return 0 === $ this ->degree ();
153
156
}
154
157
155
158
/**
156
159
* {@inheritdoc}
157
160
*/
158
161
public function isRoot (): bool
159
162
{
160
- return null === $ this ->storage [ ' parent ' ] ;
163
+ return null === $ this ->storage ()-> getParent () ;
161
164
}
162
165
163
166
/**
164
167
* {@inheritdoc}
165
168
*/
166
169
public function offsetExists ($ offset )
167
170
{
168
- return $ this ->storage [ ' children ' ] ->offsetExists ($ offset );
171
+ return $ this ->storage ()-> getChildren () ->offsetExists ($ offset );
169
172
}
170
173
171
174
/**
172
175
* {@inheritdoc}
173
176
*/
174
177
public function offsetGet ($ offset )
175
178
{
176
- return $ this ->storage [ ' children ' ] ->offsetGet ($ offset );
179
+ return $ this ->storage ()-> getChildren () ->offsetGet ($ offset );
177
180
}
178
181
179
182
/**
@@ -185,7 +188,7 @@ public function offsetSet($offset, $value)
185
188
throw new \InvalidArgumentException ('The value must implements NodeInterface. ' );
186
189
}
187
190
188
- $ this ->storage [ ' children ' ]
191
+ $ this ->storage ()-> getChildren ()
189
192
->offsetSet ($ offset , $ value ->setParent ($ this ));
190
193
}
191
194
@@ -194,17 +197,17 @@ public function offsetSet($offset, $value)
194
197
*/
195
198
public function offsetUnset ($ offset )
196
199
{
197
- $ this ->storage [ ' children ' ] ->offsetUnset ($ offset );
200
+ $ this ->storage ()-> getChildren () ->offsetUnset ($ offset );
198
201
}
199
202
200
203
/**
201
204
* {@inheritdoc}
202
205
*/
203
206
public function remove (NodeInterface ...$ nodes ): NodeInterface
204
207
{
205
- $ this ->storage [ ' children ' ] ->exchangeArray (
208
+ $ this ->storage ()-> getChildren () ->exchangeArray (
206
209
\array_filter (
207
- $ this ->storage [ ' children ' ] ->getArrayCopy (),
210
+ $ this ->storage ()-> getChildren () ->getArrayCopy (),
208
211
static function ($ child ) use ($ nodes ) {
209
212
return !\in_array ($ child , $ nodes , true );
210
213
}
@@ -219,7 +222,7 @@ static function ($child) use ($nodes) {
219
222
*/
220
223
public function setParent (NodeInterface $ node = null ): NodeInterface
221
224
{
222
- $ this ->storage [ ' parent ' ] = $ node ;
225
+ $ this ->storage ()-> setParent ( $ node) ;
223
226
224
227
return $ this ;
225
228
}
@@ -230,10 +233,20 @@ public function setParent(NodeInterface $node = null): NodeInterface
230
233
public function withChildren (NodeInterface ...$ nodes ): NodeInterface
231
234
{
232
235
$ clone = clone $ this ;
233
- $ clone ->storage [ ' children ' ] = new \ ArrayObject ( );
236
+ $ clone ->storage ()-> getChildren ()-> exchangeArray ([] );
234
237
235
238
return [] === $ nodes ?
236
239
$ clone :
237
240
$ clone ->add (...$ nodes );
238
241
}
242
+
243
+ /**
244
+ * {@inheritdoc}
245
+ *
246
+ * @return \drupol\phptree\Storage\NodeStorageInterface
247
+ */
248
+ protected function storage (): StorageInterface
249
+ {
250
+ return $ this ->storage ;
251
+ }
239
252
}
0 commit comments