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

[HttpKernel] Add a controller argument resolver for backed enums#44831

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
fabpot merged 1 commit intosymfony:6.1fromogizanagi:backed-enum-resolver
Jan 1, 2022

Conversation

@ogizanagi
Copy link
Contributor

@ogizanagiogizanagi commentedDec 28, 2021
edited
Loading

QA
Branch?6.1
Bug fix?no
New feature?yes
Deprecations?no
TicketsN/A
LicenseMIT
Doc PRTodo

Given:

namespaceApp\Model;enum Suit:string {case Hearts ='H';case Diamonds ='D';case Clubs ='C';case Spades ='S';}

and the controller:

class CardController{    #[Route('/cards/{suit}')]publicfunctionlist(Suit$suit):Response    {// [...]    }}

A request to/cards/H would inject theSuit::Hearts enum case into the controller$suit argument.

This core resolver aims to resolve backed enum from route path parameters, so we assume a 404 Not Found should be thrown on an invalid backing value provided.

jvasseur reacted with thumbs up emojifancyweb, hbgamra, chalasr, zmitic, and Chris53897 reacted with heart emojiderrabus, W0rma, chalasr, nikolaynizruhin, and yceruto reacted with rocket emoji
nicolas-grekas added a commit that referenced this pull requestDec 29, 2021
…8.1 classes (ogizanagi)This PR was merged into the 4.4 branch.Discussion----------Suppress psalm error for UndefinedDocblockClass for PHP 8.1 classes| Q             | A| ------------- | ---| Branch?       | 4.4 <!-- see below -->| Bug fix?      | no| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->| Tickets       |#44831 (comment) <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead -->| License       | MIT| Doc PR        | N/ASame as#43050 for inlined docblock comments (mainly `class-string<FQCN>`)Commits-------dbc3eea Suppress psalm error for UndefinedDocblockClass for PHP 8.1 classes
@fabpot
Copy link
Member

Thank you@ogizanagi.

@fabpotfabpot merged commita0eecdf intosymfony:6.1Jan 1, 2022
@ogizanagiogizanagi deleted the backed-enum-resolver branchJanuary 1, 2022 17:08
fabpot added a commit that referenced this pull requestApr 12, 2022
…equirements from a \BackedEnum (fancyweb)This PR was merged into the 6.1 branch.Discussion----------[Routing] Add EnumRequirement to help generate route requirements from a \BackedEnum| Q             | A| ------------- | ---| Branch?       | 6.1| Bug fix?      | no| New feature?  | yes| Deprecations? | no| Tickets       | -| License       | MIT| Doc PR        | -Ref#44831I'd like to limit a route parameter allowed values to the backed values of an enum to use it in conjunction with the new `\BackedEnum` argument resolver (ie fail from the start).Also, sometimes, I'd like to limit it only to a subset of the backed values.I couldn't find a way to do that because enums can't implement `__toString()` and accessing `->value` is not considered a constant operation.We can leverage the fact that route requirements can be a `\Stringable`.Before (no enum):```php#[Route(path: '/foo/{bar}', requirements: ['bar' => FooEnum::AAA.'|'.FooEnum::BBB])]```Allow all enum cases:```php#[Route(path: '/foo/{bar}', requirements: ['bar' => new EnumRequirement(Foo::class)])]```Allow a subset:```php#[Route(path: '/foo/{bar}', requirements: ['bar' => new EnumRequirement(Foo::class, Foo::Aaa, Foo::Bbb)])]```Probably not the best solution but I hope we can find something for that use case for 6.1 😄cc@ogizanagiCommits-------ce87606 [Routing] Add EnumRequirement to help generate route requirements from a \BackedEnum
symfony-splitter pushed a commit to symfony/routing that referenced this pull requestApr 12, 2022
…equirements from a \BackedEnum (fancyweb)This PR was merged into the 6.1 branch.Discussion----------[Routing] Add EnumRequirement to help generate route requirements from a \BackedEnum| Q             | A| ------------- | ---| Branch?       | 6.1| Bug fix?      | no| New feature?  | yes| Deprecations? | no| Tickets       | -| License       | MIT| Doc PR        | -Refsymfony/symfony#44831I'd like to limit a route parameter allowed values to the backed values of an enum to use it in conjunction with the new `\BackedEnum` argument resolver (ie fail from the start).Also, sometimes, I'd like to limit it only to a subset of the backed values.I couldn't find a way to do that because enums can't implement `__toString()` and accessing `->value` is not considered a constant operation.We can leverage the fact that route requirements can be a `\Stringable`.Before (no enum):```php#[Route(path: '/foo/{bar}', requirements: ['bar' => FooEnum::AAA.'|'.FooEnum::BBB])]```Allow all enum cases:```php#[Route(path: '/foo/{bar}', requirements: ['bar' => new EnumRequirement(Foo::class)])]```Allow a subset:```php#[Route(path: '/foo/{bar}', requirements: ['bar' => new EnumRequirement(Foo::class, Foo::Aaa, Foo::Bbb)])]```Probably not the best solution but I hope we can find something for that use case for 6.1 😄cc@ogizanagiCommits-------ce876065ff [Routing] Add EnumRequirement to help generate route requirements from a \BackedEnum
@fabpotfabpot mentioned this pull requestApr 15, 2022
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@nicolas-grekasnicolas-grekasnicolas-grekas left review comments

@derrabusderrabusderrabus left review comments

@fabpotfabpotfabpot approved these changes

@chalasrchalasrchalasr approved these changes

Assignees

No one assigned

Projects

None yet

Milestone

6.1

Development

Successfully merging this pull request may close these issues.

6 participants

@ogizanagi@fabpot@nicolas-grekas@derrabus@chalasr@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp