php-src/ext/spl/internal/recursiveiteratoriterator.inc
2004-10-29 20:58:58 +00:00

144 lines
2.8 KiB
PHP
Executable File

<?php
/** @file recursiveiteratoriterator.inc
* @ingroup Internal
* @brief class RecursiveIteratorIterator
* @author Marcus Boerger
* @date 2003 - 2004
*
* SPL - Standard PHP Library
*/
define('RIT_LEAVES_ONLY', 0);
define('RIT_SELF_FIRST', 1);
define('RIT_CHILD_FIRST', 2);
/**
* @brief Iterates through recursive iterators
* @author Marcus Boerger
* @version 1.1
*
*/
class RecursiveIteratorIterator implements OuterIterator
{
protected $ait = array();
protected $count = 0;
/** Construct from RecursiveIterator
*
* @param it RecursiveIterator to iterate
* @param flags Operation mode:
* - RIT_LEAVES_ONLY only show leaves
* - RIT_SELF_FIRST show parents prior to their childs
* - RIT_CHILD_FIRST show all childs prior to their parent
*/
function __construct(RecursiveIterator $it, $flags)
{
$this->ait[0] = $it;
}
/** Rewind to top iterator as set in constructor
*/
function rewind()
{
while ($this->count) {
unset($this->ait[$this->count--]);
}
$this->ait[0]->rewind();
$this->ait[0]->recursed = false;
}
/** @return whether iterator is valid
*/
function valid()
{
$count = $this->count;
while ($count) {
$it = $this->ait[$count];
if ($it->valid()) {
return true;
}
$count--;
}
return false;
}
/** @reutrn current key
*/
function key()
{
$it = $this->ait[$this->count];
return $it->key();
}
/** @return current element
*/
function current()
{
$it = $this->ait[$this->count];
return $it->current();
}
/** Forward to next element
*/
function next()
{
while ($this->count) {
$it = $this->ait[$this->count];
if ($it->valid()) {
if (!$it->recursed && $it->hasChildren()) {
$it->recursed = true;
$sub = $it->getChildren();
$sub->recursed = false;
$sub->rewind();
if ($sub->valid()) {
$this->ait[++$this->count] = $sub;
if (!$sub instanceof RecursiveIterator) {
throw new Exception(get_class($sub).'::getChildren() must return an object that implements RecursiveIterator');
}
return;
}
unset($sub);
}
$it->next();
$it->recursed = false;
if ($it->valid()) {
return;
}
$it->recursed = false;
}
if ($this->count) {
unset($this->ait[$this->count--]);
$it = $this->ait[$this->count];
}
}
}
/** @return Sub Iterator at given level or if unspecified the current sub
* Iterator
*/
function getSubIterator($level = NULL)
{
if (is_null($level)) {
$level = $this->count;
}
return @$this->ait[$level];
}
/**
* @return The inner iterator
*/
function getInnerIterator()
{
return $this->it;
}
/** @return Current Depth (Number of parents)
*/
function getDepth()
{
return $this->level;
}
}
?>