vendor/sulu/sulu/src/Sulu/Component/DocumentManager/Collection/QueryResultCollection.php line 91

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Sulu.
  4.  *
  5.  * (c) Sulu GmbH
  6.  *
  7.  * This source file is subject to the MIT license that is bundled
  8.  * with this source code in the file LICENSE.
  9.  */
  10. namespace Sulu\Component\DocumentManager\Collection;
  11. use PHPCR\Query\QueryResultInterface;
  12. use Sulu\Component\DocumentManager\Event\HydrateEvent;
  13. use Sulu\Component\DocumentManager\Events;
  14. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  15. /**
  16.  * Lazily hydrate query results.
  17.  */
  18. class QueryResultCollection extends AbstractLazyCollection
  19. {
  20.     /**
  21.      * @var EventDispatcherInterface
  22.      */
  23.     private $eventDispatcher;
  24.     /**
  25.      * @var QueryResultInterface
  26.      */
  27.     private $result;
  28.     /**
  29.      * @var string
  30.      */
  31.     private $locale;
  32.     /**
  33.      * @var array
  34.      */
  35.     private $options;
  36.     /**
  37.      * @var bool
  38.      */
  39.     private $initialized false;
  40.     /**
  41.      * @var null|string
  42.      */
  43.     private $primarySelector null;
  44.     /**
  45.      * @param string $locale
  46.      * @param array $options
  47.      * @param null|string $primarySelector
  48.      */
  49.     public function __construct(
  50.         QueryResultInterface $result,
  51.         EventDispatcherInterface $eventDispatcher,
  52.         $locale,
  53.         $options = [],
  54.         $primarySelector null
  55.     ) {
  56.         $this->result $result;
  57.         $this->eventDispatcher $eventDispatcher;
  58.         $this->locale $locale;
  59.         $this->options $options;
  60.         $this->primarySelector $primarySelector;
  61.     }
  62.     public function current()
  63.     {
  64.         $this->initialize();
  65.         $row $this->documents->current();
  66.         $node $row->getNode($this->primarySelector);
  67.         $hydrateEvent = new HydrateEvent($node$this->locale$this->options);
  68.         $this->eventDispatcher->dispatch($hydrateEventEvents::HYDRATE);
  69.         return $hydrateEvent->getDocument();
  70.     }
  71.     protected function initialize()
  72.     {
  73.         if (true === $this->initialized) {
  74.             return;
  75.         }
  76.         $this->documents $this->result->getRows();
  77.         $this->initialized true;
  78.     }
  79. }