php-src/ext/spl/internal/recursiveiteratoriterator.inc

101 lines
1.7 KiB
PHP
Raw Normal View History

<?php
/**
* @brief Iterates through recursive iterators
* @author Marcus Boerger
* @version 1.0
*
*/
class RecursiveIteratorIterator implements Iterator
{
protected $ait = array();
protected $count = 0;
2003-11-22 20:51:15 +00:00
function __construct(RecursiveIterator $it)
{
$this->ait[0] = $it;
}
2003-11-22 20:51:15 +00:00
function rewind()
{
while ($this->count) {
unset($this->ait[$this->count--]);
}
$this->ait[0]->rewind();
$this->ait[0]->recursed = false;
}
function valid()
2003-11-22 20:51:15 +00:00
{
$count = $this->count;
2003-11-22 20:51:15 +00:00
while ($count) {
$it = $this->ait[$count];
if ($it->valid()) {
return true;
}
2003-11-22 20:51:15 +00:00
$count--;
}
return false;
}
2003-11-22 20:51:15 +00:00
function key()
{
$it = $this->ait[$this->count];
return $it->key();
}
2003-11-22 20:51:15 +00:00
function current()
{
$it = $this->ait[$this->count];
return $it->current();
}
2003-11-22 20:51:15 +00:00
function next()
{
while ($this->count) {
2003-11-22 20:51:15 +00:00
$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()) {
2003-11-22 20:51:15 +00:00
$this->ait[++$this->count] = $sub;
2004-01-23 21:03:20 +00:00
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;
}
2003-11-22 20:51:15 +00:00
if ($this->count) {
unset($this->ait[$this->count--]);
$it = $this->ait[$this->count];
}
}
}
2003-11-22 20:51:15 +00:00
function getSubIterator($level = NULL)
{
if (is_null($level)) {
$level = $this->count;
}
return @$this->ait[$level];
}
function getDepth()
{
return $this->level;
}
}
?>