vendor/friendsofsymfony/elastica-bundle/src/Finder/TransformedFinder.php line 41

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the FOSElasticaBundle package.
  4.  *
  5.  * (c) FriendsOfSymfony <https://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\Finder;
  11. use Elastica\Query;
  12. use Elastica\Result;
  13. use Elastica\SearchableInterface;
  14. use FOS\ElasticaBundle\Paginator\FantaPaginatorAdapter;
  15. use FOS\ElasticaBundle\Paginator\HybridPaginatorAdapter;
  16. use FOS\ElasticaBundle\Paginator\RawPaginatorAdapter;
  17. use FOS\ElasticaBundle\Paginator\TransformedPaginatorAdapter;
  18. use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface;
  19. use Pagerfanta\Pagerfanta;
  20. /**
  21.  * Finds elastica documents and map them to persisted objects.
  22.  *
  23.  * @phpstan-import-type TQuery from FinderInterface
  24.  * @phpstan-import-type TOptions from FinderInterface
  25.  */
  26. class TransformedFinder implements PaginatedFinderInterfacePaginatedRawFinderInterfacePaginatedHybridFinderInterface
  27. {
  28.     protected SearchableInterface $searchable;
  29.     protected ElasticaToModelTransformerInterface $transformer;
  30.     public function __construct(SearchableInterface $searchableElasticaToModelTransformerInterface $transformer)
  31.     {
  32.         $this->searchable $searchable;
  33.         $this->transformer $transformer;
  34.     }
  35.     public function find($query, ?int $limit null, array $options = [])
  36.     {
  37.         $results $this->search($query$limit$options);
  38.         return $this->transformer->transform($results);
  39.     }
  40.     public function findHybrid($query, ?int $limit null, array $options = [])
  41.     {
  42.         $results $this->search($query$limit$options);
  43.         return $this->transformer->hybridTransform($results);
  44.     }
  45.     public function findRaw($query, ?int $limit null, array $options = []): array
  46.     {
  47.         return $this->search($query$limit$options);
  48.     }
  49.     public function findPaginated($query, array $options = [])
  50.     {
  51.         $paginatorAdapter $this->createPaginatorAdapter($query$options);
  52.         return new Pagerfanta(new FantaPaginatorAdapter($paginatorAdapter));
  53.     }
  54.     public function findHybridPaginated($query, array $options = [])
  55.     {
  56.         $paginatorAdapter $this->createHybridPaginatorAdapter($query$options);
  57.         return new Pagerfanta(new FantaPaginatorAdapter($paginatorAdapter));
  58.     }
  59.     public function findRawPaginated($query, array $options = [])
  60.     {
  61.         $paginatorAdapter $this->createRawPaginatorAdapter($query$options);
  62.         return new Pagerfanta(new FantaPaginatorAdapter($paginatorAdapter));
  63.     }
  64.     public function createPaginatorAdapter($query, array $options = [])
  65.     {
  66.         $query Query::create($query);
  67.         return new TransformedPaginatorAdapter($this->searchable$query$options$this->transformer);
  68.     }
  69.     public function createHybridPaginatorAdapter($query, array $options = [])
  70.     {
  71.         $query Query::create($query);
  72.         return new HybridPaginatorAdapter($this->searchable$query$options$this->transformer);
  73.     }
  74.     public function createRawPaginatorAdapter($query, array $options = [])
  75.     {
  76.         $query Query::create($query);
  77.         return new RawPaginatorAdapter($this->searchable$query$options);
  78.     }
  79.     /**
  80.      * @phpstan-param TQuery $query
  81.      * @phpstan-param TOptions $options
  82.      *
  83.      * @param mixed $query
  84.      *
  85.      * @return Result[]
  86.      */
  87.     protected function search($query, ?int $limit null, array $options = [])
  88.     {
  89.         $queryObject Query::create($query);
  90.         if (null !== $limit) {
  91.             $queryObject->setSize($limit);
  92.         }
  93.         return $this->searchable->search($queryObject$options)->getResults();
  94.     }
  95. }