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

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\Finder;
  11. use Elastica\Query;
  12. use Elastica\SearchableInterface;
  13. use FOS\ElasticaBundle\Paginator\FantaPaginatorAdapter;
  14. use FOS\ElasticaBundle\Paginator\HybridPaginatorAdapter;
  15. use FOS\ElasticaBundle\Paginator\RawPaginatorAdapter;
  16. use FOS\ElasticaBundle\Paginator\TransformedPaginatorAdapter;
  17. use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface;
  18. use Pagerfanta\Pagerfanta;
  19. /**
  20.  * Finds elastica documents and map them to persisted objects.
  21.  */
  22. class TransformedFinder implements PaginatedFinderInterface
  23. {
  24.     /**
  25.      * @var SearchableInterface
  26.      */
  27.     protected $searchable;
  28.     /**
  29.      * @var ElasticaToModelTransformerInterface
  30.      */
  31.     protected $transformer;
  32.     /**
  33.      * @param SearchableInterface                 $searchable
  34.      * @param ElasticaToModelTransformerInterface $transformer
  35.      */
  36.     public function __construct(SearchableInterface $searchableElasticaToModelTransformerInterface $transformer)
  37.     {
  38.         $this->searchable $searchable;
  39.         $this->transformer $transformer;
  40.     }
  41.     /**
  42.      * {@inheritdoc}
  43.      */
  44.     public function find($query$limit null$options = [])
  45.     {
  46.         $results $this->search($query$limit$options);
  47.         return $this->transformer->transform($results);
  48.     }
  49.     /**
  50.      * @param $query
  51.      * @param null|int $limit
  52.      * @param array    $options
  53.      *
  54.      * @return array
  55.      */
  56.     public function findHybrid($query$limit null$options = [])
  57.     {
  58.         $results $this->search($query$limit$options);
  59.         return $this->transformer->hybridTransform($results);
  60.     }
  61.     /**
  62.      * {@inheritdoc}
  63.      */
  64.     public function findPaginated($query$options = [])
  65.     {
  66.         $queryObject Query::create($query);
  67.         $paginatorAdapter $this->createPaginatorAdapter($queryObject$options);
  68.         return new Pagerfanta(new FantaPaginatorAdapter($paginatorAdapter));
  69.     }
  70.     /**
  71.      * {@inheritdoc}
  72.      */
  73.     public function createPaginatorAdapter($query$options = [])
  74.     {
  75.         $query Query::create($query);
  76.         return new TransformedPaginatorAdapter($this->searchable$query$options$this->transformer);
  77.     }
  78.     /**
  79.      * {@inheritdoc}
  80.      */
  81.     public function createHybridPaginatorAdapter($query)
  82.     {
  83.         $query Query::create($query);
  84.         return new HybridPaginatorAdapter($this->searchable$query$this->transformer);
  85.     }
  86.     /**
  87.      * {@inheritdoc}
  88.      */
  89.     public function createRawPaginatorAdapter($query$options = [])
  90.     {
  91.         $query Query::create($query);
  92.         return new RawPaginatorAdapter($this->searchable$query$options);
  93.     }
  94.     /**
  95.      * @param $query
  96.      * @param null|int $limit
  97.      * @param array    $options
  98.      *
  99.      * @return array
  100.      */
  101.     protected function search($query$limit null$options = [])
  102.     {
  103.         $queryObject Query::create($query);
  104.         if (null !== $limit) {
  105.             $queryObject->setSize($limit);
  106.         }
  107.         $results $this->searchable->search($queryObject$options)->getResults();
  108.         return $results;
  109.     }
  110. }