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
return call_user_func_array(array($this->getSubIterator(), $func), $params);
}
}
+
+?>
\ No newline at end of file