php-src/ext/spl/examples/recursivetreeiterator.inc

88 lines
2.4 KiB
PHP
Raw Normal View History

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
*/
/** @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;
2005-10-05 19:11:51 +00:00
/**
* @param it iterator to use as inner iterator
* @param flags flags passed to RecursiveIteratoIterator (parent)
* @param cit_flags flags passed to RecursiveCachingIterator (for hasNext)
2005-10-03 13:36:18 +00:00
*/
2005-10-05 19:11:51 +00:00
function __construct(RecursiveIterator $it, $flags = self::SELF_FIRST, $cit_flags = CachingIterator::CATCH_GET_CHILD)
2005-10-03 13:36:18 +00:00
{
2005-10-05 19:11:51 +00:00
parent::__construct(new RecursiveCachingIterator($it, $cit_flags), $flags);
$this->callToString = (bool)($cit_flags & CachingIterator::CALL_TOSTRING);
2005-10-03 13:36:18 +00:00
}
2005-10-05 19:11:51 +00:00
/** 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
2005-10-03 13:36:18 +00:00
*/
2005-10-05 19:11:51 +00:00
public $prefix = array(0=>'', 1=>'| ', 2=>' ', 3=>'|-', 4=>'\-', 5=>'');
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-05 19:11:51 +00:00
return $this->callToString ? $this->__toString() : 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-05 19:11:51 +00:00
{
return $this->getPrefix() . $this->getEntry() . $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
?>