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

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