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

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. class QuerySubscriber implements EventSubscriberInterface
  10. {
  11.     public function items(ItemsEvent $event)
  12.     {
  13.         if ($event->target instanceof Query) {
  14.             if (!isset($_GET[$event->options[PaginatorInterface::FILTER_VALUE_PARAMETER_NAME]]) || (empty($_GET[$event->options[PaginatorInterface::FILTER_VALUE_PARAMETER_NAME]]) && $_GET[$event->options[PaginatorInterface::FILTER_VALUE_PARAMETER_NAME]] !== "0")) {
  15.                 return;
  16.             }
  17.             if (!empty($_GET[$event->options[PaginatorInterface::FILTER_FIELD_PARAMETER_NAME]])) {
  18.                 $columns $_GET[$event->options[PaginatorInterface::FILTER_FIELD_PARAMETER_NAME]];
  19.             } elseif (!empty($event->options[PaginatorInterface::DEFAULT_FILTER_FIELDS])) {
  20.                 $columns $event->options[PaginatorInterface::DEFAULT_FILTER_FIELDS];
  21.             } else {
  22.                 return;
  23.             }
  24.             $value $_GET[$event->options[PaginatorInterface::FILTER_VALUE_PARAMETER_NAME]];
  25.             if (false !== strpos($value'*')) {
  26.                 $value str_replace('*''%'$value);
  27.             }
  28.             if (is_string($columns) && false !== strpos($columns',')) {
  29.                 $columns explode(','$columns);
  30.             }
  31.             $columns = (array) $columns;
  32.             if (isset($event->options[PaginatorInterface::FILTER_FIELD_WHITELIST])) {
  33.                 foreach ($columns as $column) {
  34.                     if (!in_array($column$event->options[PaginatorInterface::FILTER_FIELD_WHITELIST])) {
  35.                         throw new \UnexpectedValueException("Cannot filter by: [{$column}] this field is not in whitelist");
  36.                     }
  37.                 }
  38.             }
  39.             $event->target
  40.                     ->setHint(WhereWalker::HINT_PAGINATOR_FILTER_VALUE$value)
  41.                     ->setHint(WhereWalker::HINT_PAGINATOR_FILTER_COLUMNS$columns);
  42.             QueryHelper::addCustomTreeWalker($event->target'Knp\Component\Pager\Event\Subscriber\Filtration\Doctrine\ORM\Query\WhereWalker');
  43.         }
  44.     }
  45.     public static function getSubscribedEvents()
  46.     {
  47.         return array(
  48.                 'knp_pager.items' => array('items'0),
  49.         );
  50.     }
  51. }