vendor/knplabs/knp-components/src/Knp/Component/Pager/Event/Subscriber/Sortable/Doctrine/ORM/QuerySubscriber.php line 23

Open in your IDE?
  1. <?php
  2. namespace Knp\Component\Pager\Event\Subscriber\Sortable\Doctrine\ORM;
  3. use Doctrine\ORM\Query;
  4. use Knp\Component\Pager\ArgumentAccess\ArgumentAccessInterface;
  5. use Knp\Component\Pager\Event\ItemsEvent;
  6. use Knp\Component\Pager\Event\Subscriber\Paginate\Doctrine\ORM\Query\Helper as QueryHelper;
  7. use Knp\Component\Pager\Event\Subscriber\Sortable\Doctrine\ORM\Query\OrderByWalker;
  8. use Knp\Component\Pager\Exception\InvalidValueException;
  9. use Knp\Component\Pager\PaginatorInterface;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class QuerySubscriber implements EventSubscriberInterface
  12. {
  13.     private ArgumentAccessInterface $argumentAccess;
  14.     public function __construct(ArgumentAccessInterface $argumentAccess)
  15.     {
  16.         $this->argumentAccess $argumentAccess;
  17.     }
  18.     public function items(ItemsEvent $event): void
  19.     {
  20.         // Check if the result has already been sorted by another sort subscriber
  21.         $customPaginationParameters $event->getCustomPaginationParameters();
  22.         if (!empty($customPaginationParameters['sorted']) ) {
  23.             return;
  24.         }
  25.         if ($event->target instanceof Query) {
  26.             $event->setCustomPaginationParameter('sorted'true);
  27.             $sortField $event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME];
  28.             $sortDir $event->options[PaginatorInterface::SORT_DIRECTION_PARAMETER_NAME];
  29.             if (null !== $sortField && $this->argumentAccess->has($sortField)) {
  30.                 $dir null !== $sortDir && $this->argumentAccess->has($sortDir) && strtolower($this->argumentAccess->get($sortDir)) === 'asc' 'asc' 'desc';
  31.                 if (isset($event->options[PaginatorInterface::SORT_FIELD_ALLOW_LIST]) && !in_array($this->argumentAccess->get($sortField), $event->options[PaginatorInterface::SORT_FIELD_ALLOW_LIST])) {
  32.                     throw new InvalidValueException("Cannot sort by: [{$this->argumentAccess->get($sortField)}] this field is not in allow list.");
  33.                 }
  34.                 $sortFieldParameterNames $this->argumentAccess->get($sortField);
  35.                 $fields = [];
  36.                 $aliases = [];
  37.                 if (!is_string($sortFieldParameterNames)) {
  38.                     throw new InvalidValueException('Cannot sort with array parameter.');
  39.                 }
  40.                 foreach (explode('+'$sortFieldParameterNames) as $sortFieldParameterName) {
  41.                     $parts explode('.'$sortFieldParameterName2);
  42.                     // We have to prepend the field. Otherwise, OrderByWalker will add
  43.                     // the order-by items in the wrong order
  44.                     array_unshift($fieldsend($parts));
  45.                     array_unshift($aliases<= count($parts) ? reset($parts) : false);
  46.                 }
  47.                 $event->target
  48.                     ->setHint(OrderByWalker::HINT_PAGINATOR_SORT_DIRECTION$dir)
  49.                     ->setHint(OrderByWalker::HINT_PAGINATOR_SORT_FIELD$fields)
  50.                     ->setHint(OrderByWalker::HINT_PAGINATOR_SORT_ALIAS$aliases)
  51.                 ;
  52.                 QueryHelper::addCustomTreeWalker($event->targetOrderByWalker::class);
  53.             }
  54.         }
  55.     }
  56.     public static function getSubscribedEvents(): array
  57.     {
  58.         return [
  59.             'knp_pager.items' => ['items'1],
  60.         ];
  61.     }
  62. }