File tree Expand file tree Collapse file tree 2 files changed +10
-27
lines changed Expand file tree Collapse file tree 2 files changed +10
-27
lines changed Original file line number Diff line number Diff line change 18
18
namespace MongoDB \Model ;
19
19
20
20
use Countable ;
21
- use Generator ;
22
21
use Iterator ;
22
+ use IteratorIterator ;
23
23
use Traversable ;
24
24
use function count ;
25
25
use function current ;
@@ -41,7 +41,7 @@ class CachingIterator implements Countable, Iterator
41
41
/** @var array */
42
42
private $ items = [];
43
43
44
- /** @var Generator */
44
+ /** @var IteratorIterator */
45
45
private $ iterator ;
46
46
47
47
/** @var boolean */
@@ -61,7 +61,9 @@ class CachingIterator implements Countable, Iterator
61
61
*/
62
62
public function __construct (Traversable $ traversable )
63
63
{
64
- $ this ->iterator = $ this ->wrapTraversable ($ traversable );
64
+ $ this ->iterator = new IteratorIterator ($ traversable );
65
+
66
+ $ this ->iterator ->rewind ();
65
67
$ this ->storeCurrentItem ();
66
68
}
67
69
@@ -101,8 +103,12 @@ public function key()
101
103
public function next ()
102
104
{
103
105
if (! $ this ->iteratorExhausted ) {
106
+ $ this ->iteratorAdvanced = true ;
104
107
$ this ->iterator ->next ();
108
+
105
109
$ this ->storeCurrentItem ();
110
+
111
+ $ this ->iteratorExhausted = ! $ this ->iterator ->valid ();
106
112
}
107
113
108
114
next ($ this ->items );
@@ -156,20 +162,4 @@ private function storeCurrentItem()
156
162
157
163
$ this ->items [$ key ] = $ this ->iterator ->current ();
158
164
}
159
-
160
- /**
161
- * Wraps the Traversable with a Generator.
162
- *
163
- * @param Traversable $traversable
164
- * @return Generator
165
- */
166
- private function wrapTraversable (Traversable $ traversable )
167
- {
168
- foreach ($ traversable as $ key => $ value ) {
169
- yield $ key => $ value ;
170
- $ this ->iteratorAdvanced = true ;
171
- }
172
-
173
- $ this ->iteratorExhausted = true ;
174
- }
175
165
}
Original file line number Diff line number Diff line change @@ -53,7 +53,7 @@ public function testIterationWithEmptySet()
53
53
54
54
public function testPartialIterationDoesNotExhaust ()
55
55
{
56
- $ traversable = $ this ->getTraversableThatThrows ([1 , 2 , new Exception ()]);
56
+ $ traversable = $ this ->getTraversable ([1 , 2 , new Exception ()]);
57
57
$ iterator = new CachingIterator ($ traversable );
58
58
59
59
$ expectedKey = 0 ;
@@ -110,13 +110,6 @@ public function testCountWithEmptySet()
110
110
}
111
111
112
112
private function getTraversable ($ items )
113
- {
114
- foreach ($ items as $ item ) {
115
- yield $ item ;
116
- }
117
- }
118
-
119
- private function getTraversableThatThrows ($ items )
120
113
{
121
114
foreach ($ items as $ item ) {
122
115
if ($ item instanceof Exception) {
You can’t perform that action at this time.
0 commit comments