Skip to content
Permalink
Browse files

Implementing Collection::compile() using BufferedIterator

  • Loading branch information...
lorenzo committed Sep 21, 2014
1 parent f979377 commit 0832a0dd2c6024e13e7165647dcec31abb302fac
@@ -17,6 +17,7 @@
use AppendIterator;
use ArrayObject;
use Cake\Collection\Collection;
use Cake\Collection\Iterator\BufferedIterator;
use Cake\Collection\Iterator\ExtractIterator;
use Cake\Collection\Iterator\FilterIterator;
use Cake\Collection\Iterator\InsertIterator;
@@ -879,14 +880,10 @@ public function jsonSerialize() {
* You can think of this method as a way to create save points for complex
* calculations in a collection.
*
* @param bool $preserveKeys whether to use the keys returned by this
* collection as the array keys. Keep in mind that it is valid for iterators
* to return the same key for different elements, setting this value to false
* can help getting all items if keys are not important in the result.
* @return \Cake\Collection\Collection
*/
public function compile($preserveKeys = true) {
return new Collection($this->toArray($preserveKeys));
public function compile() {
return new BufferedIterator($this);
}
/**
@@ -40,17 +40,23 @@ class BufferedIterator extends Collection {
/**
* Last record fetched from the inner iterator
*
* @var array
* @var mixed
*/
protected $_current;
/**
* Last key obtained from the inner iterator
*
* @var array
* @var mixed
*/
protected $_key;
/**
* Whether or not the internal iterator's rewind method was already
* called
*
* @var boolean
*/
protected $_started = false;
/**
@@ -64,14 +70,17 @@ public function __construct($items) {
parent::__construct($items);
}
/**
* Returns the current key in the iterator
*
* @return array|object
*/
public function key() {
return $this->_key;
}
/**
* Returns the current record in the result iterator
*
* Part of Iterator interface.
* Returns the current record in the iterator
*
* @return array|object
*/
@@ -82,7 +91,6 @@ public function current() {
/**
* Rewinds the collection
*
*
* @return void
*/
public function rewind() {
@@ -96,8 +104,9 @@ public function rewind() {
}
/**
* Returns whether or not the iterator has more elements
*
* @return mixed
* @return boolean
*/
public function valid() {
if ($this->_buffer->offsetExists($this->_index)) {
@@ -113,14 +122,19 @@ public function valid() {
$this->_current = parent::current();
$this->_key = parent::key();
$this->_buffer->add($this->_index, [
'key' => $this->_index,
'key' => $this->_key,
'value' => $this->_current
]);
}
return $valid;
}
/**
* Advances the iterator pointer to the next element
*
* @return void
*/
public function next() {
$this->_index++;
parent::next();
@@ -18,6 +18,9 @@
use Countable;
use JsonSerializable;
use Serializable;
use IteratorIterator;
use Cake\ORM\ResultSet;
use Cake\Collection\Iterator\BufferedIterator;
/**
* Generic ResultSet decorator. This will make any traversable object appear to

0 comments on commit 0832a0d

Please sign in to comment.
You can’t perform that action at this time.