1<?php 2 3namespace ComboStrap; 4 5 6class TreeSystem 7{ 8 9 public static function getDescendantCount(TreeNode $treeNode): int 10 { 11 $counterClass = new class { 12 13 private int $counter = -1; 14 15 public function increment() 16 { 17 $this->counter++; 18 } 19 20 public function getCounter(): int 21 { 22 return $this->counter; 23 } 24 25 }; 26 TreeVisit::visit($treeNode, array($counterClass, 'increment')); 27 return $counterClass->getCounter(); 28 } 29 30 public static function print(TreeNode $tree) 31 { 32 $printRecursively = function (TreeNode $treeNode, int $level) { 33 for ($i = 0; $i < $level - 1; $i++) { 34 echo " "; 35 } 36 echo "- $treeNode\n"; 37 }; 38 TreeVisit::visit($tree, $printRecursively); 39 } 40 41 42} 43 44