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

Open in your IDE?
  1. <?php
  2. namespace Knp\Component\Pager\Event\Subscriber\Sortable;
  3. use Knp\Component\Pager\Event\ItemsEvent;
  4. use Knp\Component\Pager\PaginatorInterface;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. /**
  7.  * Solarium query sorting
  8.  *
  9.  * @author Marek Kalnik <marekk@theodo.fr>
  10.  */
  11. class SolariumQuerySubscriber implements EventSubscriberInterface
  12. {
  13.     public function items(ItemsEvent $event)
  14.     {
  15.         // Check if the result has already been sorted by an other sort subscriber
  16.         $customPaginationParameters $event->getCustomPaginationParameters();
  17.         if (!empty($customPaginationParameters['sorted']) ) {
  18.             return;
  19.         }
  20.         if (is_array($event->target) && == count($event->target)) {
  21.             $event->setCustomPaginationParameter('sorted'true);
  22.             $values array_values($event->target);
  23.             list($client$query) = $values;
  24.             if ($client instanceof \Solarium\Client && $query instanceof \Solarium\QueryType\Select\Query\Query) {
  25.                 if (isset($_GET[$event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME]])) {
  26.                     if (isset($event->options[PaginatorInterface::SORT_FIELD_WHITELIST])) {
  27.                         if (!in_array($_GET[$event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME]], $event->options[PaginatorInterface::SORT_FIELD_WHITELIST])) {
  28.                             throw new \UnexpectedValueException("Cannot sort by: [{$_GET[$event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME]]}] this field is not in whitelist");
  29.                         }
  30.                     }
  31.                     $query->addSort($_GET[$event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME]], $this->getSortDirection($event));
  32.                 }
  33.             }
  34.         }
  35.     }
  36.     public static function getSubscribedEvents()
  37.     {
  38.         return array(
  39.             // trigger before the pagination subscriber
  40.             'knp_pager.items' => array('items'1),
  41.         );
  42.     }
  43.     private function getSortDirection($event)
  44.     {
  45.         return isset($_GET[$event->options[PaginatorInterface::SORT_DIRECTION_PARAMETER_NAME]]) &&
  46.             strtolower($_GET[$event->options[PaginatorInterface::SORT_DIRECTION_PARAMETER_NAME]]) === 'asc' 'asc' 'desc';
  47.     }
  48. }