@@ -25,7 +25,7 @@ class ArraySubscriber implements EventSubscriberInterface
25
25
26
26
private readonly ?PropertyAccessorInterface $ propertyAccessor ;
27
27
28
- public function __construct (private readonly ArgumentAccessInterface $ argumentAccess , ?PropertyAccessorInterface $ accessor = null )
28
+ public function __construct (?PropertyAccessorInterface $ accessor = null )
29
29
{
30
30
if (!$ accessor && class_exists (PropertyAccess::class)) {
31
31
$ accessor = PropertyAccess::createPropertyAccessorBuilder ()->enableMagicCall ()->getPropertyAccessor ();
@@ -36,24 +36,26 @@ public function __construct(private readonly ArgumentAccessInterface $argumentAc
36
36
37
37
public function items (ItemsEvent $ event ): void
38
38
{
39
+ $ argumentAccess = $ event ->getArgumentAccess ();
40
+
39
41
// Check if the result has already been sorted by another sort subscriber
40
42
$ customPaginationParameters = $ event ->getCustomPaginationParameters ();
41
43
if (!empty ($ customPaginationParameters ['sorted ' ]) ) {
42
44
return ;
43
45
}
44
46
$ 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 )) {
46
48
return ;
47
49
}
48
50
49
51
$ event ->setCustomPaginationParameter ('sorted ' , true );
50
52
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. " );
53
55
}
54
56
55
57
$ sortFunction = $ event ->options ['sortFunction ' ] ?? [$ this , 'proxySortFunction ' ];
56
- $ sortField = $ this -> argumentAccess ->get ($ sortField );
58
+ $ sortField = $ argumentAccess ->get ($ sortField );
57
59
58
60
// compatibility layer
59
61
if ($ sortField [0 ] === '. ' ) {
@@ -63,19 +65,19 @@ public function items(ItemsEvent $event): void
63
65
call_user_func_array ($ sortFunction , [
64
66
&$ event ->target ,
65
67
$ sortField ,
66
- $ this ->getSortDirection ($ event-> options ),
68
+ $ this ->getSortDirection ($ event ),
67
69
]);
68
70
}
69
71
70
- /**
71
- * @param array<string, mixed> $options
72
- */
73
- private function getSortDirection (array $ options ): string
72
+ private function getSortDirection (ItemsEvent $ event ): string
74
73
{
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 ])) {
76
78
return 'desc ' ;
77
79
}
78
- $ direction = $ this -> argumentAccess ->get ($ options [PaginatorInterface::SORT_DIRECTION_PARAMETER_NAME ]);
80
+ $ direction = $ argumentAccess ->get ($ options [PaginatorInterface::SORT_DIRECTION_PARAMETER_NAME ]);
79
81
if (strtolower ($ direction ) === 'asc ' ) {
80
82
return 'asc ' ;
81
83
}
0 commit comments