-
-
Notifications
You must be signed in to change notification settings - Fork 838
/
CollectionFiltersNormalizer.php
218 lines (187 loc) · 8.81 KB
/
CollectionFiltersNormalizer.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\Hydra\Serializer;
use ApiPlatform\Api\FilterInterface as LegacyFilterInterface;
use ApiPlatform\Api\ResourceClassResolverInterface as LegacyResourceClassResolverInterface;
use ApiPlatform\Doctrine\Odm\State\Options as ODMOptions;
use ApiPlatform\Doctrine\Orm\State\Options;
use ApiPlatform\Metadata\FilterInterface;
use ApiPlatform\Metadata\Parameter;
use ApiPlatform\Metadata\Parameters;
use ApiPlatform\Metadata\QueryParameterInterface;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\Metadata\ResourceClassResolverInterface;
use ApiPlatform\Serializer\CacheableSupportsMethodInterface;
use Psr\Container\ContainerInterface;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface as BaseCacheableSupportsMethodInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Serializer;
/**
* Enhances the result of collection by adding the filters applied on collection.
*
* @author Samuel ROZE <samuel.roze@gmail.com>
*/
final class CollectionFiltersNormalizer implements NormalizerInterface, NormalizerAwareInterface, CacheableSupportsMethodInterface
{
private ?ContainerInterface $filterLocator = null;
/**
* @param ContainerInterface $filterLocator The new filter locator or the deprecated filter collection
*/
public function __construct(private readonly NormalizerInterface $collectionNormalizer, private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, private readonly LegacyResourceClassResolverInterface|ResourceClassResolverInterface $resourceClassResolver, ContainerInterface $filterLocator)
{
$this->filterLocator = $filterLocator;
}
/**
* {@inheritdoc}
*/
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
{
return $this->collectionNormalizer->supportsNormalization($data, $format, $context);
}
public function getSupportedTypes($format): array
{
// @deprecated remove condition when support for symfony versions under 6.3 is dropped
if (!method_exists($this->collectionNormalizer, 'getSupportedTypes')) {
return ['*' => $this->collectionNormalizer instanceof BaseCacheableSupportsMethodInterface && $this->collectionNormalizer->hasCacheableSupportsMethod()];
}
return $this->collectionNormalizer->getSupportedTypes($format);
}
public function hasCacheableSupportsMethod(): bool
{
if (method_exists(Serializer::class, 'getSupportedTypes')) {
trigger_deprecation(
'api-platform/core',
'3.1',
'The "%s()" method is deprecated, use "getSupportedTypes()" instead.',
__METHOD__
);
}
return $this->collectionNormalizer instanceof BaseCacheableSupportsMethodInterface && $this->collectionNormalizer->hasCacheableSupportsMethod();
}
/**
* {@inheritdoc}
*/
public function normalize(mixed $object, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
{
if (($context[AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS] ?? false) && $object instanceof \ArrayObject && !\count($object)) {
return $object;
}
$data = $this->collectionNormalizer->normalize($object, $format, $context);
if (!isset($context['resource_class']) || isset($context['api_sub_level'])) {
return $data;
}
if (!\is_array($data)) {
throw new UnexpectedValueException('Expected data to be an array');
}
$resourceClass = $this->resourceClassResolver->getResourceClass($object, $context['resource_class']);
$operation = $context['operation'] ?? $this->resourceMetadataCollectionFactory->create($resourceClass)->getOperation($context['operation_name'] ?? null);
$parameters = $operation->getParameters();
$resourceFilters = $operation->getFilters();
if (!$resourceFilters && !$parameters) {
return $data;
}
$requestParts = parse_url($context['request_uri'] ?? '');
if (!\is_array($requestParts)) {
return $data;
}
$currentFilters = [];
foreach ($resourceFilters as $filterId) {
if ($filter = $this->getFilter($filterId)) {
$currentFilters[] = $filter;
}
}
if ($options = $operation->getStateOptions()) {
if ($options instanceof Options && $options->getEntityClass()) {
$resourceClass = $options->getEntityClass();
}
if ($options instanceof ODMOptions && $options->getDocumentClass()) {
$resourceClass = $options->getDocumentClass();
}
}
if ($currentFilters || ($parameters && \count($parameters))) {
$data['hydra:search'] = $this->getSearch($resourceClass, $requestParts, $currentFilters, $parameters);
}
return $data;
}
/**
* {@inheritdoc}
*/
public function setNormalizer(NormalizerInterface $normalizer): void
{
if ($this->collectionNormalizer instanceof NormalizerAwareInterface) {
$this->collectionNormalizer->setNormalizer($normalizer);
}
}
/**
* Returns the content of the Hydra search property.
*
* @param LegacyFilterInterface[]|FilterInterface[] $filters
* @param array<string, Parameter> $parameters
*/
private function getSearch(string $resourceClass, array $parts, array $filters, array|Parameters|null $parameters): array
{
$variables = [];
$mapping = [];
foreach ($filters as $filter) {
foreach ($filter->getDescription($resourceClass) as $variable => $data) {
$variables[] = $variable;
$mapping[] = ['@type' => 'IriTemplateMapping', 'variable' => $variable, 'property' => $data['property'], 'required' => $data['required']];
}
}
foreach ($parameters ?? [] as $key => $parameter) {
// Each IriTemplateMapping maps a variable used in the template to a property
if (!$parameter instanceof QueryParameterInterface) {
continue;
}
if (!($property = $parameter->getProperty()) && ($filterId = $parameter->getFilter()) && ($filter = $this->getFilter($filterId))) {
foreach ($filter->getDescription($resourceClass) ?? [] as $variable => $description) {
// This is a practice induced by PHP and is not necessary when implementing URI template
if (str_ends_with((string) $variable, '[]')) {
continue;
}
// :property is a pattern allowed when defining parameters
$k = str_replace(':property', $description['property'], $key);
$variable = str_replace($description['property'], $k, $variable);
$variables[] = $variable;
$m = ['@type' => 'IriTemplateMapping', 'variable' => $variable, 'property' => $description['property'], 'required' => $description['required']];
if (null !== ($required = $parameter->getRequired())) {
$m['required'] = $required;
}
$mapping[] = $m;
}
continue;
}
if (!$property) {
continue;
}
$m = ['@type' => 'IriTemplateMapping', 'variable' => $key, 'property' => $property];
$variables[] = $key;
if (null !== ($required = $parameter->getRequired())) {
$m['required'] = $required;
}
$mapping[] = $m;
}
return ['@type' => 'hydra:IriTemplate', 'hydra:template' => sprintf('%s{?%s}', $parts['path'], implode(',', $variables)), 'hydra:variableRepresentation' => 'BasicRepresentation', 'hydra:mapping' => $mapping];
}
/**
* Gets a filter with a backward compatibility.
*/
private function getFilter(string $filterId): LegacyFilterInterface|FilterInterface|null
{
if ($this->filterLocator && $this->filterLocator->has($filterId)) {
return $this->filterLocator->get($filterId);
}
return null;
}
}