-
Notifications
You must be signed in to change notification settings - Fork 344
/
SlidingPaginationSubscriber.php
92 lines (75 loc) · 3.7 KB
/
SlidingPaginationSubscriber.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
namespace Knp\Bundle\PaginatorBundle\Subscriber;
use Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination;
use Knp\Component\Pager\Event\PaginationEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
final class SlidingPaginationSubscriber implements EventSubscriberInterface
{
private ?string $route = null;
/** @var array<string, mixed> */
private array $params = [];
/**
* @param array<string, mixed> $options
*/
public function __construct(private readonly array $options)
{
}
public function onKernelRequest(RequestEvent $event): void
{
if (HttpKernelInterface::MAIN_REQUEST !== $event->getRequestType()) {
return;
}
$request = $event->getRequest();
$this->route = $request->attributes->get('_route');
$this->params = \array_replace($request->query->all(), $request->attributes->get('_route_params', []));
foreach ($this->params as $key => $param) {
if (\str_starts_with($key, '_')) {
unset($this->params[$key]);
}
}
}
public function pagination(PaginationEvent $event): void
{
// default sort field and order
$eventOptions = $event->options;
if (isset($eventOptions['defaultSortFieldName']) && !isset($this->params[$eventOptions['sortFieldParameterName']])) {
$this->params[$eventOptions['sortFieldParameterName']] = $eventOptions['defaultSortFieldName'];
}
if (isset($eventOptions['defaultSortDirection']) && !isset($this->params[$eventOptions['sortDirectionParameterName']])) {
$this->params[$eventOptions['sortDirectionParameterName']] = $eventOptions['defaultSortDirection'];
}
// remove default sort params from pagination links
if (isset($eventOptions['removeDefaultSortParams']) && true === $eventOptions['removeDefaultSortParams']) {
$defaultSortFieldName = $eventOptions['defaultSortFieldName'];
$sortFieldParameterName = $this->params[$eventOptions['sortFieldParameterName']];
$isFieldEqual = $defaultSortFieldName === $sortFieldParameterName;
$defaultSortDirection = $eventOptions['defaultSortDirection'];
$sortDirectionParameterName = $this->params[$eventOptions['sortDirectionParameterName']];
$isDirectionEqual = $defaultSortDirection === $sortDirectionParameterName;
if (isset($defaultSortFieldName, $sortFieldParameterName, $defaultSortDirection, $sortDirectionParameterName) && $isFieldEqual && $isDirectionEqual) {
unset($this->params[$eventOptions['sortFieldParameterName']], $this->params[$eventOptions['sortDirectionParameterName']]);
}
}
$pagination = new SlidingPagination($this->params);
$pagination->setUsedRoute($this->route);
$pagination->setTemplate($this->options['defaultPaginationTemplate']);
$pagination->setRelLinksTemplate($this->options['defaultRelLinksTemplate']);
$pagination->setSortableTemplate($this->options['defaultSortableTemplate']);
$pagination->setFiltrationTemplate($this->options['defaultFiltrationTemplate']);
$pagination->setPageRange($this->options['defaultPageRange']);
$pagination->setPageLimit($this->options['defaultPageLimit']);
$event->setPagination($pagination);
$event->stopPropagation();
}
/**
* @return array<string, array<int, int|string>>
*/
public static function getSubscribedEvents(): array
{
return [
'knp_pager.pagination' => ['pagination', 1],
];
}
}