function testLimitIterator() { $this->assertEquals(3, $this->_domainObjectCollection->getTotalRecordCount()); $limitIT = new LimitIterator($this->_domainObjectCollection, 1, 2); $limitIT->rewind(); $this->assertEquals($this->_domObject2, $limitIT->current()); $limitIT->next(); $this->assertEquals($this->_domObject3, $limitIT->current()); }
/** * Find one file and return. * * @param string $path The directory path. * @param mixed $condition Finding condition, that can be a string, a regex or a callback function. * Callback example: * <code> * function($current, $key, $iterator) * { * return @preg_match('^Foo', $current->getFilename()) && ! $iterator->isDot(); * } * </code> * @param boolean $recursive True to resursive. * * @return \SplFileInfo Finded file info object. * * @since 2.0 */ public static function findOne($path, $condition, $recursive = false) { $iterator = new \LimitIterator(static::find($path, $condition, $recursive), 0, 1); $iterator->rewind(); return $iterator->current(); }
<?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);
} } 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===
/** * Find one file and return. * * @param mixed $condition Finding condition, that can be a string, a regex or a callback function. * Callback example: * <code> * function($current, $key, $iterator) * { * return @preg_match('^Foo', $current->getFilename()) && ! $iterator->isDot(); * } * </code> * @param boolean $recursive True to resursive. * * @return \SplFileInfo Finded file info object. * * @since 1.0 */ public function find($condition, $recursive = false) { $iterator = new \LimitIterator($this->findAll($condition, $recursive), 0, 1); $iterator->rewind(); return $iterator->current(); }
public function getLatestEpisode() { $iterator = new \LimitIterator(new PastEpisodeFilterIterator(new \ArrayIterator($this->buildEpisodesList())), 0, 1); $iterator->rewind(); return $iterator->current(); }
<?php $xmlstring = <<<XML <?xml version = "1.0" encoding="windows-1251"?> <person> \t<name>Иван</name> \t<name>Вася</name> \t<name>Петя</name> \t<name>Джон</name> \t<name>Майк</name> \t<name>Лена</name> \t<name>Маша</name> \t<name>Даша</name> </person> XML; $offset = 3; $limit = 2; $it = new LimitIterator(new SimpleXMLIterator($xmlstring), $offset, $limit); foreach ($it as $r) { echo $it->key() . ' -- ' . $it->current() . '<br />'; }
function current() { return call_user_func($this->options['itemClass'] . "::getByID", parent::current()); }