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

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