SPL-StandardPHPLibrary
Public Member Functions | Private Attributes | List of all members
LimitIterator Class Reference
Inheritance diagram for LimitIterator:
Inheritance graph
Collaboration diagram for LimitIterator:
Collaboration graph

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.

38  {
39  if ($offset < 0) {
40  throw new exception('Parameter offset must be > 0');
41  }
42  if ($count < 0 && $count != -1) {
43  throw new exception('Parameter count must either be -1 or a value greater than or equal to 0');
44  }
45  $this->it = $it;
46  $this->offset = $offset;
47  $this->count = $count;
48  $this->pos = 0;
49  }

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.

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

Implements Iterator.

Definition at line 97 of file limititerator.inc.

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

Implements OuterIterator.

Definition at line 118 of file limititerator.inc.

References $it.

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

Definition at line 111 of file limititerator.inc.

References $pos.

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

Implements Iterator.

Definition at line 91 of file limititerator.inc.

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

Forward to nect element.

Implements Iterator.

Definition at line 103 of file limititerator.inc.

Referenced by seek().

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

Rewind to offset specified in constructor.

Implements Iterator.

Definition at line 75 of file limititerator.inc.

References seek().

76  {
77  $this->it->rewind();
78  $this->pos = 0;
79  $this->seek($this->offset);
80  }
seek($position)
Seek to specified position.

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().

56  {
57  if ($position < $this->offset) {
58  throw new exception('Cannot seek to '.$position.' which is below offset '.$this->offset);
59  }
60  if ($position > $this->offset + $this->count && $this->count != -1) {
61  throw new exception('Cannot seek to '.$position.' which is behind offset '.$this->offset.' plus count '.$this->count);
62  }
63  if ($this->it instanceof SeekableIterator) {
64  $this->it->seek($position);
65  $this->pos = $position;
66  } else {
67  while($this->pos < $position && $this->it->valid()) {
68  $this->next();
69  }
70  }
71  }
next()
Forward to nect element.
seekable iterator

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.

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

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(), and getInnerIterator().

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.

Referenced by getPosition().


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