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

141 lines
3.1 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
2004-10-31 19:05:37 +00:00
* @ingroup SPL
2004-10-29 20:58:58 +00:00
* @brief class CachingIterator
* @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
*/
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
/**
2004-10-31 19:05:37 +00:00
* @brief Cached iteration over another Iterator
2004-10-29 20:58:58 +00:00
* @author Marcus Boerger
* @version 1.1
2005-02-17 00:24:57 +00:00
* @since PHP 5.0
2004-10-29 20:58:58 +00:00
*
2004-10-31 19:05:37 +00:00
* This iterator wrapper does a one ahead iteration. This way it knows whether
* the inner iterator has one more element.
*
* @note If you want to convert the elements into strings and the inner
* Iterator is an internal Iterator then you need to provide the
* flag CIT_CALL_TOSTRING to do the conversion when the actual element
* is being fetched. Otherwise the conversion would happen with the
* already changed iterator. If you do not need this then it you should
* omit this flag because it costs unneccessary work and time.
2004-10-29 20:58:58 +00:00
*/
2004-10-29 20:12:57 +00:00
class CachingIterator implements OuterIterator
2003-11-18 22:18:38 +00:00
{
2005-02-08 19:00:19 +00:00
private $it;
private $current;
private $key;
private $valid;
private $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
2004-10-31 19:05:37 +00:00
*
* @param func Name of method to invoke
* @param params Array of parameters to pass to method
2004-10-29 20:58:58 +00:00
*/
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
}
?>