vendor/knplabs/knp-components/src/Knp/Component/Pager/Event/Subscriber/Sortable/ElasticaQuerySubscriber.php line 13

Open in your IDE?
  1. <?php
  2. namespace Knp\Component\Pager\Event\Subscriber\Sortable;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Knp\Component\Pager\Event\ItemsEvent;
  5. use Elastica\Query;
  6. use Elastica\SearchableInterface;
  7. use Knp\Component\Pager\PaginatorInterface;
  8. class ElasticaQuerySubscriber implements EventSubscriberInterface
  9. {
  10.     public function items(ItemsEvent $event)
  11.     {
  12.         // Check if the result has already been sorted by an other sort subscriber
  13.         $customPaginationParameters $event->getCustomPaginationParameters();
  14.         if (!empty($customPaginationParameters['sorted']) ) {
  15.             return;
  16.         }
  17.         if (is_array($event->target) && === count($event->target) && reset($event->target) instanceof SearchableInterface && end($event->target) instanceof Query) {
  18.             $event->setCustomPaginationParameter('sorted'true);
  19.             list($searchable$query) = $event->target;
  20.             if (isset($_GET[$event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME]])) {
  21.                 $field $_GET[$event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME]];
  22.                 $dir   = isset($_GET[$event->options[PaginatorInterface::SORT_DIRECTION_PARAMETER_NAME]]) && strtolower($_GET[$event->options[PaginatorInterface::SORT_DIRECTION_PARAMETER_NAME]]) === 'asc' 'asc' 'desc';
  23.                 if (isset($event->options[PaginatorInterface::SORT_FIELD_WHITELIST]) && !in_array($field$event->options[PaginatorInterface::SORT_FIELD_WHITELIST])) {
  24.                     throw new \UnexpectedValueException(sprintf('Cannot sort by: [%s] this field is not in whitelist',$field));
  25.                 }
  26.                 $query->setSort(array(
  27.                     $field => array('order' => $dir),
  28.                 ));
  29.             }
  30.         }
  31.     }
  32.     public static function getSubscribedEvents()
  33.     {
  34.         return array(
  35.             'knp_pager.items' => array('items'1)
  36.         );
  37.     }
  38. }