diff options
author | Marcus Boerger <helly@php.net> | 2005-10-02 17:33:05 +0000 |
---|---|---|
committer | Marcus Boerger <helly@php.net> | 2005-10-02 17:33:05 +0000 |
commit | 0ea0e1615f8892c3f024418c69b2398bd949b619 (patch) | |
tree | d34f8164aba4db34e6de44f06986dc160f243201 | |
parent | 224e1f50aed5e2c9852c97301c107651c2d18f22 (diff) | |
download | php-git-0ea0e1615f8892c3f024418c69b2398bd949b619.tar.gz |
- MFH Add new example
-rwxr-xr-x | ext/spl/examples/recursivetreeiterator.inc | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/ext/spl/examples/recursivetreeiterator.inc b/ext/spl/examples/recursivetreeiterator.inc new file mode 100755 index 0000000000..169b3a8022 --- /dev/null +++ b/ext/spl/examples/recursivetreeiterator.inc @@ -0,0 +1,48 @@ +<?php + +/** @file recursivetreeiterator.inc + * @ingroup Examples + * @brief class RecursiveTreeIterator + * @author Marcus Boerger, Johannes Schlueter + * @date 2005 + * + * SPL - Standard PHP Library + */ + + +/** @ingroup Examples + * @brief RecursiveIteratorIterator to generate ASCII graphic trees for the + * entries in a RecursiveIterator + * @author Marcus Boerger, Johannes Schlueter + * @version 1.0 + */ +class RecursiveTreeIterator extends RecursiveIteratorIterator +{ + private $callToString; + + function __construct(RecursiveIterator $it, $cit_flags = CachingIterator::CATCH_GET_CHILD) + { + parent::__construct(new RecursiveCachingIterator($it, $cit_flags), 1); + $this->callToString = (bool)($cit_flags & CachingIterator::CALL_TOSTRING); + } + + /** @return the current element prefixed with ASCII graphics + */ + function current() + { + $tree = ''; + for ($l=0; $l < $this->getDepth(); $l++) { + $tree .= $this->getSubIterator($l)->hasNext() ? '| ' : ' '; + } + + return $tree . ($this->getSubIterator($l)->hasNext() ? '|-' : '\-') + . ($this->callToString ? $this->getSubIterator($l)->__toString() : $this->getSubIterator($l)->current()); + } + + /** Aggregates the inner iterator + */ + function __call($func, $params) + { + return call_user_func_array(array($this->getSubIterator(), $func), $params); + } +} |