php-src/ext/spl/examples/infiniteiterator.inc

102 lines
1.7 KiB
PHP
Raw Normal View History

2004-04-27 18:15:00 +00:00
<?php
/** @file infiniteiterator.inc
* @ingroup Examples
* @brief class InfiniteIterator
* @author Marcus Boerger
* @date 2003 - 2004
*
* SPL - Standard PHP Library
*/
/** @ingroup Examples
2004-04-27 18:15:00 +00:00
* @brief An infinite Iterator
* @author Marcus Boerger
* @version 1.0
*
* This Iterator takes another Iterator and infinitvely iterates it by
* rewinding it when its end is reached.
*
* \note Even an InfiniteIterator stops if its inner Iterator is empty.
*
\verbatim
$it = new ArrayIterator(array(1,2,3));
$infinite = new InfiniteIterator($it);
$limit = new LimitIterator($infinite, 0, 5);
foreach($limit as $val=>$key)
{
echo "$val=>$key\n";
}
\endverbatim
*/
class InfiniteIterator implements Iterator
{
/** @internal
* The inner Iterator. */
2004-04-27 18:15:00 +00:00
private $it;
/** Construct from another Iterator.
* @param $it the inner Iterator.
*/
2004-04-27 18:15:00 +00:00
function __construct(Iterator $it)
{
$this->it = $it;
}
/** @return the inner iterator
*/
2004-04-27 18:15:00 +00:00
function getInnerIterator()
{
return $this->it;
}
/** Rewind the inner iterator.
* @return void
*/
2004-04-27 18:15:00 +00:00
function rewind()
{
$this->it->rewind();
}
/** @return whether the current element is valid
*/
2004-04-27 18:15:00 +00:00
function valid()
{
return $this->it->valid();
}
/** @return the current value
*/
2004-04-27 18:15:00 +00:00
function current()
{
return $this->it->current();
}
/** @return the current key
*/
2004-04-27 18:15:00 +00:00
function key()
{
return $this->it->key();
}
/** Move the inner Iterator forward to its next element or rewind it.
* @return void
*/
2004-04-27 18:15:00 +00:00
function next()
{
$this->it->next();
if (!$this->it->valid())
{
$this->it->rewind();
}
}
/** Aggregates the inner iterator
*/
function __call($func, $params)
{
return call_user_func_array(array($this->it, $func), $params);
}
2004-04-27 18:15:00 +00:00
}
?>