vendor/sulu/sulu/src/Sulu/Component/DocumentManager/Query/Query.php line 121

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\Query;
  11. use PHPCR\Query\QueryInterface;
  12. use PHPCR\Query\QueryResultInterface;
  13. use Sulu\Component\DocumentManager\Event\QueryExecuteEvent;
  14. use Sulu\Component\DocumentManager\Events;
  15. use Sulu\Component\DocumentManager\Exception\DocumentManagerException;
  16. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  17. /**
  18.  * Based heavily on the PHPCR-ODM Query object.
  19.  *
  20.  * If we can break the phpcrQuery builder from the PHPCR-ODM we should
  21.  * also be able to break-out the PhpcrQuery object too:
  22.  *
  23.  * https://github.com/doctrine/phpcr-odm/issues/627
  24.  */
  25. class Query
  26. {
  27.     public const HYDRATE_DOCUMENT 'document';
  28.     public const HYDRATE_PHPCR 'phpcr_node';
  29.     /**
  30.      * @var QueryInterface
  31.      */
  32.     private $phpcrQuery;
  33.     /**
  34.      * @var EventDispatcherInterface
  35.      */
  36.     private $dispatcher;
  37.     /**
  38.      * @var null|string
  39.      */
  40.     private $primarySelector;
  41.     /**
  42.      * @var null|string
  43.      */
  44.     private $locale;
  45.     /**
  46.      * @var array
  47.      */
  48.     private $options;
  49.     /**
  50.      * @var int
  51.      */
  52.     private $maxResults;
  53.     /**
  54.      * @var int
  55.      */
  56.     private $firstResult;
  57.     /**
  58.      * @param null|string $locale
  59.      * @param null|string $primarySelector
  60.      */
  61.     public function __construct(
  62.         QueryInterface $phpcrQuery,
  63.         EventDispatcherInterface $dispatcher,
  64.         $locale null,
  65.         array $options = [],
  66.         $primarySelector null
  67.     ) {
  68.         $this->phpcrQuery $phpcrQuery;
  69.         $this->dispatcher $dispatcher;
  70.         $this->locale $locale;
  71.         $this->options $options;
  72.         $this->primarySelector $primarySelector;
  73.     }
  74.     /**
  75.      * @param string $hydrationMode
  76.      *
  77.      * @return mixed|QueryResultInterface
  78.      *
  79.      * @throws DocumentManagerException
  80.      */
  81.     public function execute(array $parameters = [], $hydrationMode self::HYDRATE_DOCUMENT)
  82.     {
  83.         if (null !== $this->maxResults) {
  84.             $this->phpcrQuery->setLimit($this->maxResults);
  85.         }
  86.         if (null !== $this->firstResult) {
  87.             $this->phpcrQuery->setOffset($this->firstResult);
  88.         }
  89.         foreach ($parameters as $key => $value) {
  90.             $this->phpcrQuery->bindValue($key$value);
  91.         }
  92.         if (self::HYDRATE_PHPCR === $hydrationMode) {
  93.             return $this->phpcrQuery->execute();
  94.         }
  95.         if (self::HYDRATE_DOCUMENT !== $hydrationMode) {
  96.             throw new DocumentManagerException(\sprintf(
  97.                 'Unknown hydration mode "%s", should be either "document" or "phpcr_node"',
  98.                 $hydrationMode
  99.             ));
  100.         }
  101.         $event = new QueryExecuteEvent($this$this->options);
  102.         $this->dispatcher->dispatch($eventEvents::QUERY_EXECUTE);
  103.         return $event->getResult();
  104.     }
  105.     /**
  106.      * @return int
  107.      */
  108.     public function getMaxResults()
  109.     {
  110.         return $this->maxResults;
  111.     }
  112.     /**
  113.      * @param int $maxResults
  114.      */
  115.     public function setMaxResults($maxResults)
  116.     {
  117.         $this->maxResults $maxResults;
  118.     }
  119.     /**
  120.      * @return int
  121.      */
  122.     public function getFirstResult()
  123.     {
  124.         return $this->firstResult;
  125.     }
  126.     /**
  127.      * @param int $firstResult
  128.      */
  129.     public function setFirstResult($firstResult)
  130.     {
  131.         $this->firstResult $firstResult;
  132.     }
  133.     /**
  134.      * @return null|string
  135.      */
  136.     public function getLocale()
  137.     {
  138.         return $this->locale;
  139.     }
  140.     /**
  141.      * @param string $locale
  142.      */
  143.     public function setLocale($locale)
  144.     {
  145.         $this->locale $locale;
  146.     }
  147.     /**
  148.      * @return null|string
  149.      */
  150.     public function getPrimarySelector()
  151.     {
  152.         return $this->primarySelector;
  153.     }
  154.     /**
  155.      * @param string $primarySelector
  156.      */
  157.     public function setPrimarySelector($primarySelector)
  158.     {
  159.         $this->primarySelector $primarySelector;
  160.     }
  161.     /**
  162.      * @return QueryInterface
  163.      */
  164.     public function getPhpcrQuery()
  165.     {
  166.         return $this->phpcrQuery;
  167.     }
  168. }