recursivetreeiterator.inc [plain text]
<?php
class RecursiveTreeIterator extends RecursiveIteratorIterator
{
const BYPASS_CURRENT = 0x00000004;
const BYPASS_KEY = 0x00000008;
private $rit_flags;
function __construct(RecursiveIterator $it, $rit_flags = self::BYPASS_KEY, $cit_flags = CachingIterator::CATCH_GET_CHILD, $mode = self::SELF_FIRST)
{
parent::__construct(new RecursiveCachingIterator($it, $cit_flags), $mode, $rit_flags);
$this->rit_flags = $rit_flags;
}
private $prefix = array(0=>'', 1=>'| ', 2=>' ', 3=>'|-', 4=>'\-', 5=>'');
const PREFIX_LEFT = 0;
const PREFIX_MID_HAS_NEXT = 1;
const PREFIX_MID_LAST = 2;
const PREFIX_END_HAS_NEXT = 3;
const PREFIX_END_LAST = 4;
const PREFIX_RIGHT = 5;
function setPrefixPart($part, $value)
{
if (0 > $part || $part > 5) {
throw new OutOfRangeException();
}
$this->prefix[$part] = (string)$value;
}
function getPrefix()
{
$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];
}
function getEntry()
{
return @(string)parent::current();
}
function getPostfix()
{
return '';
}
function current()
{
if ($this->rit_flags & self::BYPASS_CURRENT)
{
return parent::current();
}
else
{
return $this->getPrefix() . $this->getEntry() . $this->getPostfix();
}
}
function key()
{
if ($this->rit_flags & self::BYPASS_KEY)
{
return parent::key();
}
else
{
return $this->getPrefix() . parent::key() . $this->getPostfix();
}
}
function __call($func, $params)
{
return call_user_func_array(array($this->getSubIterator(), $func), $params);
}
}
?>