src/Subscriber/PaginatorSubscriber.php line 12

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Smakmedia\Subscriber;
  4. use Knp\Component\Pager\Event\ItemsEvent;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. class PaginatorSubscriber implements EventSubscriberInterface
  7. {
  8.     public function items(ItemsEvent $event): void
  9.     {
  10.         // TODO: idea for better solution?
  11.         if (empty($event->target['args'])) {
  12.             $event->count count($event->target);
  13.             $event->items array_slice($event->target$event->getOffset(), $event->getLimit());
  14.         } else {
  15.             $args $event->target['args'];
  16.             $args[] = $event->getOffset();
  17.             $args[] = $event->getLimit();
  18.             $data call_user_func_array($event->target['callback'], $args);
  19.             $event->count $data['totalCount'];
  20.             $event->items $data['list'];
  21.         }
  22.         $event->stopPropagation();
  23.     }
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         return [
  27.             'knp_pager.items' => ['items'1/* increased priority to override any internal */],
  28.         ];
  29.     }
  30. }