From 9edaaa82e893b9cacf4db412b4e28d9a90f008f2 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Wed, 28 May 2014 17:14:33 +0200 Subject: [PATCH] lib: Add TreeIterator --- library/Icinga/Data/Tree/TreeIterator.php | 90 +++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 library/Icinga/Data/Tree/TreeIterator.php diff --git a/library/Icinga/Data/Tree/TreeIterator.php b/library/Icinga/Data/Tree/TreeIterator.php new file mode 100644 index 000000000..6722f3b35 --- /dev/null +++ b/library/Icinga/Data/Tree/TreeIterator.php @@ -0,0 +1,90 @@ +nodes = $node->getChildren(); +// } +// +// public function hasChildren() +// { +// return $this->current()->hasChildren(); +// } +// +// public function getChildren() +// { +// return new self($this->current()); +// } +// +// public function current() +// { +// return $this->nodes[$this->position]; +// } +// +// public function next() +// { +// ++$this->position; +// } +// +// public function valid() +// { +// return isset($this->nodes[$this->position]); +// } +// +// public function rewind() +// { +// $this->position = 0; +// } +// +// public function key() +// { +// return $this->position; +// } +//} + +use ArrayIterator; +use RecursiveIterator; + +class TreeIterator extends ArrayIterator implements RecursiveIterator +{ + /** + * Create a new TreeIterator + * + * @param NodeInterface $node + */ + public function __construct(NodeInterface $node) + { + parent::__construct($node->getChildren()); + } + + /** + * Whether an iterator can be created for the current node + * + * @return bool + */ + public function hasChildren() + { + return $this->current()->hasChildren(); + } + + /** + * Return an iterator for the current node + * + * @return self + */ + public function getChildren() + { + return new self($this->current()); + } +}