vendor/friendsofsymfony/elastica-bundle/src/Paginator/RawPaginatorAdapter.php line 177

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the FOSElasticaBundle package.
  4.  *
  5.  * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace FOS\ElasticaBundle\Paginator;
  11. use Elastica\Query;
  12. use Elastica\ResultSet;
  13. use Elastica\SearchableInterface;
  14. use InvalidArgumentException;
  15. /**
  16.  * Allows pagination of Elastica\Query. Does not map results.
  17.  */
  18. class RawPaginatorAdapter implements PaginatorAdapterInterface
  19. {
  20.     /**
  21.      * @var SearchableInterface the object to search in
  22.      */
  23.     private $searchable;
  24.     /**
  25.      * @var Query the query to search
  26.      */
  27.     private $query;
  28.     /**
  29.      * @var array search options
  30.      */
  31.     private $options;
  32.     /**
  33.      * @var int the number of hits
  34.      */
  35.     private $totalHits;
  36.     /**
  37.      * @var array for the aggregations
  38.      */
  39.     private $aggregations;
  40.     /**
  41.      * @var array for the suggesters
  42.      */
  43.     private $suggests;
  44.     /**
  45.      * @var float
  46.      */
  47.     private $maxScore;
  48.     /**
  49.      * @see PaginatorAdapterInterface::__construct
  50.      *
  51.      * @param SearchableInterface $searchable the object to search in
  52.      * @param Query               $query      the query to search
  53.      * @param array               $options
  54.      */
  55.     public function __construct(SearchableInterface $searchableQuery $query, array $options = [])
  56.     {
  57.         $this->searchable $searchable;
  58.         $this->query $query;
  59.         $this->options $options;
  60.     }
  61.     /**
  62.      * {@inheritdoc}
  63.      */
  64.     public function getResults($offset$itemCountPerPage)
  65.     {
  66.         return new RawPartialResults($this->getElasticaResults($offset$itemCountPerPage));
  67.     }
  68.     /**
  69.      * Returns the number of results.
  70.      *
  71.      * If genuineTotal is provided as true, total hits is returned from the
  72.      * hits.total value from the search results instead of just returning
  73.      * the requested size.
  74.      *
  75.      * {@inheritdoc}
  76.      */
  77.     public function getTotalHits($genuineTotal false)
  78.     {
  79.         if (!isset($this->totalHits)) {
  80.             $this->totalHits $this->searchable->count($this->query);
  81.         }
  82.         return $this->query->hasParam('size') && !$genuineTotal
  83.             min($this->totalHits, (int) $this->query->getParam('size'))
  84.             : $this->totalHits;
  85.     }
  86.     /**
  87.      * {@inheritdoc}
  88.      */
  89.     public function getAggregations()
  90.     {
  91.         if (!isset($this->aggregations)) {
  92.             $this->aggregations $this->searchable->search($this->query)->getAggregations();
  93.         }
  94.         return $this->aggregations;
  95.     }
  96.     /**
  97.      * {@inheritdoc}
  98.      */
  99.     public function getSuggests()
  100.     {
  101.         if (!isset($this->suggests)) {
  102.             $this->suggests $this->searchable->search($this->query)->getSuggests();
  103.         }
  104.         return $this->suggests;
  105.     }
  106.     /**
  107.      * @return float
  108.      */
  109.     public function getMaxScore()
  110.     {
  111.         if (!isset($this->maxScore)) {
  112.             $this->maxScore $this->searchable->search($this->query)->getMaxScore();
  113.         }
  114.         return $this->maxScore;
  115.     }
  116.     /**
  117.      * Returns the Query.
  118.      *
  119.      * @return Query the search query
  120.      */
  121.     public function getQuery()
  122.     {
  123.         return $this->query;
  124.     }
  125.     /**
  126.      * Returns the paginated results.
  127.      *
  128.      * @param int $offset
  129.      * @param int $itemCountPerPage
  130.      *
  131.      * @throws \InvalidArgumentException
  132.      *
  133.      * @return ResultSet
  134.      */
  135.     protected function getElasticaResults($offset$itemCountPerPage)
  136.     {
  137.         $offset = (int) $offset;
  138.         $itemCountPerPage = (int) $itemCountPerPage;
  139.         $size $this->query->hasParam('size')
  140.             ? (int) $this->query->getParam('size')
  141.             : null;
  142.         if (null !== $size && $size $offset $itemCountPerPage) {
  143.             $itemCountPerPage $size $offset;
  144.         }
  145.         if ($itemCountPerPage 1) {
  146.             throw new InvalidArgumentException('$itemCountPerPage must be greater than zero');
  147.         }
  148.         $query = clone $this->query;
  149.         $query->setFrom($offset);
  150.         $query->setSize($itemCountPerPage);
  151.         $resultSet $this->searchable->search($query$this->options);
  152.         $this->totalHits $resultSet->getTotalHits();
  153.         $this->aggregations $resultSet->getAggregations();
  154.         $this->suggests $resultSet->getSuggests();
  155.         $this->maxScore $resultSet->getMaxScore();
  156.         return $resultSet;
  157.     }
  158. }