Skip to content

Commit 08e5ca6

Browse files
committed
Pass ArgumentAccess with the knp_pager.items event
1 parent a1ccc00 commit 08e5ca6

File tree

15 files changed

+162
-152
lines changed

15 files changed

+162
-152
lines changed

Diff for: src/Knp/Component/Pager/Event/ItemsEvent.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Knp\Component\Pager\Event;
44

5+
use Knp\Component\Pager\ArgumentAccess\ArgumentAccessInterface;
56
use Symfony\Contracts\EventDispatcher\Event;
67

78
/**
@@ -34,7 +35,7 @@ final class ItemsEvent extends Event
3435
*/
3536
private array $customPaginationParams = [];
3637

37-
public function __construct(private readonly int $offset, private readonly int $limit)
38+
public function __construct(private readonly int $offset, private readonly int $limit, private readonly ArgumentAccessInterface $argumentAccess)
3839
{
3940
}
4041

@@ -67,4 +68,9 @@ public function getOffset(): int
6768
{
6869
return $this->offset;
6970
}
71+
72+
public function getArgumentAccess(): ArgumentAccessInterface
73+
{
74+
return $this->argumentAccess;
75+
}
7076
}

Diff for: src/Knp/Component/Pager/Event/Subscriber/Filtration/Doctrine/ORM/QuerySubscriber.php

+12-5
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,23 @@ public function items(ItemsEvent $event): void
2222
if (!$event->target instanceof Query) {
2323
return;
2424
}
25-
if (!$this->hasQueryParameter($event->options[PaginatorInterface::FILTER_VALUE_PARAMETER_NAME])) {
25+
$argumentAccess = $event->getArgumentAccess();
26+
27+
if (!$argumentAccess->has($event->options[PaginatorInterface::FILTER_VALUE_PARAMETER_NAME])) {
2628
return;
2729
}
28-
$filterValue = $this->getQueryParameter($event->options[PaginatorInterface::FILTER_VALUE_PARAMETER_NAME]);
30+
$filterValue = $argumentAccess->get($event->options[PaginatorInterface::FILTER_VALUE_PARAMETER_NAME]);
2931
if ((empty($filterValue) && $filterValue !== '0')) {
3032
return;
3133
}
3234
$filterName = null;
33-
if ($this->hasQueryParameter($event->options[PaginatorInterface::FILTER_FIELD_PARAMETER_NAME])) {
34-
$filterName = $this->getQueryParameter($event->options[PaginatorInterface::FILTER_FIELD_PARAMETER_NAME]);
35+
if ($argumentAccess->has($event->options[PaginatorInterface::FILTER_FIELD_PARAMETER_NAME])) {
36+
37+
38+
$old = $this->getQueryParameter($event->options[PaginatorInterface::FILTER_FIELD_PARAMETER_NAME]);
39+
40+
41+
$filterName = $argumentAccess->get($event->options[PaginatorInterface::FILTER_FIELD_PARAMETER_NAME]);
3542
}
3643
if (!empty($filterName)) {
3744
$columns = $filterName;
@@ -40,7 +47,7 @@ public function items(ItemsEvent $event): void
4047
} else {
4148
return;
4249
}
43-
$value = $this->getQueryParameter($event->options[PaginatorInterface::FILTER_VALUE_PARAMETER_NAME]);
50+
$value = $argumentAccess->get($event->options[PaginatorInterface::FILTER_VALUE_PARAMETER_NAME]);
4451
if (str_contains($value, '*')) {
4552
$value = str_replace('*', '%', $value);
4653
}

Diff for: src/Knp/Component/Pager/Event/Subscriber/Filtration/FiltrationSubscriber.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function before(BeforeEvent $event): void
2323
$dispatcher = $event->getEventDispatcher();
2424
// hook all standard filtration subscribers
2525
$dispatcher->addSubscriber(new Doctrine\ORM\QuerySubscriber($event->getArgumentAccess()));
26-
$dispatcher->addSubscriber(new PropelQuerySubscriber($event->getArgumentAccess()));
26+
$dispatcher->addSubscriber(new PropelQuerySubscriber());
2727

2828
$this->isLoaded = true;
2929
}

Diff for: src/Knp/Component/Pager/Event/Subscriber/Filtration/PropelQuerySubscriber.php

+6-9
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,24 @@
22

33
namespace Knp\Component\Pager\Event\Subscriber\Filtration;
44

5-
use Knp\Component\Pager\ArgumentAccess\ArgumentAccessInterface;
65
use Knp\Component\Pager\Event\ItemsEvent;
76
use Knp\Component\Pager\Exception\InvalidValueException;
87
use Knp\Component\Pager\PaginatorInterface;
98
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
109

1110
class PropelQuerySubscriber implements EventSubscriberInterface
1211
{
13-
public function __construct(private readonly ArgumentAccessInterface $argumentAccess)
14-
{
15-
}
16-
1712
public function items(ItemsEvent $event): void
1813
{
1914
$query = $event->target;
15+
$argumentAccess = $event->getArgumentAccess();
16+
2017
if ($query instanceof \ModelCriteria) {
21-
if (!$this->argumentAccess->has($event->options[PaginatorInterface::FILTER_VALUE_PARAMETER_NAME])) {
18+
if (!$argumentAccess->has($event->options[PaginatorInterface::FILTER_VALUE_PARAMETER_NAME])) {
2219
return;
2320
}
24-
if ($this->argumentAccess->has($event->options[PaginatorInterface::FILTER_FIELD_PARAMETER_NAME])) {
25-
$columns = $this->argumentAccess->get($event->options[PaginatorInterface::FILTER_FIELD_PARAMETER_NAME]);
21+
if ($argumentAccess->has($event->options[PaginatorInterface::FILTER_FIELD_PARAMETER_NAME])) {
22+
$columns = $argumentAccess->get($event->options[PaginatorInterface::FILTER_FIELD_PARAMETER_NAME]);
2623
} elseif (!empty($event->options[PaginatorInterface::DEFAULT_FILTER_FIELDS])) {
2724
$columns = $event->options[PaginatorInterface::DEFAULT_FILTER_FIELDS];
2825
} else {
@@ -39,7 +36,7 @@ public function items(ItemsEvent $event): void
3936
}
4037
}
4138
}
42-
$value = $this->argumentAccess->get($event->options[PaginatorInterface::FILTER_VALUE_PARAMETER_NAME]);
39+
$value = $argumentAccess->get($event->options[PaginatorInterface::FILTER_VALUE_PARAMETER_NAME]);
4340
$criteria = \Criteria::EQUAL;
4441
if (str_contains($value, '*')) {
4542
$value = str_replace('*', '%', $value);

Diff for: src/Knp/Component/Pager/Event/Subscriber/Sortable/ArraySubscriber.php

+14-12
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class ArraySubscriber implements EventSubscriberInterface
2525

2626
private readonly ?PropertyAccessorInterface $propertyAccessor;
2727

28-
public function __construct(private readonly ArgumentAccessInterface $argumentAccess, ?PropertyAccessorInterface $accessor = null)
28+
public function __construct(?PropertyAccessorInterface $accessor = null)
2929
{
3030
if (!$accessor && class_exists(PropertyAccess::class)) {
3131
$accessor = PropertyAccess::createPropertyAccessorBuilder()->enableMagicCall()->getPropertyAccessor();
@@ -36,24 +36,26 @@ public function __construct(private readonly ArgumentAccessInterface $argumentAc
3636

3737
public function items(ItemsEvent $event): void
3838
{
39+
$argumentAccess = $event->getArgumentAccess();
40+
3941
// Check if the result has already been sorted by another sort subscriber
4042
$customPaginationParameters = $event->getCustomPaginationParameters();
4143
if (!empty($customPaginationParameters['sorted']) ) {
4244
return;
4345
}
4446
$sortField = $event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME];
45-
if (!is_array($event->target) || null === $sortField || !$this->argumentAccess->has($sortField)) {
47+
if (!is_array($event->target) || null === $sortField || !$argumentAccess->has($sortField)) {
4648
return;
4749
}
4850

4951
$event->setCustomPaginationParameter('sorted', true);
5052

51-
if (isset($event->options[PaginatorInterface::SORT_FIELD_ALLOW_LIST]) && !in_array($this->argumentAccess->get($sortField), $event->options[PaginatorInterface::SORT_FIELD_ALLOW_LIST])) {
52-
throw new InvalidValueException("Cannot sort by: [{$this->argumentAccess->get($sortField)}] this field is not in allow list.");
53+
if (isset($event->options[PaginatorInterface::SORT_FIELD_ALLOW_LIST]) && !in_array($argumentAccess->get($sortField), $event->options[PaginatorInterface::SORT_FIELD_ALLOW_LIST])) {
54+
throw new InvalidValueException("Cannot sort by: [{$argumentAccess->get($sortField)}] this field is not in allow list.");
5355
}
5456

5557
$sortFunction = $event->options['sortFunction'] ?? [$this, 'proxySortFunction'];
56-
$sortField = $this->argumentAccess->get($sortField);
58+
$sortField = $argumentAccess->get($sortField);
5759

5860
// compatibility layer
5961
if ($sortField[0] === '.') {
@@ -63,19 +65,19 @@ public function items(ItemsEvent $event): void
6365
call_user_func_array($sortFunction, [
6466
&$event->target,
6567
$sortField,
66-
$this->getSortDirection($event->options),
68+
$this->getSortDirection($event),
6769
]);
6870
}
6971

70-
/**
71-
* @param array<string, mixed> $options
72-
*/
73-
private function getSortDirection(array $options): string
72+
private function getSortDirection(ItemsEvent $event): string
7473
{
75-
if (!$this->argumentAccess->has($options[PaginatorInterface::SORT_DIRECTION_PARAMETER_NAME])) {
74+
$argumentAccess = $event->getArgumentAccess();
75+
$options = $event->options;
76+
77+
if (!$argumentAccess->has($options[PaginatorInterface::SORT_DIRECTION_PARAMETER_NAME])) {
7678
return 'desc';
7779
}
78-
$direction = $this->argumentAccess->get($options[PaginatorInterface::SORT_DIRECTION_PARAMETER_NAME]);
80+
$direction = $argumentAccess->get($options[PaginatorInterface::SORT_DIRECTION_PARAMETER_NAME]);
7981
if (strtolower($direction) === 'asc') {
8082
return 'asc';
8183
}

Diff for: src/Knp/Component/Pager/Event/Subscriber/Sortable/Doctrine/ODM/MongoDB/QuerySubscriber.php

+5-8
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,17 @@
33
namespace Knp\Component\Pager\Event\Subscriber\Sortable\Doctrine\ODM\MongoDB;
44

55
use Doctrine\ODM\MongoDB\Query\Query;
6-
use Knp\Component\Pager\ArgumentAccess\ArgumentAccessInterface;
76
use Knp\Component\Pager\Event\ItemsEvent;
87
use Knp\Component\Pager\Exception\InvalidValueException;
98
use Knp\Component\Pager\PaginatorInterface;
109
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1110

1211
class QuerySubscriber implements EventSubscriberInterface
1312
{
14-
public function __construct(private readonly ArgumentAccessInterface $argumentAccess)
15-
{
16-
}
17-
1813
public function items(ItemsEvent $event): void
1914
{
15+
$argumentAccess = $event->getArgumentAccess();
16+
2017
// Check if the result has already been sorted by another sort subscriber
2118
$customPaginationParameters = $event->getCustomPaginationParameters();
2219
if (!empty($customPaginationParameters['sorted']) ) {
@@ -27,9 +24,9 @@ public function items(ItemsEvent $event): void
2724
$event->setCustomPaginationParameter('sorted', true);
2825
$sortField = $event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME];
2926
$sortDir = $event->options[PaginatorInterface::SORT_DIRECTION_PARAMETER_NAME];
30-
if (null !== $sortField && $this->argumentAccess->has($sortField)) {
31-
$field = $this->argumentAccess->get($sortField);
32-
$dir = null !== $sortDir && strtolower($this->argumentAccess->get($sortDir)) === 'asc' ? 1 : -1;
27+
if (null !== $sortField && $argumentAccess->has($sortField)) {
28+
$field = $argumentAccess->get($sortField);
29+
$dir = null !== $sortDir && strtolower($argumentAccess->get($sortDir)) === 'asc' ? 1 : -1;
3330

3431
if (isset($event->options[PaginatorInterface::SORT_FIELD_ALLOW_LIST]) && (!in_array($field, $event->options[PaginatorInterface::SORT_FIELD_ALLOW_LIST]))) {
3532
throw new InvalidValueException("Cannot sort by: [$field] this field is not in allow list.");

Diff for: src/Knp/Component/Pager/Event/Subscriber/Sortable/Doctrine/ORM/QuerySubscriber.php

+7-10
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Knp\Component\Pager\Event\Subscriber\Sortable\Doctrine\ORM;
44

55
use Doctrine\ORM\Query;
6-
use Knp\Component\Pager\ArgumentAccess\ArgumentAccessInterface;
76
use Knp\Component\Pager\Event\ItemsEvent;
87
use Knp\Component\Pager\Event\Subscriber\Paginate\Doctrine\ORM\Query\Helper as QueryHelper;
98
use Knp\Component\Pager\Event\Subscriber\Sortable\Doctrine\ORM\Query\OrderByWalker;
@@ -13,12 +12,10 @@
1312

1413
class QuerySubscriber implements EventSubscriberInterface
1514
{
16-
public function __construct(private readonly ArgumentAccessInterface $argumentAccess)
17-
{
18-
}
19-
2015
public function items(ItemsEvent $event): void
2116
{
17+
$argumentAccess = $event->getArgumentAccess();
18+
2219
// Check if the result has already been sorted by another sort subscriber
2320
$customPaginationParameters = $event->getCustomPaginationParameters();
2421
if (!empty($customPaginationParameters['sorted']) ) {
@@ -29,14 +26,14 @@ public function items(ItemsEvent $event): void
2926
$event->setCustomPaginationParameter('sorted', true);
3027
$sortField = $event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME];
3128
$sortDir = $event->options[PaginatorInterface::SORT_DIRECTION_PARAMETER_NAME];
32-
if (null !== $sortField && $this->argumentAccess->has($sortField)) {
33-
$dir = null !== $sortDir && $this->argumentAccess->has($sortDir) && strtolower($this->argumentAccess->get($sortDir)) === 'asc' ? 'asc' : 'desc';
29+
if (null !== $sortField && $argumentAccess->has($sortField)) {
30+
$dir = null !== $sortDir && $argumentAccess->has($sortDir) && strtolower($argumentAccess->get($sortDir)) === 'asc' ? 'asc' : 'desc';
3431

35-
if (isset($event->options[PaginatorInterface::SORT_FIELD_ALLOW_LIST]) && !in_array($this->argumentAccess->get($sortField), $event->options[PaginatorInterface::SORT_FIELD_ALLOW_LIST])) {
36-
throw new InvalidValueException("Cannot sort by: [{$this->argumentAccess->get($sortField)}] this field is not in allow list.");
32+
if (isset($event->options[PaginatorInterface::SORT_FIELD_ALLOW_LIST]) && !in_array($argumentAccess->get($sortField), $event->options[PaginatorInterface::SORT_FIELD_ALLOW_LIST])) {
33+
throw new InvalidValueException("Cannot sort by: [{$argumentAccess->get($sortField)}] this field is not in allow list.");
3734
}
3835

39-
$sortFieldParameterNames = $this->argumentAccess->get($sortField);
36+
$sortFieldParameterNames = $argumentAccess->get($sortField);
4037
$fields = [];
4138
$aliases = [];
4239
if (!is_string($sortFieldParameterNames)) {

Diff for: src/Knp/Component/Pager/Event/Subscriber/Sortable/ElasticaQuerySubscriber.php

+5-8
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,17 @@
44

55
use Elastica\Query;
66
use Elastica\SearchableInterface;
7-
use Knp\Component\Pager\ArgumentAccess\ArgumentAccessInterface;
87
use Knp\Component\Pager\Event\ItemsEvent;
98
use Knp\Component\Pager\Exception\InvalidValueException;
109
use Knp\Component\Pager\PaginatorInterface;
1110
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1211

1312
class ElasticaQuerySubscriber implements EventSubscriberInterface
1413
{
15-
public function __construct(private readonly ArgumentAccessInterface $argumentAccess)
16-
{
17-
}
18-
1914
public function items(ItemsEvent $event): void
2015
{
16+
$argumentAccess = $event->getArgumentAccess();
17+
2118
// Check if the result has already been sorted by another sort subscriber
2219
$customPaginationParameters = $event->getCustomPaginationParameters();
2320
if (!empty($customPaginationParameters['sorted']) ) {
@@ -29,9 +26,9 @@ public function items(ItemsEvent $event): void
2926
$event->setCustomPaginationParameter('sorted', true);
3027
$sortField = $event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME];
3128
$sortDir = $event->options[PaginatorInterface::SORT_DIRECTION_PARAMETER_NAME];
32-
if (null !== $sortField && $this->argumentAccess->has($sortField)) {
33-
$field = $this->argumentAccess->get($sortField);
34-
$dir = null !== $sortDir && $this->argumentAccess->has($sortDir) && strtolower($this->argumentAccess->get($sortDir)) === 'asc' ? 'asc' : 'desc';
29+
if (null !== $sortField && $argumentAccess->has($sortField)) {
30+
$field = $argumentAccess->get($sortField);
31+
$dir = null !== $sortDir && $argumentAccess->has($sortDir) && strtolower($argumentAccess->get($sortDir)) === 'asc' ? 'asc' : 'desc';
3532

3633
if (isset($event->options[PaginatorInterface::SORT_FIELD_ALLOW_LIST]) && !in_array($field, $event->options[PaginatorInterface::SORT_FIELD_ALLOW_LIST])) {
3734
throw new InvalidValueException(sprintf('Cannot sort by: [%s] this field is not in allow list.', $field));

Diff for: src/Knp/Component/Pager/Event/Subscriber/Sortable/PropelQuerySubscriber.php

+7-10
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,17 @@
22

33
namespace Knp\Component\Pager\Event\Subscriber\Sortable;
44

5-
use Knp\Component\Pager\ArgumentAccess\ArgumentAccessInterface;
65
use Knp\Component\Pager\Event\ItemsEvent;
76
use Knp\Component\Pager\Exception\InvalidValueException;
87
use Knp\Component\Pager\PaginatorInterface;
98
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
109

1110
class PropelQuerySubscriber implements EventSubscriberInterface
1211
{
13-
public function __construct(private readonly ArgumentAccessInterface $argumentAccess)
14-
{
15-
}
16-
1712
public function items(ItemsEvent $event): void
1813
{
14+
$argumentAccess = $event->getArgumentAccess();
15+
1916
// Check if the result has already been sorted by another sort subscriber
2017
$customPaginationParameters = $event->getCustomPaginationParameters();
2118
if (!empty($customPaginationParameters['sorted']) ) {
@@ -27,13 +24,13 @@ public function items(ItemsEvent $event): void
2724
$event->setCustomPaginationParameter('sorted', true);
2825
$sortField = $event->options[PaginatorInterface::SORT_FIELD_PARAMETER_NAME];
2926
$sortDir = $event->options[PaginatorInterface::SORT_DIRECTION_PARAMETER_NAME];
30-
if (null !== $sortField && $this->argumentAccess->has($sortField)) {
31-
$part = $this->argumentAccess->get($sortField);
32-
$direction = (null !== $sortDir && $this->argumentAccess->has($sortDir) && strtolower($this->argumentAccess->get($sortDir)) === 'asc')
27+
if (null !== $sortField && $argumentAccess->has($sortField)) {
28+
$part = $argumentAccess->get($sortField);
29+
$direction = (null !== $sortDir && $argumentAccess->has($sortDir) && strtolower($argumentAccess->get($sortDir)) === 'asc')
3330
? 'asc' : 'desc';
3431

35-
if (isset($event->options[PaginatorInterface::SORT_FIELD_ALLOW_LIST]) && !in_array($this->argumentAccess->get($sortField), $event->options[PaginatorInterface::SORT_FIELD_ALLOW_LIST])) {
36-
throw new InvalidValueException("Cannot sort by: [{$this->argumentAccess->get($sortField)}] this field is not in allow list.");
32+
if (isset($event->options[PaginatorInterface::SORT_FIELD_ALLOW_LIST]) && !in_array($argumentAccess->get($sortField), $event->options[PaginatorInterface::SORT_FIELD_ALLOW_LIST])) {
33+
throw new InvalidValueException("Cannot sort by: [{$argumentAccess->get($sortField)}] this field is not in allow list.");
3734
}
3835

3936
$query->orderBy($part, $direction);

0 commit comments

Comments
 (0)