SPL-StandardPHPLibrary
limititerator.inc
Go to the documentation of this file.
1 <?php
2 
24 class LimitIterator implements OuterIterator
25 {
26  private $it;
27  private $offset;
28  private $count;
29  private $pos;
30 
37  function __construct(Iterator $it, $offset = 0, $count = -1)
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  }
50 
56  function seek($position) {
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  }
72 
75  function rewind()
76  {
77  $this->it->rewind();
78  $this->pos = 0;
79  $this->seek($this->offset);
80  }
81 
84  function valid() {
85  return ($this->count == -1 || $this->pos < $this->offset + $this->count)
86  && $this->it->valid();
87  }
88 
91  function key() {
92  return $this->it->key();
93  }
94 
97  function current() {
98  return $this->it->current();
99  }
100 
103  function next() {
104  $this->it->next();
105  $this->pos++;
106  }
107 
111  function getPosition() {
112  return $this->pos;
113  }
114 
118  function getInnerIterator()
119  {
120  return $this->it;
121  }
122 
128  function __call($func, $params)
129  {
130  return call_user_func_array(array($this->it, $func), $params);
131  }
132 }
133 
134 ?>
Limited Iteration over another Iterator.
next()
Forward to nect element.
Interface to access the current inner iteraor of iterator wrappers.
seekable iterator
rewind()
Rewind to offset specified in constructor.
seek($position)
Seek to specified position.
Basic iterator.
Definition: spl.php:549
__call($func, $params)
Aggregate the inner iterator.
__construct(Iterator $it, $offset=0, $count=-1)
Construct.