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

Open in your IDE?
  1. <?php
  2. namespace Knp\Component\Pager\Event\Subscriber\Sortable\Doctrine\ODM\MongoDB;
  3. use Doctrine\ODM\MongoDB\Query\Query;
  4. use Knp\Component\Pager\Event\ItemsEvent;
  5. use Knp\Component\Pager\PaginatorInterface;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpFoundation\Request;
  8. class QuerySubscriber implements EventSubscriberInterface
  9. {
  10.     private Request $request;
  11.     public function __construct(Request $request)
  12.     {
  13.         $this->request $request;
  14.     }
  15.     public function items(ItemsEvent $event): void
  16.     {
  17.         // Check if the result has already been sorted by an other sort subscriber
  18.         $customPaginationParameters $event->getCustomPaginationParameters();
  19.         if (!empty($customPaginationParameters['sorted']) ) {
  20.             return;
  21.         }
  22.         if ($event->target instanceof Query) {
  23.             $event->setCustomPaginationParameter('sorted'true);
  24.             $sortField $event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME];
  25.             $sortDir $event->options[PaginatorInterface::SORT_DIRECTION_PARAMETER_NAME];
  26.             if (null !== $sortField && $this->request->query->has($sortField)) {
  27.                 $field $this->request->query->get($sortField);
  28.                 $dir null !== $sortDir && strtolower($this->request->query->get($sortDir)) === 'asc' : -1;
  29.                 if (isset($event->options[PaginatorInterface::SORT_FIELD_ALLOW_LIST])) {
  30.                     if (!in_array($field$event->options[PaginatorInterface::SORT_FIELD_ALLOW_LIST])) {
  31.                         throw new \UnexpectedValueException("Cannot sort by: [{$field}] this field is not in allow list.");
  32.                     }
  33.                 }
  34.                 static $reflectionProperty;
  35.                 if (is_null($reflectionProperty)) {
  36.                     $reflectionClass = new \ReflectionClass(Query::class);
  37.                     $reflectionProperty $reflectionClass->getProperty('query');
  38.                     $reflectionProperty->setAccessible(true);
  39.                 }
  40.                 $queryOptions $reflectionProperty->getValue($event->target);
  41.                 // handle multi sort
  42.                 $sortFields explode('+'$field);
  43.                 $sortOption = [];
  44.                 foreach ($sortFields as $sortField) {
  45.                     $sortOption[$sortField] = $dir;
  46.                 }
  47.                 $queryOptions['sort'] = $sortOption;
  48.                 $reflectionProperty->setValue($event->target$queryOptions);
  49.             }
  50.         }
  51.     }
  52.     public static function getSubscribedEvents(): array
  53.     {
  54.         return [
  55.             'knp_pager.items' => ['items'1],
  56.         ];
  57.     }
  58. }