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

128 lines
2.4 KiB
PHP
Raw Normal View History

2003-11-18 22:18:38 +00:00
<?php
2004-10-29 20:58:58 +00:00
/** @file cachingiterator.inc
* @ingroup Internal
* @brief class CachingIterator
* @author Marcus Boerger
* @date 2003 - 2004
*
* SPL - Standard PHP Library
*/
2003-12-08 08:31:08 +00:00
define('CIT_CALL_TOSTRING', 1);
define('CIT_CATCH_GET_CHILD', 2);
2004-10-29 20:58:58 +00:00
/**
* @brief Cached Iteration over another Iterator
* @author Marcus Boerger
* @version 1.1
*
*/
2004-10-29 20:12:57 +00:00
class CachingIterator implements OuterIterator
2003-11-18 22:18:38 +00:00
{
protected $it;
protected $current;
protected $key;
2004-05-02 22:07:32 +00:00
protected $valid;
2003-12-06 19:03:17 +00:00
protected $strValue;
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:
* - CIT_CALL_TOSTRING (whether to call __toString() for every element)
*/
2003-12-08 08:31:08 +00:00
function __construct(Iterator $it, $flags = CIT_CALL_TOSTRING)
2003-11-22 20:51:15 +00:00
{
2003-11-18 22:18:38 +00:00
$this->it = $it;
2003-12-08 08:31:08 +00:00
$this->flags = $flags & (CIT_CALL_TOSTRING|CIT_CATCH_GET_CHILD);
2004-05-02 22:07:32 +00:00
$this->next();
2003-11-18 22:18:38 +00:00
}
2004-10-29 20:58:58 +00:00
/** Rewind the Iterator
*/
2003-11-22 20:51:15 +00:00
function rewind()
{
2003-11-18 22:18:38 +00:00
$this->it->rewind();
$this->next();
}
2004-10-29 20:58:58 +00:00
/** Forward to the next element
*/
2003-11-22 20:51:15 +00:00
function next()
{
2004-05-02 22:07:32 +00:00
if ($this->valid = $this->it->valid()) {
2003-11-18 22:18:38 +00:00
$this->current = $this->it->current();
$this->key = $this->it->key();
2003-12-08 08:31:08 +00:00
if ($this->flags & CIT_CALL_TOSTRING) {
2003-12-06 19:03:17 +00:00
if (is_object($this->current)) {
$this->strValue = $this->current->__toString();
} else {
$this->strValue = (string)$this->current;
}
2003-12-04 20:56:32 +00:00
}
2003-11-18 22:18:38 +00:00
} else {
$this->current = NULL;
$this->key = NULL;
$this->strValue = NULL;
2003-11-18 22:18:38 +00:00
}
$this->it->next();
}
2004-10-29 20:58:58 +00:00
/** @return whether teh iterator is valid
*/
function valid()
2003-11-22 20:51:15 +00:00
{
2004-05-02 22:07:32 +00:00
return $this->valid;
2003-11-18 22:18:38 +00:00
}
2004-10-29 20:58:58 +00:00
/** @return whether there is one more element
*/
2003-11-22 20:51:15 +00:00
function hasNext()
{
return $this->it->valid();
2003-11-18 22:18:38 +00:00
}
2004-10-29 20:58:58 +00:00
/** @return the current element
*/
2003-11-22 20:51:15 +00:00
function current()
{
2003-11-18 22:18:38 +00:00
return $this->current;
}
2004-10-29 20:58:58 +00:00
/** @return the current key
*/
2003-11-22 20:51:15 +00:00
function key()
{
2003-11-18 22:18:38 +00:00
return $this->key;
}
2004-10-29 20:58:58 +00:00
/** Aggregate the inner iterator
*/
2003-11-22 20:51:15 +00:00
function __call($func, $params)
{
2003-11-18 22:18:38 +00:00
return call_user_func_array(array($this->it, $func), $params);
}
2004-10-29 20:58:58 +00:00
/** @return the string represenatation that was generated for the current
* element
* @throw exception when CIT_CALL_TOSTRING was not specified in constructor
*/
2003-11-22 20:51:15 +00:00
function __toString()
{
2003-12-08 08:31:08 +00:00
if (!$this->flags & CIT_CALL_TOSTRING) {
2003-12-06 19:03:17 +00:00
throw new exception('CachingIterator does not fetch string value (see CachingIterator::__construct)');
}
return $this->strValue;
2003-11-18 22:18:38 +00:00
}
2004-10-29 20:12:57 +00:00
2004-10-29 20:58:58 +00:00
/**
* @return The inner iterator
*/
2004-10-29 20:12:57 +00:00
function getInnerIterator()
{
return $this->it;
}
2003-11-18 22:18:38 +00:00
}
?>