From: Marcus Boerger Date: Wed, 5 Oct 2005 19:11:51 +0000 (+0000) Subject: - MFH Update examples X-Git-Tag: php-5.1.0RC2~97 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3ced246dfcdb3ee54db1c3ab06b67934e66fdea8;p=php - MFH Update examples --- diff --git a/ext/spl/examples/recursivetreeiterator.inc b/ext/spl/examples/recursivetreeiterator.inc index ab526e4ca1..62198c8d36 100755 --- a/ext/spl/examples/recursivetreeiterator.inc +++ b/ext/spl/examples/recursivetreeiterator.inc @@ -19,68 +19,62 @@ 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 prefix used if $level < depth and hasNext($level) == true + /** + * @param it iterator to use as inner iterator + * @param flags flags passed to RecursiveIteratoIterator (parent) + * @param cit_flags flags passed to RecursiveCachingIterator (for hasNext) */ - function getPrefix1() + function __construct(RecursiveIterator $it, $flags = self::SELF_FIRST, $cit_flags = CachingIterator::CATCH_GET_CHILD) { - return '| '; + parent::__construct(new RecursiveCachingIterator($it, $cit_flags), $flags); + $this->callToString = (bool)($cit_flags & CachingIterator::CALL_TOSTRING); } - /** @return prefix used if $level < depth and hasNext($level) == false + /** Prefix strings used in getPrefix() + * + * 0: prefix used to start elements + * 1: prefix used if $level < depth and hasNext($level) == true + * 2: prefix used if $level < depth and hasNext($level) == false + * 3: prefix used if $level == depth and hasNext($level) == true + * 4: prefix used if $level == depth and hasNext($level) == false + * 5: prefix used right in front of the current element */ - function getPrefix2() - { - return ' '; - } + public $prefix = array(0=>'', 1=>'| ', 2=>' ', 3=>'|-', 4=>'\-', 5=>''); - /** @return prefix used if $level == depth and hasNext($level) == true + /** @return string to place in front of current element */ - function getPrefix3() + function getPrefix() { - return '|-'; + $tree = ''; + for ($level = 0; $level < $this->getDepth(); $level++) + { + $tree .= $this->getSubIterator($level)->hasNext() ? $this->prefix[1] : $this->prefix[2]; + } + $tree .= $this->getSubIterator($level)->hasNext() ? $this->prefix[3] : $this->prefix[4]; + + return $this->prefix[0] . $tree . $this->prefix[5]; } - /** @return prefix used if $level == depth and hasNext($level) == false + /** @return string presentation build for current element */ - function getPrefix4() + function getEntry() { - return '\-'; + return $this->callToString ? $this->__toString() : parent::current(); } - function getPrefix($level) + /** @return string to place after the current element + */ + function getPostfix() { - if ($level < 0 || $level > $this->getDepth()) - { - throw new OutOfBoundsException('Parameter $level must be >= 0 and <= depth'); - } - if ($level < $this->getDepth()) - { - return $this->getSubIterator($level)->hasNext() ? $this->getPrefix1() : $this->getPrefix2(); - } - else - { - return $this->getSubIterator($level)->hasNext() ? $this->getPrefix3() : $this->getPrefix4(); - } + return ''; } - /** @return the current element prefixed with ASCII graphics + /** @return the current element prefixed and postfixed */ function current() - { - $tree = ''; - for ($l=0; $l <= $this->getDepth(); $l++) - { - $tree .= $this->getPrefix($l); - } - - return $tree . ($this->callToString ? $this->__toString() : parent::current()); + { + return $this->getPrefix() . $this->getEntry() . $this->getPostfix(); } /** Aggregates the inner iterator @@ -90,3 +84,5 @@ class RecursiveTreeIterator extends RecursiveIteratorIterator return call_user_func_array(array($this->getSubIterator(), $func), $params); } } + +?> \ No newline at end of file