Skip to content

Commit 23f449a

Browse files
committed
tt
1 parent 7452d19 commit 23f449a

File tree

2 files changed

+11
-90
lines changed

2 files changed

+11
-90
lines changed

src/Hydra/Serializer/CollectionFiltersNormalizer.php

Lines changed: 7 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,13 @@ public function normalize(mixed $object, ?string $format = null, array $context
107107

108108
if ($currentFilters || ($parameters && \count($parameters))) {
109109
$hydraPrefix = $this->getHydraPrefix($context + $this->defaultContext);
110-
$data[$hydraPrefix.'search'] = $this->getSearch($resourceClass, $requestParts, $currentFilters, $parameters, $hydraPrefix);
110+
['mapping' => $mapping, 'keys' => $keys] = $this->getSearchMappingAndKeys($operation, $resourceClass, $currentFilters, $parameters, [$this, 'getFilter']);
111+
$data[$hydraPrefix.'search'] = [
112+
'@type' => $hydraPrefix.'IriTemplate',
113+
$hydraPrefix.'template' => \sprintf('%s{?%s}', $requestParts['path'], implode(',', $keys)),
114+
$hydraPrefix.'variableRepresentation' => 'BasicRepresentation',
115+
$hydraPrefix.'mapping' => $mapping,
116+
];
111117
}
112118

113119
return $data;
@@ -123,91 +129,6 @@ public function setNormalizer(NormalizerInterface $normalizer): void
123129
}
124130
}
125131

126-
/**
127-
* Returns the content of the Hydra search property.
128-
*
129-
* @param FilterInterface[] $filters
130-
*/
131-
private function getSearch(string $resourceClass, array $parts, array $filters, ?Parameters $parameters, string $hydraPrefix): array
132-
{
133-
$variables = [];
134-
$mapping = [];
135-
foreach ($filters as $filter) {
136-
foreach ($filter->getDescription($resourceClass) as $variable => $data) {
137-
$variables[] = $variable;
138-
$mapping[] = ['@type' => 'IriTemplateMapping', 'variable' => $variable, 'property' => $data['property'] ?? null, 'required' => $data['required'] ?? false];
139-
}
140-
}
141-
142-
foreach ($parameters ?? [] as $key => $parameter) {
143-
// Each IriTemplateMapping maps a variable used in the template to a property
144-
if (!$parameter instanceof QueryParameterInterface || false === $parameter->getHydra()) {
145-
continue;
146-
}
147-
148-
if (($filterId = $parameter->getFilter()) && \is_string($filterId) && ($filter = $this->getFilter($filterId))) {
149-
$filterDescription = $filter->getDescription($resourceClass);
150-
151-
foreach ($filterDescription as $variable => $description) {
152-
// // This is a practice induced by PHP and is not necessary when implementing URI template
153-
if (str_ends_with((string) $variable, '[]')) {
154-
continue;
155-
}
156-
157-
if (!($descriptionProperty = $description['property'] ?? null)) {
158-
continue;
159-
}
160-
161-
if (($prop = $parameter->getProperty()) && $descriptionProperty !== $prop) {
162-
continue;
163-
}
164-
165-
// :property is a pattern allowed when defining parameters
166-
$k = str_replace(':property', $descriptionProperty, $key);
167-
$variable = str_replace($descriptionProperty, $k, $variable);
168-
$variables[] = $variable;
169-
$m = ['@type' => 'IriTemplateMapping', 'variable' => $variable, 'property' => $descriptionProperty];
170-
if (null !== ($required = $parameter->getRequired() ?? $description['required'] ?? null)) {
171-
$m['required'] = $required;
172-
}
173-
$mapping[] = $m;
174-
}
175-
176-
if ($filterDescription) {
177-
continue;
178-
}
179-
}
180-
181-
if (str_contains($key, ':property') && $parameter->getProperties()) {
182-
$required = $parameter->getRequired();
183-
foreach ($parameter->getProperties() as $prop) {
184-
$k = str_replace(':property', $prop, $key);
185-
$m = ['@type' => 'IriTemplateMapping', 'variable' => $k, 'property' => $prop];
186-
$variables[] = $k;
187-
if (null !== $required) {
188-
$m['required'] = $required;
189-
}
190-
$mapping[] = $m;
191-
}
192-
193-
continue;
194-
}
195-
196-
if (!($property = $parameter->getProperty())) {
197-
continue;
198-
}
199-
200-
$m = ['@type' => 'IriTemplateMapping', 'variable' => $key, 'property' => $property];
201-
$variables[] = $key;
202-
if (null !== ($required = $parameter->getRequired())) {
203-
$m['required'] = $required;
204-
}
205-
$mapping[] = $m;
206-
}
207-
208-
return ['@type' => $hydraPrefix.'IriTemplate', $hydraPrefix.'template' => \sprintf('%s{?%s}', $parts['path'], implode(',', $variables)), $hydraPrefix.'variableRepresentation' => 'BasicRepresentation', $hydraPrefix.'mapping' => $mapping];
209-
}
210-
211132
/**
212133
* Gets a filter with a backward compatibility.
213134
*/

src/Hydra/State/Util/SearchHelperTrait.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ trait SearchHelperTrait
2525
/**
2626
* @param FilterInterface[] $filters
2727
*/
28-
private function getSearch(string $path, ?Operation $operation = null, ?string $resourceClass = null, ?array $filters = [], ?Parameters $parameters = null): IriTemplate
28+
private function getSearch(string $path, ?Operation $operation = null, ?string $resourceClass = null, ?array $filters = [], ?Parameters $parameters = null, ?callable $getFilter = null): IriTemplate
2929
{
30-
['mapping' => $mapping, 'keys' => $keys] = $this->getSearchMappingAndKeys($operation, $resourceClass, $filters, $parameters);
30+
['mapping' => $mapping, 'keys' => $keys] = $this->getSearchMappingAndKeys($operation, $resourceClass, $filters, $parameters, $getFilter);
3131

3232
return new IriTemplate(
3333
variableRepresentation: 'BasicRepresentation',
@@ -41,7 +41,7 @@ private function getSearch(string $path, ?Operation $operation = null, ?string $
4141
*
4242
* @return array{mapping: list<IriTemplateMapping>, keys: list<string>}
4343
*/
44-
private function getSearchMappingAndKeys(?Operation $operation = null, ?string $resourceClass = null, ?array $filters = [], ?Parameters $parameters = null): array
44+
private function getSearchMappingAndKeys(?Operation $operation = null, ?string $resourceClass = null, ?array $filters = [], ?Parameters $parameters = null, ?callable $getFilter = null): array
4545
{
4646
$mapping = [];
4747
$keys = [];
@@ -62,7 +62,7 @@ private function getSearchMappingAndKeys(?Operation $operation = null, ?string $
6262
continue;
6363
}
6464

65-
if (($filterId = $parameter->getFilter()) && \is_string($filterId) && ($filter = $this->getFilter($filterId))) {
65+
if ($getFilter && ($filterId = $parameter->getFilter()) && \is_string($filterId) && ($filter = $getFilter($filterId))) {
6666
$filterDescription = $filter->getDescription($resourceClass);
6767

6868
foreach ($filterDescription as $variable => $description) {

0 commit comments

Comments
 (0)