2005-10-02 17:33:05 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/** @file recursivetreeiterator.inc
|
|
|
|
* @ingroup Examples
|
|
|
|
* @brief class RecursiveTreeIterator
|
|
|
|
* @author Marcus Boerger, Johannes Schlueter
|
|
|
|
* @date 2005
|
|
|
|
*
|
|
|
|
* SPL - Standard PHP Library
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2008-07-19 15:49:21 +00:00
|
|
|
/** @ingroup SPL
|
2005-10-02 17:33:05 +00:00
|
|
|
* @brief RecursiveIteratorIterator to generate ASCII graphic trees for the
|
|
|
|
* entries in a RecursiveIterator
|
|
|
|
* @author Marcus Boerger, Johannes Schlueter
|
2008-07-19 15:49:21 +00:00
|
|
|
* @version 1.1
|
|
|
|
* @since PHP 5.3
|
2005-10-02 17:33:05 +00:00
|
|
|
*/
|
|
|
|
class RecursiveTreeIterator extends RecursiveIteratorIterator
|
|
|
|
{
|
2005-10-08 19:09:58 +00:00
|
|
|
const BYPASS_CURRENT = 0x00000004;
|
2005-10-13 19:49:27 +00:00
|
|
|
const BYPASS_KEY = 0x00000008;
|
2005-10-08 19:09:58 +00:00
|
|
|
|
|
|
|
private $rit_flags;
|
2005-10-02 17:33:05 +00:00
|
|
|
|
2005-10-05 19:11:51 +00:00
|
|
|
/**
|
|
|
|
* @param it iterator to use as inner iterator
|
2005-10-08 19:09:58 +00:00
|
|
|
* @param rit_flags flags passed to RecursiveIteratoIterator (parent)
|
2005-10-05 19:11:51 +00:00
|
|
|
* @param cit_flags flags passed to RecursiveCachingIterator (for hasNext)
|
2005-10-08 19:09:58 +00:00
|
|
|
* @param mode mode passed to RecursiveIteratoIterator (parent)
|
2005-10-03 13:36:18 +00:00
|
|
|
*/
|
2005-10-13 19:49:27 +00:00
|
|
|
function __construct(RecursiveIterator $it, $rit_flags = self::BYPASS_KEY, $cit_flags = CachingIterator::CATCH_GET_CHILD, $mode = self::SELF_FIRST)
|
2005-10-03 13:36:18 +00:00
|
|
|
{
|
2005-10-08 19:09:58 +00:00
|
|
|
parent::__construct(new RecursiveCachingIterator($it, $cit_flags), $mode, $rit_flags);
|
|
|
|
$this->rit_flags = $rit_flags;
|
2005-10-03 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
2008-07-19 15:49:21 +00:00
|
|
|
private $prefix = array(0=>'', 1=>'| ', 2=>' ', 3=>'|-', 4=>'\-', 5=>'');
|
|
|
|
|
|
|
|
/** Prefix used to start elements. */
|
|
|
|
const PREFIX_LEFT = 0;
|
|
|
|
/** Prefix used if $level < depth and hasNext($level) == true. */
|
|
|
|
const PREFIX_MID_HAS_NEXT = 1;
|
|
|
|
/** Prefix used if $level < depth and hasNext($level) == false. */
|
|
|
|
const PREFIX_MID_LAST = 2;
|
|
|
|
/** Prefix used if $level == depth and hasNext($level) == true. */
|
|
|
|
const PREFIX_END_HAS_NEXT = 3;
|
|
|
|
/** Prefix used if $level == depth and hasNext($level) == false. */
|
|
|
|
const PREFIX_END_LAST = 4;
|
|
|
|
/** Prefix used right in front of the current element. */
|
|
|
|
const PREFIX_RIGHT = 5;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set prefix part as used in getPrefix() and stored in $prefix.
|
|
|
|
* @param $part any PREFIX_* const.
|
|
|
|
* @param $value new prefix string for specified part.
|
|
|
|
* @throws OutOfRangeException if 0 > $part or $part > 5.
|
2005-10-03 13:36:18 +00:00
|
|
|
*/
|
2008-07-19 15:49:21 +00:00
|
|
|
function setPrefixPart($part, $value)
|
|
|
|
{
|
|
|
|
if (0 > $part || $part > 5) {
|
|
|
|
throw new OutOfRangeException();
|
|
|
|
}
|
|
|
|
$this->prefix[$part] = (string)$value;
|
|
|
|
}
|
2005-10-03 13:36:18 +00:00
|
|
|
|
2005-10-05 19:11:51 +00:00
|
|
|
/** @return string to place in front of current element
|
2005-10-03 13:36:18 +00:00
|
|
|
*/
|
2005-10-05 19:11:51 +00:00
|
|
|
function getPrefix()
|
2005-10-03 13:36:18 +00:00
|
|
|
{
|
2005-10-05 19:11:51 +00:00
|
|
|
$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];
|
2005-10-03 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
2005-10-05 19:11:51 +00:00
|
|
|
/** @return string presentation build for current element
|
2005-10-03 13:36:18 +00:00
|
|
|
*/
|
2005-10-05 19:11:51 +00:00
|
|
|
function getEntry()
|
2005-10-03 13:36:18 +00:00
|
|
|
{
|
2005-10-13 19:49:27 +00:00
|
|
|
return @(string)parent::current();
|
2005-10-03 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
2005-10-05 19:11:51 +00:00
|
|
|
/** @return string to place after the current element
|
|
|
|
*/
|
|
|
|
function getPostfix()
|
2005-10-03 13:36:18 +00:00
|
|
|
{
|
2005-10-05 19:11:51 +00:00
|
|
|
return '';
|
2005-10-03 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
2005-10-05 19:11:51 +00:00
|
|
|
/** @return the current element prefixed and postfixed
|
2005-10-02 17:33:05 +00:00
|
|
|
*/
|
|
|
|
function current()
|
2005-10-13 19:49:27 +00:00
|
|
|
{
|
2005-10-08 19:09:58 +00:00
|
|
|
if ($this->rit_flags & self::BYPASS_CURRENT)
|
|
|
|
{
|
|
|
|
return parent::current();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-10-13 19:49:27 +00:00
|
|
|
return $this->getPrefix() . $this->getEntry() . $this->getPostfix();
|
2005-10-08 19:09:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @return the current key prefixed and postfixed
|
|
|
|
*/
|
|
|
|
function key()
|
|
|
|
{
|
2005-10-13 19:49:27 +00:00
|
|
|
if ($this->rit_flags & self::BYPASS_KEY)
|
|
|
|
{
|
|
|
|
return parent::key();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return $this->getPrefix() . parent::key() . $this->getPostfix();
|
|
|
|
}
|
2005-10-02 17:33:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Aggregates the inner iterator
|
|
|
|
*/
|
|
|
|
function __call($func, $params)
|
|
|
|
{
|
|
|
|
return call_user_func_array(array($this->getSubIterator(), $func), $params);
|
|
|
|
}
|
|
|
|
}
|
2005-10-05 19:11:51 +00:00
|
|
|
|
|
|
|
?>
|