vendor/pagerfanta/pagerfanta/lib/Adapter/Elastica/ElasticaAdapter.php line 98

Open in your IDE?
  1. <?php
  2. namespace Pagerfanta\Elastica;
  3. use Elastica\Query;
  4. use Elastica\ResultSet;
  5. use Elastica\SearchableInterface;
  6. use Pagerfanta\Adapter\AdapterInterface;
  7. /**
  8.  * Adapter which calculates pagination from a Elastica Query.
  9.  */
  10. class ElasticaAdapter implements AdapterInterface
  11. {
  12.     /**
  13.      * @var SearchableInterface
  14.      */
  15.     private $searchable;
  16.     /**
  17.      * @var Query
  18.      */
  19.     private $query;
  20.     /**
  21.      * @var array
  22.      */
  23.     private $options;
  24.     /**
  25.      * Used to limit the number of totalHits returned by ElasticSearch.
  26.      * For more information, see: https://github.com/whiteoctober/Pagerfanta/pull/213#issue-87631892.
  27.      *
  28.      * @var int|null
  29.      */
  30.     private $maxResults;
  31.     /**
  32.      * @var ResultSet|null
  33.      */
  34.     private $resultSet;
  35.     /**
  36.      * @param int|null $maxResults
  37.      */
  38.     public function __construct(SearchableInterface $searchableQuery $query, array $options = [], $maxResults null)
  39.     {
  40.         $this->searchable $searchable;
  41.         $this->query $query;
  42.         $this->options $options;
  43.         $this->maxResults $maxResults;
  44.     }
  45.     /**
  46.      * Returns the Elastica ResultSet.
  47.      *
  48.      * Will return null if getSlice has not yet been called.
  49.      *
  50.      * @return ResultSet|null
  51.      */
  52.     public function getResultSet()
  53.     {
  54.         return $this->resultSet;
  55.     }
  56.     /**
  57.      * @return int
  58.      */
  59.     public function getNbResults()
  60.     {
  61.         if (!$this->resultSet) {
  62.             $totalHits $this->searchable->count($this->query);
  63.         } else {
  64.             $totalHits $this->resultSet->getTotalHits();
  65.         }
  66.         if (null === $this->maxResults) {
  67.             return $totalHits;
  68.         }
  69.         return min($totalHits$this->maxResults);
  70.     }
  71.     /**
  72.      * @param int $offset
  73.      * @param int $length
  74.      *
  75.      * @return iterable
  76.      */
  77.     public function getSlice($offset$length)
  78.     {
  79.         return $this->resultSet $this->searchable->search(
  80.             $this->query,
  81.             array_merge(
  82.                 $this->options,
  83.                 [
  84.                     'from' => $offset,
  85.                     'size' => $length,
  86.                 ]
  87.             )
  88.         );
  89.     }
  90. }