Skip to content

Commit

Permalink
Add implementation of Countable for CachingIterator
Browse files Browse the repository at this point in the history
  • Loading branch information
Branlute committed Jun 27, 2022
1 parent 8f968ba commit 5224b08
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/Doctrine/ODM/MongoDB/Iterator/CachingIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

namespace Doctrine\ODM\MongoDB\Iterator;

use Countable;
use Generator;
use ReturnTypeWillChange;
use RuntimeException;
use Traversable;

use function count;
use function current;
use function key;
use function next;
Expand All @@ -26,7 +28,7 @@
* @template TValue
* @template-implements Iterator<TValue>
*/
final class CachingIterator implements Iterator
final class CachingIterator implements Countable, Iterator
{
/** @var array<mixed, TValue> */
private $items = [];
Expand Down Expand Up @@ -55,6 +57,18 @@ public function __construct(Traversable $iterator)
$this->storeCurrentItem();
}

/**
* @see https://php.net/countable.count
* @return integer
*/
#[ReturnTypeWillChange]
public function count()
{
$this->exhaustIterator();

return count($this->items);
}

public function __destruct()
{
$this->iterator = null;
Expand Down

0 comments on commit 5224b08

Please sign in to comment.