Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9fd614a

Browse files
seb-jeanfabpot
authored andcommitted
[HttpKernel] SupportUid in#[MapQueryParameter]
1 parentcf015b8 commit9fd614a

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

‎src/Symfony/Component/HttpKernel/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Add`$key` argument to`#[MapQueryString]` that allows using a specific key for argument resolving
8+
* Support`Uid` in`#[MapQueryParameter]`
89

910
7.2
1011
---

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
useSymfony\Component\HttpKernel\Controller\ValueResolverInterface;
1717
useSymfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
1818
useSymfony\Component\HttpKernel\Exception\HttpException;
19+
useSymfony\Component\Uid\AbstractUid;
1920

2021
/**
2122
* Resolve arguments of type: array, string, int, float, bool, \BackedEnum from query parameters.
@@ -73,17 +74,24 @@ public function resolve(Request $request, ArgumentMetadata $argument): array
7374
$options['flags'] |= \FILTER_REQUIRE_SCALAR;
7475
}
7576

77+
$uidType =null;
78+
if (is_subclass_of($type, AbstractUid::class)) {
79+
$uidType =$type;
80+
$type ='uid';
81+
}
82+
7683
$enumType =null;
7784
$filter =match ($type) {
7885
'array' => \FILTER_DEFAULT,
7986
'string' =>isset($attribute->options['regexp']) ? \FILTER_VALIDATE_REGEXP : \FILTER_DEFAULT,
8087
'int' => \FILTER_VALIDATE_INT,
8188
'float' => \FILTER_VALIDATE_FLOAT,
8289
'bool' => \FILTER_VALIDATE_BOOL,
90+
'uid' => \FILTER_DEFAULT,
8391
default =>match ($enumType =is_subclass_of($type, \BackedEnum::class) ? (new \ReflectionEnum($type))->getBackingType()->getName() :null) {
8492
'int' => \FILTER_VALIDATE_INT,
8593
'string' => \FILTER_DEFAULT,
86-
default =>thrownew \LogicException(\sprintf('#[MapQueryParameter] cannot be used on controller argument "%s$%s" of type "%s"; one of array, string, int, float, bool or \BackedEnum should be used.',$argument->isVariadic() ?'...' :'',$argument->getName(),$type ??'mixed')),
94+
default =>thrownew \LogicException(\sprintf('#[MapQueryParameter] cannot be used on controller argument "%s$%s" of type "%s"; one of array, string, int, float, bool, uid or \BackedEnum should be used.',$argument->isVariadic() ?'...' :'',$argument->getName(),$type ??'mixed')),
8795
},
8896
};
8997

@@ -105,6 +113,10 @@ public function resolve(Request $request, ArgumentMetadata $argument): array
105113
$value =\is_array($value) ?array_map($enumFrom,$value) :$enumFrom($value);
106114
}
107115

116+
if (null !==$uidType) {
117+
$value =\is_array($value) ?array_map([$uidType,'fromString'],$value) :$uidType::fromString($value);
118+
}
119+
108120
if (null ===$value && !($attribute->flags & \FILTER_NULL_ON_FAILURE)) {
109121
throw HttpException::fromStatusCode($validationFailedCode,\sprintf('Invalid query parameter "%s".',$name));
110122
}

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
useSymfony\Component\HttpKernel\Exception\HttpException;
2323
useSymfony\Component\HttpKernel\Exception\NotFoundHttpException;
2424
useSymfony\Component\HttpKernel\Tests\Fixtures\Suit;
25+
useSymfony\Component\Uid\Ulid;
2526

2627
class QueryParameterValueResolverTestextends TestCase
2728
{
@@ -44,7 +45,7 @@ public function testSkipWhenNoAttribute()
4445
*/
4546
publicfunctiontestResolvingSuccessfully(Request$request,ArgumentMetadata$metadata,array$expected)
4647
{
47-
$this->assertSame($expected,$this->resolver->resolve($request,$metadata));
48+
$this->assertEquals($expected,$this->resolver->resolve($request,$metadata));
4849
}
4950

5051
/**
@@ -231,6 +232,12 @@ public static function validDataProvider(): iterable
231232
newArgumentMetadata('firstName','string',false,true,false, attributes: [newMapQueryParameter()]),
232233
[],
233234
];
235+
236+
yield'parameter found and ULID' => [
237+
Request::create('/','GET', ['groupId' =>'01E439TP9XJZ9RPFH3T1PYBCR8']),
238+
newArgumentMetadata('groupId', Ulid::class,false,true,false, attributes: [newMapQueryParameter()]),
239+
[Ulid::fromString('01E439TP9XJZ9RPFH3T1PYBCR8')],
240+
];
234241
}
235242

236243
/**
@@ -245,13 +252,13 @@ public static function invalidArgumentTypeProvider(): iterable
245252
yield'unsupported type' => [
246253
Request::create('/','GET', ['standardClass' =>'test']),
247254
newArgumentMetadata('standardClass', \stdClass::class,false,false,false, attributes: [newMapQueryParameter()]),
248-
'#[MapQueryParameter] cannot be used on controller argument "$standardClass" of type "stdClass"; one of array, string, int, float, bool or \BackedEnum should be used.',
255+
'#[MapQueryParameter] cannot be used on controller argument "$standardClass" of type "stdClass"; one of array, string, int, float, bool, uid or \BackedEnum should be used.',
249256
];
250257

251258
yield'unsupported type variadic' => [
252259
Request::create('/','GET', ['standardClass' =>'test']),
253260
newArgumentMetadata('standardClass', \stdClass::class,true,false,false, attributes: [newMapQueryParameter()]),
254-
'#[MapQueryParameter] cannot be used on controller argument "...$standardClass" of type "stdClass"; one of array, string, int, float, bool or \BackedEnum should be used.',
261+
'#[MapQueryParameter] cannot be used on controller argument "...$standardClass" of type "stdClass"; one of array, string, int, float, bool, uid or \BackedEnum should be used.',
255262
];
256263
}
257264

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp