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

  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])) {
  32.                     if (!in_array($this->argumentAccess->get($sortField), $event->options[PaginatorInterface::SORT_FIELD_ALLOW_LIST])) {
  33.                         throw new InvalidValueException("Cannot sort by: [{$this->argumentAccess->get($sortField)}] this field is not in allow list.");
  34.                     }
  35.                 }
  36.                 $sortFieldParameterNames $this->argumentAccess->get($sortField);
  37.                 $fields = [];
  38.                 $aliases = [];
  39.                 if (!is_string($sortFieldParameterNames)) {
  40.                     throw new InvalidValueException('Cannot sort with array parameter.');
  41.                 }
  42.                 foreach (explode('+'$sortFieldParameterNames) as $sortFieldParameterName) {
  43.                     $parts explode('.'$sortFieldParameterName2);
  44.                     // We have to prepend the field. Otherwise OrderByWalker will add
  45.                     // the order-by items in the wrong order
  46.                     array_unshift($fieldsend($parts));
  47.                     array_unshift($aliases<= count($parts) ? reset($parts) : false);
  48.                 }
  49.                 $event->target
  50.                     ->setHint(OrderByWalker::HINT_PAGINATOR_SORT_DIRECTION$dir)
  51.                     ->setHint(OrderByWalker::HINT_PAGINATOR_SORT_FIELD$fields)
  52.                     ->setHint(OrderByWalker::HINT_PAGINATOR_SORT_ALIAS$aliases)
  53.                 ;
  54.                 QueryHelper::addCustomTreeWalker($event->targetOrderByWalker::class);
  55.             }
  56.         }
  57.     }
  58.     public static function getSubscribedEvents(): array
  59.     {
  60.         return [
  61.             'knp_pager.items' => ['items'1],
  62.         ];
  63.     }
  64. }