cachingiterator.inc [plain text]
<?php
class CachingIterator implements OuterIterator
{
const CALL_TOSTRING = 0x00000001;
const CATCH_GET_CHILD = 0x00000002;
const TOSTRING_USE_KEY = 0x00000010;
const TOSTRING_USE_CURRENT = 0x00000020;
private $it;
private $current;
private $key;
private $valid;
private $strValue;
function __construct(Iterator $it, $flags = self::CALL_TOSTRING)
{
if ((($flags & self::CALL_TOSTRING) && ($flags & (self::TOSTRING_USE_KEY|self::TOSTRING_USE_CURRENT)))
|| ((flags & (self::CIT_TOSTRING_USE_KEY|self::CIT_TOSTRING_USE_CURRENT)) == (self::CIT_TOSTRING_USE_KEY|self::CIT_TOSTRING_USE_CURRENT)))
{
throw new InvalidArgumentException('Flags must contain only one of CIT_CALL_TOSTRING, CIT_TOSTRING_USE_KEY, CIT_TOSTRING_USE_CURRENT');
}
$this->it = $it;
$this->flags = $flags & (0x0000FFFF);
$this->next();
}
function rewind()
{
$this->it->rewind();
$this->next();
}
function next()
{
if ($this->valid = $this->it->valid()) {
$this->current = $this->it->current();
$this->key = $this->it->key();
if ($this->flags & self::CALL_TOSTRING) {
if (is_object($this->current)) {
$this->strValue = $this->current->__toString();
} else {
$this->strValue = (string)$this->current;
}
}
} else {
$this->current = NULL;
$this->key = NULL;
$this->strValue = NULL;
}
$this->it->next();
}
function valid()
{
return $this->valid;
}
function hasNext()
{
return $this->it->valid();
}
function current()
{
return $this->current;
}
function key()
{
return $this->key;
}
function __call($func, $params)
{
return call_user_func_array(array($this->it, $func), $params);
}
function __toString()
{
if ($this->flags & self::TOSTRING_USE_KEY)
{
return $this->key;
}
else if ($this->flags & self::TOSTRING_USE_CURRENT)
{
return $this->current;
}
if (!$this->flags & self::CALL_TOSTRING)
{
throw new exception('CachingIterator does not fetch string value (see CachingIterator::__construct)');
}
return $this->strValue;
}
function getInnerIterator()
{
return $this->it;
}
}
?>