From ce0aee5e4129dbd5d74e6476c5899dba4c5068ec Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Fri, 6 Jun 2014 13:58:14 +0200 Subject: [PATCH] lib: Add Data/Tree/Node.php --- library/Icinga/Data/Tree/Node.php | 62 +++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 library/Icinga/Data/Tree/Node.php diff --git a/library/Icinga/Data/Tree/Node.php b/library/Icinga/Data/Tree/Node.php new file mode 100644 index 000000000..db3a49749 --- /dev/null +++ b/library/Icinga/Data/Tree/Node.php @@ -0,0 +1,62 @@ +value = $value; + } + + public function getValue() + { + return $this->value; + } + + public function appendChild($value) + { + $child = new static($value); + $this->push($child); + return $child; + } + + public function hasChildren() + { + $current = $this->current(); + if ($current === null) {; + $current = $this; + } + return ! $current->isEmpty(); + } + + public function getChildren() + { + $current = $this->current(); + if ($current === null) {; + $current = $this; + } + return $current; + } + + public function findNodeBy($callback) + { + if (! is_callable($callback)) { + throw new RuntimeException(); + } + foreach (new RecursiveIteratorIterator($this, RecursiveIteratorIterator::SELF_FIRST) as $node) { + if (call_user_func($callback, $node)) { + return $node; + } + } + return null; + } +}