{ echo __METHOD__ . "\n"; return $this->i; } public function current() { echo __METHOD__ . "\n"; return $this->a[$this->i]; } public function next() { echo __METHOD__ . "\n"; $this->i++; } } $it = new LimitIterator(new NumericArrayIterator(array(12, 25, 42, 56))); foreach ($it as $k => $v) { var_dump($k); var_dump($v); } echo "===SEEK===\n"; $it->seek(2); echo "===LOOP===\n"; foreach (new NoRewindIterator($it) as $k => $v) { var_dump($k); var_dump($v); } ?> ===DONE=== <?php exit(0);
} } class SeekableNumericArrayIterator extends NumericArrayIterator implements SeekableIterator { public function seek($index) { if ($index < count($this->a)) { $this->i = $index; } echo __METHOD__ . '(' . $index . ")\n"; } } $a = array(1, 2, 3, 4, 5); foreach (new LimitIterator(new NumericArrayIterator($a), 1, 3) as $v) { print "{$v}\n"; } echo "===SEEKABLE===\n"; $a = array(1, 2, 3, 4, 5); foreach (new LimitIterator(new SeekableNumericArrayIterator($a), 1, 3) as $v) { print "{$v}\n"; } echo "===SEEKING===\n"; $a = array(1, 2, 3, 4, 5); $l = new LimitIterator(new SeekableNumericArrayIterator($a)); for ($i = 1; $i < 4; $i++) { $l->seek($i); print $l->current() . "\n"; } ?> ===DONE===
<?php $it = new LimitIterator(new ArrayIterator(array(1, 2, 3, 4)), 1, 2); foreach ($it as $k => $v) { echo "{$k}=>{$v}\n"; var_dump($it->getPosition()); } try { $it->seek(0); } catch (OutOfBoundsException $e) { echo $e->getMessage() . "\n"; } $it->seek(2); var_dump($it->current()); try { $it->seek(3); } catch (OutOfBoundsException $e) { echo $e->getMessage() . "\n"; } $it->next(); var_dump($it->valid()); ?> ===DONE=== <?php exit(0);
<?php $a = array(1, 2, 3); $lt = new LimitIterator(new ArrayIterator($a)); $lt->seek(1, 1); // Should throw a warning as seek expects only 1 argument