Skip to content

Commit 821fd43

Browse files
committed
Support new query parameters in collection
1 parent adaee45 commit 821fd43

File tree

2 files changed

+15
-30
lines changed

2 files changed

+15
-30
lines changed

src/EventListener/Api/CollectionApiEventListener.php

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -40,34 +40,17 @@ class CollectionApiEventListener
4040
{
4141
use UriVariablesResolverTrait;
4242

43-
private ApiResourceRouteFinder $resourceRouteFinder;
44-
private ProviderInterface $provider;
45-
private RequestStack $requestStack;
46-
private SerializerContextBuilderInterface $serializerContextBuilder;
47-
private NormalizerInterface $itemNormalizer;
48-
private SerializeFormatResolver $serializeFormatResolver;
49-
private string $itemsPerPageParameterName;
50-
private ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory;
51-
5243
public function __construct(
53-
ApiResourceRouteFinder $resourceRouteFinder,
54-
ProviderInterface $provider,
55-
RequestStack $requestStack,
56-
SerializerContextBuilderInterface $serializerContextBuilder,
57-
NormalizerInterface $itemNormalizer,
58-
SerializeFormatResolver $serializeFormatResolver,
59-
ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory,
60-
string $itemsPerPageParameterName,
61-
) {
62-
$this->resourceRouteFinder = $resourceRouteFinder;
63-
$this->provider = $provider;
64-
$this->requestStack = $requestStack;
65-
$this->serializerContextBuilder = $serializerContextBuilder;
66-
$this->itemNormalizer = $itemNormalizer;
67-
$this->serializeFormatResolver = $serializeFormatResolver;
68-
$this->itemsPerPageParameterName = $itemsPerPageParameterName;
69-
$this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
70-
}
44+
private readonly ApiResourceRouteFinder $resourceRouteFinder,
45+
private readonly ProviderInterface $provider,
46+
private readonly RequestStack $requestStack,
47+
private readonly SerializerContextBuilderInterface $serializerContextBuilder,
48+
private readonly NormalizerInterface $itemNormalizer,
49+
private readonly SerializeFormatResolver $serializeFormatResolver,
50+
private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory,
51+
private readonly ?ProviderInterface $parameterProvider,
52+
private readonly string $itemsPerPageParameterName,
53+
) {}
7154

7255
public function supportsTransformation($data, string $to, array $context = []): bool
7356
{
@@ -104,7 +87,7 @@ private function transform(Collection $object): Collection
10487
}
10588

10689
// Build context
107-
$collectionContext = ['operation' => $getCollectionOperation];
90+
$collectionContext = [ 'operation' => $getCollectionOperation, 'resource_class' => $resourceClass ];
10891

10992
// Build filters
11093
$filters = [];
@@ -123,15 +106,16 @@ private function transform(Collection $object): Collection
123106
// e.g. a default search value could be overridden by no search value
124107
$filters = array_merge($filters, $requestFilters);
125108
}
109+
126110
$collectionContext['filters'] = $filters;
127111

128112
// Compose context for provider
129113
$collectionContext += $normalizationContext = $this->serializerContextBuilder->createFromRequest($request, true, $attributes);
130-
131114
try {
132115
$uriVariables = $this->getOperationUriVariables($getCollectionOperation, $parameters, $resourceClass);
116+
$this->parameterProvider->provide($getCollectionOperation, $uriVariables, [ ...$collectionContext, 'request' => clone $request, 'uri_variables' => $uriVariables ]);
133117
// Operation $operation, array $uriVariables = [], array $context = []
134-
$collectionData = $this->provider->provide($getCollectionOperation, $uriVariables, $collectionContext);
118+
$collectionData = $this->provider->provide($getCollectionOperation, $uriVariables, [ ...$collectionContext, 'request' => $request, 'uri_variables' => $uriVariables ]);
135119
} catch (InvalidIdentifierException $e) {
136120
throw new NotFoundHttpException('Invalid identifier value or configuration.', $e);
137121
}

src/Resources/config/services.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@
305305
new Reference(NormalizerInterface::class),
306306
new Reference(SerializeFormatResolver::class),
307307
new Reference('api_platform.metadata.resource.metadata_collection_factory'),
308+
new Reference('api_platform.state_provider.parameter'),
308309
]
309310
)
310311
->tag('kernel.event_listener', ['event' => ViewEvent::class, 'priority' => EventPriorities::PRE_SERIALIZE, 'method' => 'onPreSerialize']);

0 commit comments

Comments
 (0)