Unverified Commit 98fcbeab authored by Andreas Braun's avatar Andreas Braun

Merge pull request #696

parents 32cfb9bb 964954e4
...@@ -18,8 +18,8 @@ ...@@ -18,8 +18,8 @@
namespace MongoDB\Model; namespace MongoDB\Model;
use Countable; use Countable;
use Generator;
use Iterator; use Iterator;
use IteratorIterator;
use Traversable; use Traversable;
use function count; use function count;
use function current; use function current;
...@@ -41,7 +41,7 @@ class CachingIterator implements Countable, Iterator ...@@ -41,7 +41,7 @@ class CachingIterator implements Countable, Iterator
/** @var array */ /** @var array */
private $items = []; private $items = [];
/** @var Generator */ /** @var IteratorIterator */
private $iterator; private $iterator;
/** @var boolean */ /** @var boolean */
...@@ -61,7 +61,9 @@ class CachingIterator implements Countable, Iterator ...@@ -61,7 +61,9 @@ class CachingIterator implements Countable, Iterator
*/ */
public function __construct(Traversable $traversable) public function __construct(Traversable $traversable)
{ {
$this->iterator = $this->wrapTraversable($traversable); $this->iterator = new IteratorIterator($traversable);
$this->iterator->rewind();
$this->storeCurrentItem(); $this->storeCurrentItem();
} }
...@@ -101,8 +103,12 @@ class CachingIterator implements Countable, Iterator ...@@ -101,8 +103,12 @@ class CachingIterator implements Countable, Iterator
public function next() public function next()
{ {
if (! $this->iteratorExhausted) { if (! $this->iteratorExhausted) {
$this->iteratorAdvanced = true;
$this->iterator->next(); $this->iterator->next();
$this->storeCurrentItem(); $this->storeCurrentItem();
$this->iteratorExhausted = ! $this->iterator->valid();
} }
next($this->items); next($this->items);
...@@ -156,20 +162,4 @@ class CachingIterator implements Countable, Iterator ...@@ -156,20 +162,4 @@ class CachingIterator implements Countable, Iterator
$this->items[$key] = $this->iterator->current(); $this->items[$key] = $this->iterator->current();
} }
/**
* Wraps the Traversable with a Generator.
*
* @param Traversable $traversable
* @return Generator
*/
private function wrapTraversable(Traversable $traversable)
{
foreach ($traversable as $key => $value) {
yield $key => $value;
$this->iteratorAdvanced = true;
}
$this->iteratorExhausted = true;
}
} }
...@@ -53,7 +53,7 @@ class CachingIteratorTest extends TestCase ...@@ -53,7 +53,7 @@ class CachingIteratorTest extends TestCase
public function testPartialIterationDoesNotExhaust() public function testPartialIterationDoesNotExhaust()
{ {
$traversable = $this->getTraversableThatThrows([1, 2, new Exception()]); $traversable = $this->getTraversable([1, 2, new Exception()]);
$iterator = new CachingIterator($traversable); $iterator = new CachingIterator($traversable);
$expectedKey = 0; $expectedKey = 0;
...@@ -110,13 +110,6 @@ class CachingIteratorTest extends TestCase ...@@ -110,13 +110,6 @@ class CachingIteratorTest extends TestCase
} }
private function getTraversable($items) private function getTraversable($items)
{
foreach ($items as $item) {
yield $item;
}
}
private function getTraversableThatThrows($items)
{ {
foreach ($items as $item) { foreach ($items as $item) {
if ($item instanceof Exception) { if ($item instanceof Exception) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment