Skip to content

Instantly share code, notes, and snippets.

@hakre
Created December 11, 2012 20:49
Show Gist options
  • Save hakre/4262006 to your computer and use it in GitHub Desktop.
Save hakre/4262006 to your computer and use it in GitHub Desktop.
NoRewindLimitIterator
<?php
class NoRewindLimitIterator extends IteratorIterator
{
private $start, $count, $next;
public function __construct($it, $start, $count) {
parent::__construct($it);
$this->start = max(0, $start);
$this->count = max(0, $count);
}
public function rewind() {
$start = $this->start;
while($start--) {
parent::next();
}
$this->next = 0;
}
public function next() {
parent::next();
$this->next++;
}
public function valid() {
if (!parent::valid()) return false;
return $this->next < $this->count;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment