15
15
use Symfony \Component \HttpKernel \Attribute \MapQueryParameter ;
16
16
use Symfony \Component \HttpKernel \Controller \ValueResolverInterface ;
17
17
use Symfony \Component \HttpKernel \ControllerMetadata \ArgumentMetadata ;
18
- use Symfony \Component \HttpKernel \Exception \NotFoundHttpException ;
18
+ use Symfony \Component \HttpKernel \Exception \HttpException ;
19
19
20
20
/**
21
21
* Resolve arguments of type: array, string, int, float, bool, \BackedEnum from query parameters.
22
22
*
23
23
* @author Ruud Kamphuis <ruud@ticketswap.com>
24
24
* @author Nicolas Grekas <p@tchwork.com>
25
25
* @author Mateusz Anders <anders_mateusz@outlook.com>
26
+ * @author Ionut Enache <i.ovidiuenache@yahoo.com>
26
27
*/
27
28
final class QueryParameterValueResolver implements ValueResolverInterface
28
29
{
@@ -33,12 +34,14 @@ public function resolve(Request $request, ArgumentMetadata $argument): array
33
34
}
34
35
35
36
$ name = $ attribute ->name ?? $ argument ->getName ();
37
+ $ validationFailedCode = $ attribute ->validationFailedStatusCode ;
38
+
36
39
if (!$ request ->query ->has ($ name )) {
37
40
if ($ argument ->isNullable () || $ argument ->hasDefaultValue ()) {
38
41
return [];
39
42
}
40
43
41
- throw new NotFoundHttpException ( sprintf ('Missing query parameter "%s". ' , $ name ));
44
+ throw new HttpException ( $ validationFailedCode , sprintf ('Missing query parameter "%s". ' , $ name ));
42
45
}
43
46
44
47
$ value = $ request ->query ->all ()[$ name ];
@@ -52,7 +55,7 @@ public function resolve(Request $request, ArgumentMetadata $argument): array
52
55
$ filtered = array_values (array_filter ((array ) $ value , \is_array (...)));
53
56
54
57
if ($ filtered !== $ value && !($ attribute ->flags & \FILTER_NULL_ON_FAILURE )) {
55
- throw new NotFoundHttpException ( sprintf ('Invalid query parameter "%s". ' , $ name ));
58
+ throw new HttpException ( $ validationFailedCode , sprintf ('Invalid query parameter "%s". ' , $ name ));
56
59
}
57
60
58
61
return $ filtered ;
@@ -103,7 +106,7 @@ public function resolve(Request $request, ArgumentMetadata $argument): array
103
106
}
104
107
105
108
if (null === $ value && !($ attribute ->flags & \FILTER_NULL_ON_FAILURE )) {
106
- throw new NotFoundHttpException ( sprintf ('Invalid query parameter "%s". ' , $ name ));
109
+ throw new HttpException ( $ validationFailedCode , sprintf ('Invalid query parameter "%s". ' , $ name ));
107
110
}
108
111
109
112
if (!\is_array ($ value )) {
@@ -117,7 +120,7 @@ public function resolve(Request $request, ArgumentMetadata $argument): array
117
120
}
118
121
119
122
if ($ filtered !== $ value && !($ attribute ->flags & \FILTER_NULL_ON_FAILURE )) {
120
- throw new NotFoundHttpException ( sprintf ('Invalid query parameter "%s". ' , $ name ));
123
+ throw new HttpException ( $ validationFailedCode , sprintf ('Invalid query parameter "%s". ' , $ name ));
121
124
}
122
125
123
126
return $ argument ->isVariadic () ? $ filtered : [$ filtered ];
0 commit comments