Skip to content

Commit

Permalink
Implementing Collection::compile() using BufferedIterator
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Sep 21, 2014
1 parent f979377 commit 0832a0d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
9 changes: 3 additions & 6 deletions src/Collection/CollectionTrait.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand Down
30 changes: 22 additions & 8 deletions src/Collection/Iterator/BufferedIterator.php
Expand Up @@ -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;

/**
Expand All @@ -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
*/
Expand All @@ -82,7 +91,6 @@ public function current() {
/**
* Rewinds the collection
*
*
* @return void
*/
public function rewind() {
Expand All @@ -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)) {
Expand All @@ -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();
Expand Down
3 changes: 3 additions & 0 deletions src/Datasource/ResultSetDecorator.php
Expand Up @@ -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
Expand Down

0 comments on commit 0832a0d

Please sign in to comment.