<?php
declare(strict_types=1);
namespace Smakmedia\Subscriber;
use Knp\Component\Pager\Event\ItemsEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class PaginatorSubscriber implements EventSubscriberInterface
{
public function items(ItemsEvent $event): void
{
// TODO: idea for better solution?
if (empty($event->target['args'])) {
$event->count = count($event->target);
$event->items = array_slice($event->target, $event->getOffset(), $event->getLimit());
} else {
$args = $event->target['args'];
$args[] = $event->getOffset();
$args[] = $event->getLimit();
$data = call_user_func_array($event->target['callback'], $args);
$event->count = $data['totalCount'];
$event->items = $data['list'];
}
$event->stopPropagation();
}
public static function getSubscribedEvents(): array
{
return [
'knp_pager.items' => ['items', 1/* increased priority to override any internal */],
];
}
}