SPL-StandardPHPLibrary
Public Member Functions | Private Attributes
LimitIterator Class Reference
Inheritance diagram for LimitIterator:
Inheritance graph
[legend]
Collaboration diagram for LimitIterator:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 __call ($func, $params)
 __construct (Iterator $it, $offset=0, $count=-1)
 current ()
 getInnerIterator ()
 getPosition ()
 key ()
 next ()
 rewind ()
 seek ($position)
 valid ()

Private Attributes

 $count
 $it
 $offset
 $pos

Detailed Description

Limited Iteration over another Iterator.

Author:
Marcus Boerger
Version:
1.1
Since:
PHP 5.0

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.

Definition at line 24 of file limititerator.inc.


Constructor & Destructor Documentation

LimitIterator::__construct ( Iterator it,
offset = 0,
count = -1 
)

Construct.

Parameters:
itIterator to limit
offsetOffset to first element
countMaximum number of elements to show or -1 for all

Definition at line 37 of file limititerator.inc.

References $count, $it, and $offset.

    {
        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;
        $this->pos    = 0;
    }

Member Function Documentation

LimitIterator::__call ( func,
params 
)

Aggregate the inner iterator.

Parameters:
funcName of method to invoke
paramsArray of parameters to pass to method

Definition at line 128 of file limititerator.inc.

    {
        return call_user_func_array(array($this->it, $func), $params);
    }
LimitIterator::current ( )
Returns:
current element

Implements Iterator.

Definition at line 97 of file limititerator.inc.

                       {
        return $this->it->current();
    }
LimitIterator::getInnerIterator ( )
Returns:
The inner iterator

Implements OuterIterator.

Definition at line 118 of file limititerator.inc.

    {
        return $this->it;
    }
LimitIterator::getPosition ( )
Returns:
current position relative to zero (not to offset specified in constructor).

Definition at line 111 of file limititerator.inc.

                           {
        return $this->pos;
    }
LimitIterator::key ( )
Returns:
current key

Implements Iterator.

Definition at line 91 of file limititerator.inc.

                   {
        return $this->it->key();
    }
LimitIterator::next ( )

Forward to nect element.

Implements Iterator.

Definition at line 103 of file limititerator.inc.

Referenced by seek().

                    {
        $this->it->next();
        $this->pos++;
    }
LimitIterator::rewind ( )

Rewind to offset specified in constructor.

Implements Iterator.

Definition at line 75 of file limititerator.inc.

References seek().

    {
        $this->it->rewind();
        $this->pos = 0;
        $this->seek($this->offset);
    }

Here is the call graph for this function:

LimitIterator::seek ( position)

Seek to specified position.

Parameters:
positionoffset to seek to (relative to beginning not offset specified in constructor).
Exceptions:
exceptionwhen position is invalid

Definition at line 56 of file limititerator.inc.

References next().

Referenced by rewind().

                             {
        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);
        }
        if ($this->it instanceof SeekableIterator) {
            $this->it->seek($position);
            $this->pos = $position;
        } else {
            while($this->pos < $position && $this->it->valid()) {
                $this->next();
            }
        }
    }

Here is the call graph for this function:

LimitIterator::valid ( )
Returns:
whether iterator is valid

Implements Iterator.

Definition at line 84 of file limititerator.inc.

                     {
        return ($this->count == -1 || $this->pos < $this->offset + $this->count)
             && $this->it->valid();
    }

Member Data Documentation

LimitIterator::$count [private]

Definition at line 28 of file limititerator.inc.

Referenced by __construct().

LimitIterator::$it [private]

Definition at line 26 of file limititerator.inc.

Referenced by __construct().

LimitIterator::$offset [private]

Definition at line 27 of file limititerator.inc.

Referenced by __construct().

LimitIterator::$pos [private]

Definition at line 29 of file limititerator.inc.


The documentation for this class was generated from the following file: