44
55use Livy \Climber \Spotter \Forester ;
66use Livy \Climber \Spotter \Spotter ;
7- use \ Zenodorus as Z ;
7+ use Zenodorus as Z ;
88
99/**
1010 * This class generates data trees for processing by Climber.
@@ -172,34 +172,19 @@ protected function plant()
172172 return null ;
173173 }
174174
175- public function getLeafPath (int $ id , array $ ancestors = [] )
175+ public function getLeafSiblings (int $ id , bool $ exclude = null )
176176 {
177- if (null === $ this ->getLeaf ($ id )
178- || null === $ this ->getLeafContent ($ id , 0 )) {
179- // This item doesn't exist or has no ancestors.
180- // Either we've reached the last step in our path, we were passed
181- // a bad leaf, or this leaf has no parent.
182- return array_reverse ($ ancestors );
183- }
184-
185- array_push ($ ancestors , $ this ->getLeafContent ($ id , 0 ));
186-
187- return $ this ->getLeafPath ($ this ->getLeafContent ($ id , 0 ), $ ancestors );
188- }
177+ $ parent = $ this ->getLeafContent ($ id , 'parent ' );
178+ $ tree = $ this ->tree ;
179+ $ siblings = array_filter ($ tree , function ($ item ) use ($ parent ) {
180+ return $ item [0 ] == $ parent ;
181+ });
189182
190- public function isLeafChildOf (int $ leaf , int $ parent )
191- {
192- $ ancestors = $ this ->getLeafPath ($ leaf );
193- if (count ($ ancestors ) > 0 && in_array ($ parent , $ ancestors )) {
194- return true ;
183+ if ($ exclude ) {
184+ unset($ siblings [$ id ]);
195185 }
196186
197- return false ;
198- }
199-
200- public function getLeaf (int $ id )
201- {
202- return isset ($ this ->tree [$ id ]) ? $ this ->tree [$ id ] : null ;
187+ return $ siblings ;
203188 }
204189
205190 public function getLeafContent (int $ id , $ slot , $ data = null )
@@ -242,19 +227,9 @@ protected function query($slot)
242227 return false ;
243228 }
244229
245- public function getLeafSiblings (int $ id, bool $ exclude = null )
230+ public function getLeaf (int $ id )
246231 {
247- $ parent = $ this ->getLeafContent ($ id , 'parent ' );
248- $ tree = $ this ->tree ;
249- $ siblings = array_filter ($ tree , function ($ item ) use ($ parent ) {
250- return $ item [0 ] == $ parent ;
251- });
252-
253- if ($ exclude ) {
254- unset($ siblings [$ id ]);
255- }
256-
257- return $ siblings ;
232+ return isset ($ this ->tree [$ id ]) ? $ this ->tree [$ id ] : null ;
258233 }
259234
260235 public function setLeaf (int $ id , array ...$ actions )
@@ -624,16 +599,6 @@ protected function setActive(int $id, string $value)
624599 );
625600 }
626601
627- /**
628- * We want to know if we're the original or not, so set `$clone`.
629- *
630- * @return void
631- */
632- protected function __clone ()
633- {
634- $ this ->clone = true ;
635- }
636-
637602 /**
638603 * Takes a leaf and returns a new Tree composed of only the children of that leaf.
639604 *
@@ -664,6 +629,31 @@ public function subtree(int $root, bool $includeRoot = false)
664629 );
665630 }
666631
632+ public function isLeafChildOf (int $ leaf , int $ parent )
633+ {
634+ $ ancestors = $ this ->getLeafPath ($ leaf );
635+ if (count ($ ancestors ) > 0 && in_array ($ parent , $ ancestors )) {
636+ return true ;
637+ }
638+
639+ return false ;
640+ }
641+
642+ public function getLeafPath (int $ id , array $ ancestors = [])
643+ {
644+ if (null === $ this ->getLeaf ($ id )
645+ || null === $ this ->getLeafContent ($ id , 0 )) {
646+ // This item doesn't exist or has no ancestors.
647+ // Either we've reached the last step in our path, we were passed
648+ // a bad leaf, or this leaf has no parent.
649+ return array_reverse ($ ancestors );
650+ }
651+
652+ array_push ($ ancestors , $ this ->getLeafContent ($ id , 0 ));
653+
654+ return $ this ->getLeafPath ($ this ->getLeafContent ($ id , 0 ), $ ancestors );
655+ }
656+
667657 /**
668658 * Sometimes we don't want to be a clone after all.
669659 */
@@ -672,6 +662,16 @@ public function declone()
672662 $ this ->clone = false ;
673663 }
674664
665+ /**
666+ * We want to know if we're the original or not, so set `$clone`.
667+ *
668+ * @return void
669+ */
670+ protected function __clone ()
671+ {
672+ $ this ->clone = true ;
673+ }
674+
675675 /**
676676 * Remove a leaf from the tree.
677677 *
0 commit comments