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

99 lines
2.1 KiB
PHP
Raw Normal View History

2003-11-18 22:18:38 +00:00
<?php
/** @file recursivecachingiterator.inc
2004-10-31 19:05:37 +00:00
* @ingroup SPL
* @brief class RecursiveCachingIterator
2004-10-29 20:58:58 +00:00
* @author Marcus Boerger
2006-02-21 23:21:53 +00:00
* @date 2003 - 2006
2004-10-29 20:58:58 +00:00
*
* SPL - Standard PHP Library
*/
/**
2004-10-31 19:05:37 +00:00
* @brief Cached recursive iteration over another Iterator
2004-10-29 20:58:58 +00:00
* @author Marcus Boerger
* @version 1.2
* @since PHP 5.1
2004-10-31 19:05:37 +00:00
*
2004-11-01 18:11:39 +00:00
* @see CachingIterator
2004-10-29 20:58:58 +00:00
*/
class RecursiveCachingIterator extends CachingIterator implements RecursiveIterator
2003-11-18 22:18:38 +00:00
{
2005-02-08 19:00:19 +00:00
private $hasChildren;
private $getChildren;
2003-11-18 22:18:38 +00:00
2004-10-29 20:58:58 +00:00
/** Construct from another iterator
*
* @param it Iterator to cache
* @param flags Bitmask:
* - CALL_TOSTRING (whether to call __toString() for every element)
* - CATCH_GET_CHILD (whether to catch exceptions when trying to get childs)
2004-10-29 20:58:58 +00:00
*/
function __construct(RecursiveIterator $it, $flags = self::CALL_TOSTRING)
2003-11-22 20:51:15 +00:00
{
parent::__construct($it, $flags);
2003-11-18 22:18:38 +00:00
}
2004-10-29 20:58:58 +00:00
/** Rewind Iterator
*/
2004-05-02 22:07:32 +00:00
function rewind();
{
$this->hasChildren = false;
$this->getChildren = NULL;
parent::rewind();
}
2004-10-29 20:58:58 +00:00
/** Forward to next element if necessary then an Iterator for the Children
* will be created.
*/
2003-11-22 20:51:15 +00:00
function next()
{
if ($this->hasChildren = $this->it->hasChildren())
{
try
{
$child = $this->it->getChildren();
if (!$this->ref)
{
$this->ref = new ReflectionClass($this);
}
$this->getChildren = $ref->newInstance($child, $this->flags);
}
catch(Exception $e)
{
if (!$this->flags & self::CATCH_GET_CHILD)
{
throw $e;
}
$this->hasChildren = false;
$this->getChildren = NULL;
}
} else
{
2003-11-18 22:18:38 +00:00
$this->getChildren = NULL;
}
parent::next();
}
private $ref;
2003-11-18 22:18:38 +00:00
2004-10-29 20:58:58 +00:00
/** @return whether the current element has children
* @note The check whether the Iterator for the children can be created was
* already executed. Hence when flag CATCH_GET_CHILD was given in
2004-10-29 20:58:58 +00:00
* constructor this fucntion returns false so that getChildren does
* not try to access those children.
*/
2003-11-22 20:51:15 +00:00
function hasChildren()
{
2003-11-18 22:18:38 +00:00
return $this->hasChildren;
}
2004-10-29 20:58:58 +00:00
/** @return An Iterator for the children
*/
2003-11-22 20:51:15 +00:00
function getChildren()
{
2003-11-18 22:18:38 +00:00
return $this->getChildren;
}
}
?>