vendor/knplabs/knp-components/src/Knp/Component/Pager/Event/Subscriber/Sortable/Doctrine/ODM/MongoDB/QuerySubscriber.php line 12

Open in your IDE?
  1. <?php
  2. namespace Knp\Component\Pager\Event\Subscriber\Sortable\Doctrine\ODM\MongoDB;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Knp\Component\Pager\Event\ItemsEvent;
  5. use Doctrine\ODM\MongoDB\Query\Query;
  6. use Knp\Component\Pager\PaginatorInterface;
  7. class QuerySubscriber implements EventSubscriberInterface
  8. {
  9.     public function items(ItemsEvent $event)
  10.     {
  11.         // Check if the result has already been sorted by an other sort subscriber
  12.         $customPaginationParameters $event->getCustomPaginationParameters();
  13.         if (!empty($customPaginationParameters['sorted']) ) {
  14.             return;
  15.         }
  16.         if ($event->target instanceof Query) {
  17.             $event->setCustomPaginationParameter('sorted'true);
  18.             if (isset($_GET[$event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME]])) {
  19.                 $field $_GET[$event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME]];
  20.                 $dir strtolower($_GET[$event->options[PaginatorInterface::SORT_DIRECTION_PARAMETER_NAME]]) == 'asc' : -1;
  21.                 if (isset($event->options[PaginatorInterface::SORT_FIELD_WHITELIST])) {
  22.                     if (!in_array($field$event->options[PaginatorInterface::SORT_FIELD_WHITELIST])) {
  23.                         throw new \UnexpectedValueException("Cannot sort by: [{$field}] this field is not in whitelist");
  24.                     }
  25.                 }
  26.                 static $reflectionProperty;
  27.                 if (is_null($reflectionProperty)) {
  28.                     $reflectionClass = new \ReflectionClass('Doctrine\MongoDB\Query\Query');
  29.                     $reflectionProperty $reflectionClass->getProperty('query');
  30.                     $reflectionProperty->setAccessible(true);
  31.                 }
  32.                 $queryOptions $reflectionProperty->getValue($event->target);
  33.                 //@todo: seems like does not support multisort ??
  34.                 $queryOptions['sort'] = array($field => $dir);
  35.                 $reflectionProperty->setValue($event->target$queryOptions);
  36.             }
  37.         }
  38.     }
  39.     public static function getSubscribedEvents()
  40.     {
  41.         return array(
  42.             'knp_pager.items' => array('items'1)
  43.         );
  44.     }
  45. }