@@ -107,7 +107,13 @@ public function normalize(mixed $object, ?string $format = null, array $context
107
107
108
108
if ($ currentFilters || ($ parameters && \count ($ parameters ))) {
109
109
$ 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
+ ];
111
117
}
112
118
113
119
return $ data ;
@@ -123,91 +129,6 @@ public function setNormalizer(NormalizerInterface $normalizer): void
123
129
}
124
130
}
125
131
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
-
211
132
/**
212
133
* Gets a filter with a backward compatibility.
213
134
*/
0 commit comments