Skip to content

Commit 909c1fe

Browse files
minor #59514 [HttpKernel] Improve MapQueryParameter handling of options (nicolas-grekas)
This PR was merged into the 7.3 branch. Discussion ---------- [HttpKernel] Improve MapQueryParameter handling of options | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | - | License | MIT Making the attribute better documented and more natural to use. Commits ------- 496573f [HttpKernel] Improve MapQueryParameter handling of options
2 parents 0134078 + 496573f commit 909c1fe

File tree

3 files changed

+15
-15
lines changed

3 files changed

+15
-15
lines changed

src/Symfony/Component/HttpKernel/Attribute/MapQueryParameter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ final class MapQueryParameter extends ValueResolver
2828
* @see https://php.net/manual/filter.constants for filter, flags and options
2929
*
3030
* @param string|null $name The name of the query parameter; if null, the name of the argument in the controller will be used
31-
* @param (FILTER_VALIDATE_*)|(FILTER_SANITIZE_*)|null $filter The filter to pass to "filter_var()"
32-
* @param int-mask-of<(FILTER_FLAG_*)|FILTER_NULL_ON_FAILURE> $flags The flags to pass to "filter_var()"
33-
* @param array $options The options to pass to "filter_var()"
31+
* @param (FILTER_VALIDATE_*)|(FILTER_SANITIZE_*)|null $filter The filter to pass to "filter_var()", deduced from the type-hint if null
32+
* @param int-mask-of<(FILTER_FLAG_*)|FILTER_NULL_ON_FAILURE> $flags
33+
* @param array{min_range?: int|float, max_range?: int|float, regexp?: string, ...} $options
3434
* @param class-string<ValueResolverInterface>|string $resolver The name of the resolver to use
3535
*/
3636
public function __construct(

src/Symfony/Component/HttpKernel/Controller/ArgumentResolver/QueryParameterValueResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function resolve(Request $request, ArgumentMetadata $argument): array
7676
$enumType = null;
7777
$filter = match ($type) {
7878
'array' => \FILTER_DEFAULT,
79-
'string' => \FILTER_DEFAULT,
79+
'string' => isset($attribute->options['regexp']) ? \FILTER_VALIDATE_REGEXP : \FILTER_DEFAULT,
8080
'int' => \FILTER_VALIDATE_INT,
8181
'float' => \FILTER_VALIDATE_FLOAT,
8282
'bool' => \FILTER_VALIDATE_BOOL,

src/Symfony/Component/HttpKernel/Tests/Controller/ArgumentResolver/QueryParameterValueResolverTest.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,50 +108,50 @@ public static function validDataProvider(): iterable
108108

109109
yield 'parameter found and string with regexp filter that matches' => [
110110
Request::create('/', 'GET', ['firstName' => 'John']),
111-
new ArgumentMetadata('firstName', 'string', false, false, false, attributes: [new MapQueryParameter(filter: \FILTER_VALIDATE_REGEXP, flags: \FILTER_NULL_ON_FAILURE, options: ['regexp' => '/John/'])]),
111+
new ArgumentMetadata('firstName', 'string', false, false, false, attributes: [new MapQueryParameter(options: ['regexp' => '/John/'])]),
112112
['John'],
113113
];
114114

115115
yield 'parameter found and string with regexp filter that falls back to null on failure' => [
116116
Request::create('/', 'GET', ['firstName' => 'Fabien']),
117-
new ArgumentMetadata('firstName', 'string', false, false, false, attributes: [new MapQueryParameter(filter: \FILTER_VALIDATE_REGEXP, flags: \FILTER_NULL_ON_FAILURE, options: ['regexp' => '/John/'])]),
117+
new ArgumentMetadata('firstName', 'string', false, false, false, attributes: [new MapQueryParameter(flags: \FILTER_NULL_ON_FAILURE, options: ['regexp' => '/John/'])]),
118118
[null],
119119
];
120120

121121
yield 'parameter found and string variadic with regexp filter that matches' => [
122122
Request::create('/', 'GET', ['firstName' => ['John', 'John']]),
123-
new ArgumentMetadata('firstName', 'string', true, false, false, attributes: [new MapQueryParameter(filter: \FILTER_VALIDATE_REGEXP, flags: \FILTER_NULL_ON_FAILURE, options: ['regexp' => '/John/'])]),
123+
new ArgumentMetadata('firstName', 'string', true, false, false, attributes: [new MapQueryParameter(options: ['regexp' => '/John/'])]),
124124
['John', 'John'],
125125
];
126126

127127
yield 'parameter found and string variadic with regexp filter that falls back to null on failure' => [
128128
Request::create('/', 'GET', ['firstName' => ['John', 'Fabien']]),
129-
new ArgumentMetadata('firstName', 'string', true, false, false, attributes: [new MapQueryParameter(filter: \FILTER_VALIDATE_REGEXP, flags: \FILTER_NULL_ON_FAILURE, options: ['regexp' => '/John/'])]),
129+
new ArgumentMetadata('firstName', 'string', true, false, false, attributes: [new MapQueryParameter(flags: \FILTER_NULL_ON_FAILURE, options: ['regexp' => '/John/'])]),
130130
['John'],
131131
];
132132

133133
yield 'parameter found and integer' => [
134-
Request::create('/', 'GET', ['age' => 123]),
134+
Request::create('/', 'GET', ['age' => '123']),
135135
new ArgumentMetadata('age', 'int', false, false, false, attributes: [new MapQueryParameter()]),
136136
[123],
137137
];
138138

139139
yield 'parameter found and integer variadic' => [
140-
Request::create('/', 'GET', ['age' => [123, 222]]),
140+
Request::create('/', 'GET', ['age' => ['123', '222']]),
141141
new ArgumentMetadata('age', 'int', true, false, false, attributes: [new MapQueryParameter()]),
142142
[123, 222],
143143
];
144144

145145
yield 'parameter found and float' => [
146-
Request::create('/', 'GET', ['price' => 10.99]),
146+
Request::create('/', 'GET', ['price' => '10.99']),
147147
new ArgumentMetadata('price', 'float', false, false, false, attributes: [new MapQueryParameter()]),
148148
[10.99],
149149
];
150150

151151
yield 'parameter found and float variadic' => [
152-
Request::create('/', 'GET', ['price' => [10.99, 5.99]]),
152+
Request::create('/', 'GET', ['price' => ['10.99e2', '5.99']]),
153153
new ArgumentMetadata('price', 'float', true, false, false, attributes: [new MapQueryParameter()]),
154-
[10.99, 5.99],
154+
[1099.0, 5.99],
155155
];
156156

157157
yield 'parameter found and boolean yes' => [
@@ -209,7 +209,7 @@ public static function validDataProvider(): iterable
209209
];
210210

211211
yield 'parameter found and backing type variadic and at least one backing value not int nor string that fallbacks to null on failure' => [
212-
Request::create('/', 'GET', ['suits' => [1, 'D']]),
212+
Request::create('/', 'GET', ['suits' => ['1', 'D']]),
213213
new ArgumentMetadata('suits', Suit::class, false, false, false, attributes: [new MapQueryParameter(flags: \FILTER_NULL_ON_FAILURE)]),
214214
[null],
215215
];
@@ -265,7 +265,7 @@ public static function invalidArgumentTypeProvider(): iterable
265265
public static function invalidOrMissingArgumentProvider(): iterable
266266
{
267267
yield 'parameter found and array variadic with parameter not array failure' => [
268-
Request::create('/', 'GET', ['ids' => [['1', '2'], 1]]),
268+
Request::create('/', 'GET', ['ids' => [['1', '2'], '1']]),
269269
new ArgumentMetadata('ids', 'array', true, false, false, attributes: [new MapQueryParameter()]),
270270
new NotFoundHttpException('Invalid query parameter "ids".'),
271271
];

0 commit comments

Comments
 (0)