/** * Private method, does the brunt of getLeafNode, but is separate to allow recursion * @param Tree $segment * @param array $path * @return Tree */ private function walkTree(Tree $segment, &$path) { if (!$segment->evaluateCondition()) { return null; } if ($segment->isLeafNode()) { return $segment; } $path[] = $segment; /* @var $child Tree */ foreach ($segment->getChildren() as $child) { $val = $this->walkTree($child, $path); if (isset($val)) { return $val; } } // Branch evaluated to false, pop the last path off array_pop($path); }