vendor/friendsofsymfony/elastica-bundle/Subscriber/PaginateElasticaQuerySubscriber.php line 34

Open in your IDE?
  1. <?php
  2. namespace FOS\ElasticaBundle\Subscriber;
  3. use FOS\ElasticaBundle\Paginator\PaginatorAdapterInterface;
  4. use FOS\ElasticaBundle\Paginator\PartialResultsInterface;
  5. use Knp\Component\Pager\Event\ItemsEvent;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. class PaginateElasticaQuerySubscriber implements EventSubscriberInterface
  10. {
  11.     /**
  12.      * @var Request
  13.      */
  14.     private $request;
  15.     /**
  16.      * @param RequestStack|Request $requestStack
  17.      */
  18.     public function setRequest($requestStack)
  19.     {
  20.         if ($requestStack instanceof Request) {
  21.             $this->request $requestStack;
  22.         } elseif ($requestStack instanceof RequestStack) {
  23.             $this->request $requestStack->getMasterRequest();
  24.         }
  25.     }
  26.     /**
  27.      * @param ItemsEvent $event
  28.      */
  29.     public function items(ItemsEvent $event)
  30.     {
  31.         if ($event->target instanceof PaginatorAdapterInterface) {
  32.             // Add sort to query
  33.             $this->setSorting($event);
  34.             /** @var $results PartialResultsInterface */
  35.             $results $event->target->getResults($event->getOffset(), $event->getLimit());
  36.             $event->count $results->getTotalHits();
  37.             $event->items $results->toArray();
  38.             $aggregations $results->getAggregations();
  39.             if (null != $aggregations) {
  40.                 $event->setCustomPaginationParameter('aggregations'$aggregations);
  41.             }
  42.             $event->stopPropagation();
  43.         }
  44.     }
  45.     /**
  46.      * Adds knp paging sort to query.
  47.      *
  48.      * @param ItemsEvent $event
  49.      */
  50.     protected function setSorting(ItemsEvent $event)
  51.     {
  52.         $options $event->options;
  53.         $sortField $this->request->get($options['sortFieldParameterName']);
  54.         if (!$sortField && isset($options['defaultSortFieldName'])) {
  55.             $sortField $options['defaultSortFieldName'];
  56.         }
  57.         if (!empty($sortField)) {
  58.             $event->target->getQuery()->setSort(array(
  59.                 $sortField => $this->getSort($sortField$options),
  60.             ));
  61.         }
  62.     }
  63.     protected function getSort($sortField, array $options = [])
  64.     {
  65.         $sort = [
  66.             'order' => $this->getSortDirection($sortField$options),
  67.         ];
  68.         if (isset($options['sortNestedPath'])) {
  69.             $path is_callable($options['sortNestedPath']) ?
  70.                 $options['sortNestedPath']($sortField) : $options['sortNestedPath'];
  71.             if (!empty($path)) {
  72.                 $sort['nested_path'] = $path;
  73.             }
  74.         }
  75.         if (isset($options['sortNestedFilter'])) {
  76.             $filter is_callable($options['sortNestedFilter']) ?
  77.                 $options['sortNestedFilter']($sortField) : $options['sortNestedFilter'];
  78.             if (!empty($filter)) {
  79.                 $sort['nested_filter'] = $filter;
  80.             }
  81.         }
  82.         return $sort;
  83.     }
  84.     protected function getSortDirection($sortField, array $options = [])
  85.     {
  86.         $dir 'asc';
  87.         $sortDirection $this->request->get($options['sortDirectionParameterName']);
  88.         if (empty($sortDirection) && isset($options['defaultSortDirection'])) {
  89.             $sortDirection $options['defaultSortDirection'];
  90.         }
  91.         if ('desc' === strtolower($sortDirection)) {
  92.             $dir 'desc';
  93.         }
  94.         // check if the requested sort field is in the sort whitelist
  95.         if (isset($options['sortFieldWhitelist']) && !in_array($sortField$options['sortFieldWhitelist'])) {
  96.             throw new \UnexpectedValueException(sprintf('Cannot sort by: [%s] this field is not in whitelist'$sortField));
  97.         }
  98.         return $dir;
  99.     }
  100.     /**
  101.      * @return array
  102.      */
  103.     public static function getSubscribedEvents()
  104.     {
  105.         return array(
  106.             'knp_pager.items' => array('items'1),
  107.         );
  108.     }
  109. }