Skip to content

Commit c4ee8fd

Browse files
committed
feature symfony#58717 [HttpKernel] Support Uid in #[MapQueryParameter] (seb-jean)
This PR was squashed before being merged into the 7.3 branch. Discussion ---------- [HttpKernel] Support `Uid` in `#[MapQueryParameter]` | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | Fix symfony#51618 | License | MIT I added support `Uid` in `#[MapQueryParameter]`. To use it, you must do this: ```php #[Route(path: '/', name: 'user_show')] public function indexAction( #[MapQueryParameter] ?Ulid $groupId = null, ) ``` Commits ------- 9fd614a [HttpKernel] Support `Uid` in `#[MapQueryParameter]`
2 parents cb0a724 + 9fd614a commit c4ee8fd

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
use Symfony\Component\HttpKernel\Controller\ValueResolverInterface;
1717
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
1818
use Symfony\Component\HttpKernel\Exception\HttpException;
19+
use Symfony\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 => throw new \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 => throw new \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
use Symfony\Component\HttpKernel\Exception\HttpException;
2323
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2424
use Symfony\Component\HttpKernel\Tests\Fixtures\Suit;
25+
use Symfony\Component\Uid\Ulid;
2526

2627
class QueryParameterValueResolverTest extends TestCase
2728
{
@@ -44,7 +45,7 @@ public function testSkipWhenNoAttribute()
4445
*/
4546
public function testResolvingSuccessfully(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
new ArgumentMetadata('firstName', 'string', false, true, false, attributes: [new MapQueryParameter()]),
232233
[],
233234
];
235+
236+
yield 'parameter found and ULID' => [
237+
Request::create('/', 'GET', ['groupId' => '01E439TP9XJZ9RPFH3T1PYBCR8']),
238+
new ArgumentMetadata('groupId', Ulid::class, false, true, false, attributes: [new MapQueryParameter()]),
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
new ArgumentMetadata('standardClass', \stdClass::class, false, false, false, attributes: [new MapQueryParameter()]),
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
new ArgumentMetadata('standardClass', \stdClass::class, true, false, false, attributes: [new MapQueryParameter()]),
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)