Skip to content

Commit 0832a0d

Browse files
committed
Implementing Collection::compile() using BufferedIterator
1 parent f979377 commit 0832a0d

File tree

3 files changed

+28
-14
lines changed

3 files changed

+28
-14
lines changed

src/Collection/CollectionTrait.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use AppendIterator;
1818
use ArrayObject;
1919
use Cake\Collection\Collection;
20+
use Cake\Collection\Iterator\BufferedIterator;
2021
use Cake\Collection\Iterator\ExtractIterator;
2122
use Cake\Collection\Iterator\FilterIterator;
2223
use Cake\Collection\Iterator\InsertIterator;
@@ -879,14 +880,10 @@ public function jsonSerialize() {
879880
* You can think of this method as a way to create save points for complex
880881
* calculations in a collection.
881882
*
882-
* @param bool $preserveKeys whether to use the keys returned by this
883-
* collection as the array keys. Keep in mind that it is valid for iterators
884-
* to return the same key for different elements, setting this value to false
885-
* can help getting all items if keys are not important in the result.
886883
* @return \Cake\Collection\Collection
887884
*/
888-
public function compile($preserveKeys = true) {
889-
return new Collection($this->toArray($preserveKeys));
885+
public function compile() {
886+
return new BufferedIterator($this);
890887
}
891888

892889
/**

src/Collection/Iterator/BufferedIterator.php

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,23 @@ class BufferedIterator extends Collection {
4040
/**
4141
* Last record fetched from the inner iterator
4242
*
43-
* @var array
43+
* @var mixed
4444
*/
4545
protected $_current;
4646

4747
/**
4848
* Last key obtained from the inner iterator
4949
*
50-
* @var array
50+
* @var mixed
5151
*/
5252
protected $_key;
5353

54+
/**
55+
* Whether or not the internal iterator's rewind method was already
56+
* called
57+
*
58+
* @var boolean
59+
*/
5460
protected $_started = false;
5561

5662
/**
@@ -64,14 +70,17 @@ public function __construct($items) {
6470
parent::__construct($items);
6571
}
6672

73+
/**
74+
* Returns the current key in the iterator
75+
*
76+
* @return array|object
77+
*/
6778
public function key() {
6879
return $this->_key;
6980
}
7081

7182
/**
72-
* Returns the current record in the result iterator
73-
*
74-
* Part of Iterator interface.
83+
* Returns the current record in the iterator
7584
*
7685
* @return array|object
7786
*/
@@ -82,7 +91,6 @@ public function current() {
8291
/**
8392
* Rewinds the collection
8493
*
85-
*
8694
* @return void
8795
*/
8896
public function rewind() {
@@ -96,8 +104,9 @@ public function rewind() {
96104
}
97105

98106
/**
107+
* Returns whether or not the iterator has more elements
99108
*
100-
* @return mixed
109+
* @return boolean
101110
*/
102111
public function valid() {
103112
if ($this->_buffer->offsetExists($this->_index)) {
@@ -113,14 +122,19 @@ public function valid() {
113122
$this->_current = parent::current();
114123
$this->_key = parent::key();
115124
$this->_buffer->add($this->_index, [
116-
'key' => $this->_index,
125+
'key' => $this->_key,
117126
'value' => $this->_current
118127
]);
119128
}
120129

121130
return $valid;
122131
}
123132

133+
/**
134+
* Advances the iterator pointer to the next element
135+
*
136+
* @return void
137+
*/
124138
public function next() {
125139
$this->_index++;
126140
parent::next();

src/Datasource/ResultSetDecorator.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
use Countable;
1919
use JsonSerializable;
2020
use Serializable;
21+
use IteratorIterator;
22+
use Cake\ORM\ResultSet;
23+
use Cake\Collection\Iterator\BufferedIterator;
2124

2225
/**
2326
* Generic ResultSet decorator. This will make any traversable object appear to

0 commit comments

Comments
 (0)