diff options
Diffstat (limited to 'ext/spl/examples/limititerator.inc')
| -rwxr-xr-x | ext/spl/examples/limititerator.inc | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/ext/spl/examples/limititerator.inc b/ext/spl/examples/limititerator.inc new file mode 100755 index 0000000000..c1a19fedd9 --- /dev/null +++ b/ext/spl/examples/limititerator.inc @@ -0,0 +1,48 @@ +<?php + +class LimitIterator implements Iterator +{ + protected $it; + protected $offset; + protected $count; + protected $index; + + // negative offset is respected + // count === NULL means all + function __construct(Iterator $it, $offset = 0, $count = NULL) + { + $this->it = $it; + $this->offset = $offset; + $this->count = $count; + $this->index = 0; + } + + function rewind() + { + $this->it->rewind(); + $this->index = 0; + while($this->index < $this->offset && $this->it->hasMore()) { + $this->next(); + } + } + + function hasMore() { + return (is_null($this->count) || $this->index < $this->offset + $this->count) + && $this->it->hasMore(); + } + + function key() { + return $this->it->key(); + } + + function current() { + return $this->it->current(); + } + + function next() { + $this->it->next(); + $this->index++; + } +} + +?>
\ No newline at end of file |
