LimitIterator Class Reference

Limited Iteration over another Iterator. More...

Inheritance diagram for LimitIterator:

Inheritance graph
{OuterIterator\n||+ getInnerIterator()\l}{Iterator\n||+ current()\l+ key()\l+ next()\l+ rewind()\l+ valid()\l}{Traversable\n||}
[legend]
Collaboration diagram for LimitIterator:

Collaboration graph
{OuterIterator\n||+ getInnerIterator()\l}{Iterator\n||+ current()\l+ key()\l+ next()\l+ rewind()\l+ valid()\l}{Traversable\n||}
[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:
it Iterator to limit
offset Offset to first element
count Maximum number of elements to show or -1 for all

Definition at line 37 of file limititerator.inc.

References $count, $it, and $offset.

00038     {
00039         if ($offset < 0) {
00040             throw new exception('Parameter offset must be > 0');
00041         }
00042         if ($count < 0 && $count != -1) {
00043             throw new exception('Parameter count must either be -1 or a value greater than or equal to 0');
00044         }
00045         $this->it     = $it;
00046         $this->offset = $offset;
00047         $this->count  = $count;
00048         $this->pos    = 0;
00049     }


Member Function Documentation

LimitIterator::__call ( func,
params 
)

Aggregate the inner iterator.

Parameters:
func Name of method to invoke
params Array of parameters to pass to method

Definition at line 128 of file limititerator.inc.

00129     {
00130         return call_user_func_array(array($this->it, $func), $params);
00131     }

LimitIterator::current (  ) 

Returns:
current element

Implements Iterator.

Definition at line 97 of file limititerator.inc.

00097                        {
00098         return $this->it->current();
00099     }

LimitIterator::getInnerIterator (  ) 

Returns:
The inner iterator

Implements OuterIterator.

Definition at line 118 of file limititerator.inc.

00119     {
00120         return $this->it;
00121     }

LimitIterator::getPosition (  ) 

Returns:
current position relative to zero (not to offset specified in constructor).

Definition at line 111 of file limititerator.inc.

00111                            {
00112         return $this->pos;
00113     }

LimitIterator::key (  ) 

Returns:
current key

Implements Iterator.

Definition at line 91 of file limititerator.inc.

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

LimitIterator::next (  ) 

Forward to nect element.

Implements Iterator.

Definition at line 103 of file limititerator.inc.

Referenced by seek().

00103                     {
00104         $this->it->next();
00105         $this->pos++;
00106     }

LimitIterator::rewind (  ) 

Rewind to offset specified in constructor.

Implements Iterator.

Definition at line 75 of file limititerator.inc.

References seek().

00076     {
00077         $this->it->rewind();
00078         $this->pos = 0;
00079         $this->seek($this->offset);
00080     }

Here is the call graph for this function:

LimitIterator::seekLimitIterator::next

LimitIterator::seek ( position  ) 

Seek to specified position.

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

Definition at line 56 of file limititerator.inc.

References next().

Referenced by rewind().

00056                              {
00057         if ($position < $this->offset) {
00058             throw new exception('Cannot seek to '.$position.' which is below offset '.$this->offset);
00059         }
00060         if ($position > $this->offset + $this->count && $this->count != -1) {
00061             throw new exception('Cannot seek to '.$position.' which is behind offset '.$this->offset.' plus count '.$this->count);
00062         }
00063         if ($this->it instanceof SeekableIterator) {
00064             $this->it->seek($position);
00065             $this->pos = $position;
00066         } else {
00067             while($this->pos < $position && $this->it->valid()) {
00068                 $this->next();
00069             }
00070         }
00071     }

Here is the call graph for this function:

LimitIterator::next

LimitIterator::valid (  ) 

Returns:
whether iterator is valid

Implements Iterator.

Definition at line 84 of file limititerator.inc.

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


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:
Generated on Thu Apr 26 01:06:20 2007 for SPL-StandardPHPLibrary by  doxygen 1.5.2