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

Open in your IDE?
  1. <?php
  2. namespace Knp\Component\Pager\Event\Subscriber\Filtration\Doctrine\ORM;
  3. use Doctrine\ORM\Query;
  4. use Knp\Component\Pager\Event\ItemsEvent;
  5. use Knp\Component\Pager\Event\Subscriber\Filtration\Doctrine\ORM\Query\WhereWalker;
  6. use Knp\Component\Pager\Event\Subscriber\Paginate\Doctrine\ORM\Query\Helper as QueryHelper;
  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.     private Request $request;
  13.     public function __construct(?Request $request)
  14.     {
  15.         $this->request $request ?? Request::createFromGlobals();
  16.     }
  17.     public function items(ItemsEvent $event): void
  18.     {
  19.         if ($event->target instanceof Query) {
  20.             $filterValue $this->getQueryParameter($event->options[PaginatorInterface::FILTER_VALUE_PARAMETER_NAME]);
  21.             if (null === $filterValue || (empty($filterValue) && $filterValue !== '0')) {
  22.                 return;
  23.             }
  24.             $filterName $this->getQueryParameter($event->options[PaginatorInterface::FILTER_FIELD_PARAMETER_NAME]);
  25.             if (!empty($filterName)) {
  26.                 $columns $filterName;
  27.             } elseif (!empty($event->options[PaginatorInterface::DEFAULT_FILTER_FIELDS])) {
  28.                 $columns $event->options[PaginatorInterface::DEFAULT_FILTER_FIELDS];
  29.             } else {
  30.                 return;
  31.             }
  32.             $value $this->getQueryParameter($event->options[PaginatorInterface::FILTER_VALUE_PARAMETER_NAME]);
  33.             if (false !== strpos($value'*')) {
  34.                 $value str_replace('*''%'$value);
  35.             }
  36.             if (is_string($columns) && false !== strpos($columns',')) {
  37.                 $columns explode(','$columns);
  38.             }
  39.             $columns = (array) $columns;
  40.             if (isset($event->options[PaginatorInterface::FILTER_FIELD_ALLOW_LIST])) {
  41.                 foreach ($columns as $column) {
  42.                     if (!in_array($column$event->options[PaginatorInterface::FILTER_FIELD_ALLOW_LIST])) {
  43.                         throw new \UnexpectedValueException("Cannot filter by: [{$column}] this field is not in allow list");
  44.                     }
  45.                 }
  46.             }
  47.             $event->target
  48.                     ->setHint(WhereWalker::HINT_PAGINATOR_FILTER_VALUE$value)
  49.                     ->setHint(WhereWalker::HINT_PAGINATOR_FILTER_COLUMNS$columns);
  50.             QueryHelper::addCustomTreeWalker($event->targetWhereWalker::class);
  51.         }
  52.     }
  53.     public static function getSubscribedEvents(): array
  54.     {
  55.         return [
  56.             'knp_pager.items' => ['items'0],
  57.         ];
  58.     }
  59.     private function getQueryParameter(string $name): ?string
  60.     {
  61.         return $this->request->query->get($name);
  62.     }
  63. }