2003-11-18 22:18:38 +00:00
|
|
|
<?php
|
|
|
|
|
2005-09-18 17:15:04 +00:00
|
|
|
/** @file recursivecachingiterator.inc
|
2004-10-31 19:05:37 +00:00
|
|
|
* @ingroup SPL
|
2005-09-18 17:15:04 +00:00
|
|
|
* @brief class RecursiveCachingIterator
|
2004-10-29 20:58:58 +00:00
|
|
|
* @author Marcus Boerger
|
2005-02-08 19:10:06 +00:00
|
|
|
* @date 2003 - 2005
|
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
|
2005-09-18 17:15:04 +00:00
|
|
|
* @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
|
|
|
*/
|
2005-09-18 17:15:04 +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:
|
2005-09-15 03:33:04 +00:00
|
|
|
* - 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
|
|
|
*/
|
2005-09-15 03:33:04 +00:00
|
|
|
function __construct(RecursiveIterator $it, $flags = self::CALL_TOSTRING)
|
2003-11-22 20:51:15 +00:00
|
|
|
{
|
2003-12-07 15:03:11 +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()
|
|
|
|
{
|
2005-09-18 17:15:04 +00:00
|
|
|
if ($this->hasChildren = $this->it->hasChildren())
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2003-11-19 00:18:30 +00:00
|
|
|
$child = $this->it->getChildren();
|
2005-09-18 17:15:04 +00:00
|
|
|
if (!$this->ref)
|
|
|
|
{
|
|
|
|
$this->ref = new ReflectionClass($this);
|
|
|
|
}
|
|
|
|
$this->getChildren = $ref->newInstance($child, $this->flags);
|
2003-11-19 00:18:30 +00:00
|
|
|
}
|
2005-09-18 17:15:04 +00:00
|
|
|
catch(Exception $e)
|
|
|
|
{
|
|
|
|
if (!$this->flags & self::CATCH_GET_CHILD)
|
|
|
|
{
|
2003-11-19 00:18:30 +00:00
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
$this->hasChildren = false;
|
|
|
|
$this->getChildren = NULL;
|
|
|
|
}
|
2005-09-18 17:15:04 +00:00
|
|
|
} else
|
|
|
|
{
|
2003-11-18 22:18:38 +00:00
|
|
|
$this->getChildren = NULL;
|
|
|
|
}
|
|
|
|
parent::next();
|
|
|
|
}
|
2005-09-18 17:15:04 +00:00
|
|
|
|
|
|
|
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
|
2005-09-15 03:33:04 +00:00
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|