File tree 3 files changed +44
-0
lines changed
3 files changed +44
-0
lines changed Original file line number Diff line number Diff line change @@ -202,4 +202,32 @@ public function it_can_use_withChildren()
202
202
->children ()
203
203
->shouldReturn ([]);
204
204
}
205
+
206
+ public function it_can_get_its_depth ()
207
+ {
208
+ $ this
209
+ ->depth ()
210
+ ->shouldReturn (0 );
211
+
212
+ $ tree = new \drupol \phptree \Node \ValueNode ('root ' , 2 );
213
+
214
+ $ tree ->add ($ this ->getWrappedObject ());
215
+
216
+ $ this
217
+ ->depth ()
218
+ ->shouldReturn (1 );
219
+
220
+ $ nodes = [];
221
+ foreach (\range ('A ' , 'Z ' ) as $ v ) {
222
+ $ nodes [] = new \drupol \phptree \Node \ValueNode ($ v );
223
+ }
224
+
225
+ $ tree ->add (...$ nodes );
226
+
227
+ $ tree ->add ($ this ->getWrappedObject ());
228
+
229
+ $ this
230
+ ->depth ()
231
+ ->shouldReturn (4 );
232
+ }
205
233
}
Original file line number Diff line number Diff line change @@ -174,4 +174,12 @@ public function withChildren(NodeInterface ...$nodes): NodeInterface
174
174
175
175
return $ clone ;
176
176
}
177
+
178
+ /**
179
+ * {@inheritdoc}
180
+ */
181
+ public function depth (): int
182
+ {
183
+ return \count ($ this ->getAncestors ());
184
+ }
177
185
}
Original file line number Diff line number Diff line change @@ -97,4 +97,12 @@ public function getSibblings(): array;
97
97
* The amound of children.
98
98
*/
99
99
public function degree (): int ;
100
+
101
+ /**
102
+ * Get the node depth from the root node.
103
+ *
104
+ * @return int
105
+ * The depth is the number of nodes before root.
106
+ */
107
+ public function depth (): int ;
100
108
}
You can’t perform that action at this time.
0 commit comments