iterator = $this->wrapTraversable($traversable); $this->storeCurrentItem(); } /** * @see http://php.net/countable.count * @return integer */ public function count() { $this->exhaustIterator(); return count($this->items); } /** * @see http://php.net/iterator.current * @return mixed */ public function current() { return current($this->items); } /** * @see http://php.net/iterator.key * @return mixed */ public function key() { return key($this->items); } /** * @see http://php.net/iterator.next * @return void */ public function next() { if ( ! $this->iteratorExhausted) { $this->iterator->next(); $this->storeCurrentItem(); } next($this->items); } /** * @see http://php.net/iterator.rewind * @return void */ public function rewind() { /* If the iterator has advanced, exhaust it now so that future iteration * can rely on the cache. */ if ($this->iteratorAdvanced) { $this->exhaustIterator(); } reset($this->items); } /** * @see http://php.net/iterator.valid * @return boolean */ public function valid() { return $this->key() !== null; } /** * Ensures that the inner iterator is fully consumed and cached. */ private function exhaustIterator() { while ( ! $this->iteratorExhausted) { $this->next(); } } /** * Stores the current item in the cache. */ private function storeCurrentItem() { $key = $this->iterator->key(); if ($key === null) { return; } $this->items[$key] = $this->iterator->current(); } /** * Wraps the Traversable with a Generator. * * @param Traversable $traversable * @return Generator */ private function wrapTraversable(Traversable $traversable) { foreach ($traversable as $key => $value) { yield $key => $value; $this->iteratorAdvanced = true; } $this->iteratorExhausted = true; } }