Back to index

php5  5.3.10
Public Member Functions | Private Attributes
LimitIterator Class Reference

Limited Iteration over another Iterator. More...

Inheritance diagram for LimitIterator:
Inheritance graph
[legend]
Collaboration diagram for LimitIterator:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 __construct (Iterator $it, $offset=0, $count=-1)
 Construct.
 seek ($position)
 Seek to specified position.
 rewind ()
 Rewind to offset specified in constructor.
 valid ()
 key ()
 current ()
 next ()
 Forward to nect element.
 getPosition ()
 getInnerIterator ()
 __call ($func, $params)
 Aggregate the inner iterator.

Private Attributes

 $it
 $offset
 $count
 $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.

       {
              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);
       }
Returns:
current element

Implements Iterator.

Definition at line 97 of file limititerator.inc.

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

Implements OuterIterator.

Definition at line 118 of file limititerator.inc.

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

Definition at line 111 of file limititerator.inc.

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

Implements Iterator.

Definition at line 91 of file limititerator.inc.

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

Forward to nect element.

Implements Iterator.

Definition at line 103 of file limititerator.inc.

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

Here is the caller graph for this function:

Rewind to offset specified in constructor.

Implements Iterator.

Definition at line 75 of file limititerator.inc.

       {
              $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.

                                {
              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:

Here is the caller graph for this function:

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

Definition at line 28 of file limititerator.inc.

LimitIterator::$it [private]

Definition at line 26 of file limititerator.inc.

Definition at line 27 of file limititerator.inc.

Definition at line 29 of file limititerator.inc.


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