vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/Iterator/HydratingIterator.php line 67

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\ODM\MongoDB\Iterator;
  4. use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
  5. use Doctrine\ODM\MongoDB\UnitOfWork;
  6. use Generator;
  7. use Iterator;
  8. use ReturnTypeWillChange;
  9. use RuntimeException;
  10. use Traversable;
  11. /**
  12.  * Iterator that wraps a traversable and hydrates results into objects
  13.  *
  14.  * @internal
  15.  *
  16.  * @psalm-import-type Hints from UnitOfWork
  17.  *
  18.  * @template TValue
  19.  * @template TDocument of object
  20.  * @template-implements Iterator<TValue>
  21.  */
  22. final class HydratingIterator implements Iterator
  23. {
  24.     /** @var Generator<mixed, TValue>|null */
  25.     private $iterator;
  26.     /** @var UnitOfWork */
  27.     private $unitOfWork;
  28.     /** @var ClassMetadata<TDocument> */
  29.     private $class;
  30.     /**
  31.      * @var array<int, mixed>
  32.      * @psalm-var Hints
  33.      */
  34.     private $unitOfWorkHints;
  35.     /**
  36.      * @param Traversable<mixed, TValue> $traversable
  37.      * @param ClassMetadata<TDocument>   $class
  38.      * @psalm-param Hints $unitOfWorkHints
  39.      */
  40.     public function __construct(Traversable $traversableUnitOfWork $unitOfWorkClassMetadata $class, array $unitOfWorkHints = [])
  41.     {
  42.         $this->iterator        $this->wrapTraversable($traversable);
  43.         $this->unitOfWork      $unitOfWork;
  44.         $this->class           $class;
  45.         $this->unitOfWorkHints $unitOfWorkHints;
  46.     }
  47.     public function __destruct()
  48.     {
  49.         $this->iterator null;
  50.     }
  51.     /**
  52.      * @return TDocument|null
  53.      */
  54.     #[ReturnTypeWillChange]
  55.     public function current()
  56.     {
  57.         return $this->hydrate($this->getIterator()->current());
  58.     }
  59.     /**
  60.      * @return mixed
  61.      */
  62.     #[ReturnTypeWillChange]
  63.     public function key()
  64.     {
  65.         return $this->getIterator()->key();
  66.     }
  67.     /**
  68.      * @see http://php.net/iterator.next
  69.      */
  70.     public function next(): void
  71.     {
  72.         $this->getIterator()->next();
  73.     }
  74.     /**
  75.      * @see http://php.net/iterator.rewind
  76.      */
  77.     public function rewind(): void
  78.     {
  79.         $this->getIterator()->rewind();
  80.     }
  81.     /**
  82.      * @see http://php.net/iterator.valid
  83.      */
  84.     public function valid(): bool
  85.     {
  86.         return $this->key() !== null;
  87.     }
  88.     /**
  89.      * @return Generator<mixed, TValue>
  90.      */
  91.     private function getIterator(): Generator
  92.     {
  93.         if ($this->iterator === null) {
  94.             throw new RuntimeException('Iterator has already been destroyed');
  95.         }
  96.         return $this->iterator;
  97.     }
  98.     /**
  99.      * @param array<string, mixed>|null $document
  100.      *
  101.      * @return TDocument|null
  102.      */
  103.     private function hydrate(?array $document): ?object
  104.     {
  105.         return $document !== null $this->unitOfWork->getOrCreateDocument($this->class->name$document$this->unitOfWorkHints) : null;
  106.     }
  107.     /**
  108.      * @param Traversable<mixed, TValue> $traversable
  109.      *
  110.      * @return Generator<mixed, TValue>
  111.      */
  112.     private function wrapTraversable(Traversable $traversable): Generator
  113.     {
  114.         foreach ($traversable as $key => $value) {
  115.             yield $key => $value;
  116.         }
  117.     }
  118. }