vendor/knplabs/knp-components/src/Knp/Component/Pager/Event/Subscriber/Filtration/PropelQuerySubscriber.php line 20

  1. <?php
  2. namespace Knp\Component\Pager\Event\Subscriber\Filtration;
  3. use Knp\Component\Pager\ArgumentAccess\ArgumentAccessInterface;
  4. use Knp\Component\Pager\Event\ItemsEvent;
  5. use Knp\Component\Pager\Exception\InvalidValueException;
  6. use Knp\Component\Pager\PaginatorInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. class PropelQuerySubscriber implements EventSubscriberInterface
  9. {
  10.     private ArgumentAccessInterface $argumentAccess;
  11.     public function __construct(ArgumentAccessInterface $argumentAccess)
  12.     {
  13.         $this->argumentAccess $argumentAccess;
  14.     }
  15.     public function items(ItemsEvent $event): void
  16.     {
  17.         $query $event->target;
  18.         if ($query instanceof \ModelCriteria) {
  19.             if (!$this->argumentAccess->has($event->options[PaginatorInterface::FILTER_VALUE_PARAMETER_NAME])) {
  20.                 return;
  21.             }
  22.             if ($this->argumentAccess->has($event->options[PaginatorInterface::FILTER_FIELD_PARAMETER_NAME])) {
  23.                 $columns $this->argumentAccess->get($event->options[PaginatorInterface::FILTER_FIELD_PARAMETER_NAME]);
  24.             } elseif (!empty($event->options[PaginatorInterface::DEFAULT_FILTER_FIELDS])) {
  25.                 $columns $event->options[PaginatorInterface::DEFAULT_FILTER_FIELDS];
  26.             } else {
  27.                 return;
  28.             }
  29.             if (is_string($columns) && false !== strpos($columns',')) {
  30.                 $columns explode(','$columns);
  31.             }
  32.             $columns = (array) $columns;
  33.             if (isset($event->options[PaginatorInterface::FILTER_FIELD_ALLOW_LIST])) {
  34.                 foreach ($columns as $column) {
  35.                     if (!in_array($column$event->options[PaginatorInterface::FILTER_FIELD_ALLOW_LIST])) {
  36.                         throw new InvalidValueException("Cannot filter by: [{$column}] this field is not in allow list");
  37.                     }
  38.                 }
  39.             }
  40.             $value $this->argumentAccess->get($event->options[PaginatorInterface::FILTER_VALUE_PARAMETER_NAME]);
  41.             $criteria \Criteria::EQUAL;
  42.             if (false !== strpos($value'*')) {
  43.                 $value str_replace('*''%'$value);
  44.                 $criteria \Criteria::LIKE;
  45.             }
  46.             foreach ($columns as $column) {
  47.                 if (false !== strpos($column'.')) {
  48.                     $query->where($column.$criteria.'?'$value);
  49.                 } else {
  50.                     $query->{'filterBy'.$column}($value$criteria);
  51.                 }
  52.             }
  53.         }
  54.     }
  55.     public static function getSubscribedEvents(): array
  56.     {
  57.         return [
  58.             'knp_pager.items' => ['items'0],
  59.         ];
  60.     }
  61. }