Last active
August 29, 2015 14:05
-
-
Save teslitsky/aed6def1c30726755e95 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/php | |
| <?php | |
| require_once __DIR__ . '/PartialIterator.php'; | |
| $array = array( | |
| '1element', | |
| '2element', | |
| '3element', | |
| '4element', | |
| '5element', | |
| '6element', | |
| '7element', | |
| ); | |
| $obj = new \ArrayObject($array); | |
| $it = $obj->getIterator(); | |
| $partial = new PartialIterator($it, 3); | |
| var_dump('1'); | |
| foreach ($partial as $iterator) | |
| { | |
| var_dump($iterator); | |
| } | |
| $partial = new PartialIterator($it, 3); | |
| var_dump('2'); | |
| foreach ($partial as $iterator) | |
| { | |
| var_dump($iterator); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Итератор возвращает набор значений начального итератора | |
| */ | |
| class PartialIterator implements \Iterator | |
| { | |
| /** | |
| * @var int Текущая позиция итератора | |
| */ | |
| private $position = 0; | |
| /** | |
| * @var \Iterator Начальный итератор | |
| */ | |
| private $iterator; | |
| /** | |
| * @var int Максимальное количество возвращаемых значений | |
| */ | |
| private $maxItems; | |
| /** | |
| * @var bool Флаг вызова rewind() | |
| */ | |
| private $isRewindAlready = false; | |
| /** | |
| * @param \Iterator $iterator Начальный итератор | |
| * @param int $maxItems Максимальное количество возвращаемых значений | |
| */ | |
| public function __construct(\Iterator $iterator, $maxItems = 3) | |
| { | |
| $this->iterator = $iterator; | |
| $this->maxItems = $maxItems; | |
| } | |
| /** | |
| * (PHP 5 >= 5.0.0)<br/> | |
| * Return the current element | |
| * @link http://php.net/manual/en/iterator.current.php | |
| * @return mixed Can return any type. | |
| */ | |
| public function current() | |
| { | |
| return $this->iterator->current(); | |
| } | |
| /** | |
| * (PHP 5 >= 5.0.0)<br/> | |
| * Move forward to next element | |
| * @link http://php.net/manual/en/iterator.next.php | |
| * @return void Any returned value is ignored. | |
| */ | |
| public function next() | |
| { | |
| $this->iterator->next(); | |
| ++$this->position; | |
| } | |
| /** | |
| * (PHP 5 >= 5.0.0)<br/> | |
| * Return the key of the current element | |
| * @link http://php.net/manual/en/iterator.key.php | |
| * @return mixed scalar on success, or null on failure. | |
| */ | |
| public function key() | |
| { | |
| return $this->position; | |
| } | |
| /** | |
| * (PHP 5 >= 5.0.0)<br/> | |
| * Checks if current position is valid | |
| * @link http://php.net/manual/en/iterator.valid.php | |
| * @return boolean The return value will be casted to boolean and then evaluated. | |
| * Returns true on success or false on failure. | |
| */ | |
| public function valid() | |
| { | |
| return $this->position < $this->maxItems && $this->iterator->valid(); | |
| } | |
| /** | |
| * (PHP 5 >= 5.0.0)<br/> | |
| * Rewind the Iterator to the first element | |
| * @link http://php.net/manual/en/iterator.rewind.php | |
| * @throws LogicException | |
| * @return void Any returned value is ignored. | |
| */ | |
| public function rewind() | |
| { | |
| if ($this->isRewindAlready) | |
| { | |
| throw new \LogicException("You can't call rewind twice"); | |
| } | |
| $this->isRewindAlready = true; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment