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

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