Browse Source

Implementing Collection::compile() using BufferedIterator

Jose Lorenzo Rodriguez 11 years ago
parent
commit
0832a0dd2c

+ 3 - 6
src/Collection/CollectionTrait.php

@@ -17,6 +17,7 @@ namespace Cake\Collection;
 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 @@ trait CollectionTrait {
  * 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);
 	}
 
 /**

+ 22 - 8
src/Collection/Iterator/BufferedIterator.php

@@ -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 @@ class BufferedIterator extends Collection {
 		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 @@ class BufferedIterator extends Collection {
 /**
  * Rewinds the collection
  *
- *
  * @return void
  */
 	public function rewind() {
@@ -96,8 +104,9 @@ class BufferedIterator extends Collection {
 	}
 
 /**
+ * Returns whether or not the iterator has more elements
  *
- * @return mixed
+ * @return boolean
  */
 	public function valid() {
 		if ($this->_buffer->offsetExists($this->_index)) {
@@ -113,7 +122,7 @@ class BufferedIterator extends Collection {
 			$this->_current = parent::current();
 			$this->_key = parent::key();
 			$this->_buffer->add($this->_index, [
-				'key' => $this->_index,
+				'key' => $this->_key,
 				'value' => $this->_current
 			]);
 		}
@@ -121,6 +130,11 @@ class BufferedIterator extends Collection {
 		return $valid;
 	}
 
+/**
+ * Advances the iterator pointer to the next element
+ *
+ * @return void
+ */
 	public function next() {
 		$this->_index++;
 		parent::next();

+ 3 - 0
src/Datasource/ResultSetDecorator.php

@@ -18,6 +18,9 @@ use Cake\Collection\Collection;
 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