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

134 lines
3.0 KiB
PHP
Raw Normal View History

<?php
2004-10-29 20:58:58 +00:00
/** @file limititerator.inc
2004-10-31 19:05:37 +00:00
* @ingroup SPL
2004-10-29 20:58:58 +00:00
* @brief class LimitIterator
* @author Marcus Boerger
* @date 2003 - 2009
2004-10-29 20:58:58 +00:00
*
* SPL - Standard PHP Library
*/
/**
* @brief Limited Iteration over another Iterator
* @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-11-01 18:11:39 +00:00
* A class that starts iteration at a certain offset and only iterates over
* a specified amount of elements.
*
* This class uses SeekableIterator::seek() if available and rewind() plus
* a skip loop otehrwise.
2004-10-29 20:58:58 +00:00
*/
2004-10-29 20:12:57 +00:00
class LimitIterator implements OuterIterator
{
2005-02-08 19:00:19 +00:00
private $it;
private $offset;
private $count;
2003-12-06 19:03:17 +00:00
private $pos;
2004-10-29 20:58:58 +00:00
/** Construct
*
* @param it Iterator to limit
* @param offset Offset to first element
2006-01-05 19:00:27 +00:00
* @param count Maximum number of elements to show or -1 for all
2004-10-29 20:58:58 +00:00
*/
2003-12-06 19:03:17 +00:00
function __construct(Iterator $it, $offset = 0, $count = -1)
{
2003-12-06 19:03:17 +00:00
if ($offset < 0) {
throw new exception('Parameter offset must be > 0');
}
if ($count < 0 && $count != -1) {
throw new exception('Parameter count must either be -1 or a value greater than or equal to 0');
}
$this->it = $it;
$this->offset = $offset;
$this->count = $count;
2003-12-06 19:03:17 +00:00
$this->pos = 0;
}
2004-10-29 20:58:58 +00:00
/** Seek to specified position
* @param position offset to seek to (relative to beginning not offset
* specified in constructor).
* @throw exception when position is invalid
*/
2003-12-06 19:03:17 +00:00
function seek($position) {
if ($position < $this->offset) {
throw new exception('Cannot seek to '.$position.' which is below offset '.$this->offset);
}
if ($position > $this->offset + $this->count && $this->count != -1) {
throw new exception('Cannot seek to '.$position.' which is behind offset '.$this->offset.' plus count '.$this->count);
}
2003-11-18 22:18:38 +00:00
if ($this->it instanceof SeekableIterator) {
2003-12-06 19:03:17 +00:00
$this->it->seek($position);
$this->pos = $position;
} else {
while($this->pos < $position && $this->it->valid()) {
$this->next();
}
}
}
2003-12-06 19:03:17 +00:00
2004-10-29 20:58:58 +00:00
/** Rewind to offset specified in constructor
*/
2003-12-06 19:03:17 +00:00
function rewind()
{
$this->it->rewind();
$this->pos = 0;
$this->seek($this->offset);
}
2004-10-29 20:58:58 +00:00
/** @return whether iterator is valid
*/
function valid() {
2003-12-06 19:03:17 +00:00
return ($this->count == -1 || $this->pos < $this->offset + $this->count)
&& $this->it->valid();
}
2004-10-29 20:58:58 +00:00
/** @return current key
*/
function key() {
return $this->it->key();
}
2004-10-29 20:58:58 +00:00
/** @return current element
*/
function current() {
return $this->it->current();
}
2004-10-29 20:58:58 +00:00
/** Forward to nect element
*/
function next() {
$this->it->next();
2003-12-06 19:03:17 +00:00
$this->pos++;
}
2004-10-29 20:58:58 +00:00
/** @return current position relative to zero (not to offset specified in
* constructor).
*/
2003-12-06 19:03:17 +00:00
function getPosition() {
return $this->pos;
}
2004-10-29 20:12:57 +00:00
/**
* @return The inner iterator
*/
function getInnerIterator()
{
return $this->it;
}
2004-10-31 19:05:37 +00:00
/** Aggregate the inner iterator
*
* @param func Name of method to invoke
* @param params Array of parameters to pass to method
*/
function __call($func, $params)
{
return call_user_func_array(array($this->it, $func), $params);
}
}
?>