_buffer = new SplDoublyLinkedList(); parent::__construct($items); } /** * Returns the current key in the iterator * * @return mixed */ public function key() { return $this->_key; } /** * Returns the current record in the iterator * * @return mixed */ public function current() { return $this->_current; } /** * Rewinds the collection * * @return void */ public function rewind() { if ($this->_index === 0 && !$this->_started) { $this->_started = true; parent::rewind(); return; } $this->_index = 0; } /** * Returns whether or not the iterator has more elements * * @return bool */ public function valid() { if ($this->_buffer->offsetExists($this->_index)) { $current = $this->_buffer->offsetGet($this->_index); $this->_current = $current['value']; $this->_key = $current['key']; return true; } $valid = parent::valid(); if ($valid) { $this->_current = parent::current(); $this->_key = parent::key(); $this->_buffer->push([ 'key' => $this->_key, 'value' => $this->_current, ]); } $this->_finished = !$valid; return $valid; } /** * Advances the iterator pointer to the next element * * @return void */ public function next() { $this->_index++; if (!$this->_finished) { parent::next(); } } /** * Returns the number or items in this collection * * @return int */ public function count() { if (!$this->_started) { $this->rewind(); } while ($this->valid()) { $this->next(); } return $this->_buffer->count(); } /** * Returns a string representation of this object that can be used * to reconstruct it * * @return string */ public function serialize() { if (!$this->_finished) { $this->count(); } return serialize($this->_buffer); } /** * Unserializes the passed string and rebuilds the BufferedIterator instance * * @param string $buffer The serialized buffer iterator * @return void */ public function unserialize($buffer) { $this->__construct([]); $this->_buffer = unserialize($buffer); $this->_started = true; $this->_finished = true; } }