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

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