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

  1. <?php
  2. namespace Knp\Component\Pager\Event\Subscriber\Filtration\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\Filtration\Doctrine\ORM\Query\WhereWalker;
  7. use Knp\Component\Pager\Event\Subscriber\Paginate\Doctrine\ORM\Query\Helper as QueryHelper;
  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.         if (!$event->target instanceof Query) {
  21.             return;
  22.         }
  23.         if (!$this->hasQueryParameter($event->options[PaginatorInterface::FILTER_VALUE_PARAMETER_NAME])) {
  24.             return;
  25.         }
  26.         $filterValue $this->getQueryParameter($event->options[PaginatorInterface::FILTER_VALUE_PARAMETER_NAME]);
  27.         if ((empty($filterValue) && $filterValue !== '0')) {
  28.             return;
  29.         }
  30.         $filterName null;
  31.         if ($this->hasQueryParameter($event->options[PaginatorInterface::FILTER_FIELD_PARAMETER_NAME])) {
  32.             $filterName $this->getQueryParameter($event->options[PaginatorInterface::FILTER_FIELD_PARAMETER_NAME]);
  33.         }
  34.         if (!empty($filterName)) {
  35.             $columns $filterName;
  36.         } elseif (!empty($event->options[PaginatorInterface::DEFAULT_FILTER_FIELDS])) {
  37.             $columns $event->options[PaginatorInterface::DEFAULT_FILTER_FIELDS];
  38.         } else {
  39.             return;
  40.         }
  41.         $value $this->getQueryParameter($event->options[PaginatorInterface::FILTER_VALUE_PARAMETER_NAME]);
  42.         if (str_contains($value'*')) {
  43.             $value str_replace('*''%'$value);
  44.         }
  45.         if (is_string($columns) && false !== strpos($columns',')) {
  46.             $columns explode(','$columns);
  47.         }
  48.         $columns = (array) $columns;
  49.         if (isset($event->options[PaginatorInterface::FILTER_FIELD_ALLOW_LIST])) {
  50.             foreach ($columns as $column) {
  51.                 if (!in_array($column$event->options[PaginatorInterface::FILTER_FIELD_ALLOW_LIST])) {
  52.                     throw new InvalidValueException("Cannot filter by: [{$column}] this field is not in allow list");
  53.                 }
  54.             }
  55.         }
  56.         $event->target
  57.                 ->setHint(WhereWalker::HINT_PAGINATOR_FILTER_VALUE$value)
  58.                 ->setHint(WhereWalker::HINT_PAGINATOR_FILTER_COLUMNS$columns);
  59.         QueryHelper::addCustomTreeWalker($event->targetWhereWalker::class);
  60.     }
  61.     public static function getSubscribedEvents(): array
  62.     {
  63.         return [
  64.             'knp_pager.items' => ['items'0],
  65.         ];
  66.     }
  67.     private function hasQueryParameter(string $name): bool
  68.     {
  69.         return $this->argumentAccess->has($name);
  70.     }
  71.     private function getQueryParameter(string $name): ?string
  72.     {
  73.         return $this->argumentAccess->get($name);
  74.     }
  75. }