Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ExpressionLanguage] Add enum expression function #48669

Open
wants to merge 1 commit into
base: 6.3
Choose a base branch
from

Conversation

alexandre-daubois
Copy link
Contributor

@alexandre-daubois alexandre-daubois commented Dec 15, 2022

Q A
Branch? 6.3
Bug fix? no
New feature? yes
Deprecations? no
Tickets Fix #48593
License MIT
Doc PR symfony/symfony-docs#17632

@carsonbot carsonbot added this to the 6.3 milestone Dec 15, 2022
@alexandre-daubois alexandre-daubois force-pushed the feat/enum-expression-language branch 5 times, most recently from 63e5a9a to 3437438 Compare Dec 15, 2022
@carsonbot
Copy link

carsonbot commented Dec 16, 2022

Hey!

I think @mytuny has recently worked with this code. Maybe they can help review this?

Cheers!

Carsonbot

@alexandre-daubois alexandre-daubois force-pushed the feat/enum-expression-language branch from 3437438 to db92423 Compare Dec 19, 2022
@@ -138,6 +138,19 @@ public function registerProvider(ExpressionFunctionProviderInterface $provider)
protected function registerFunctions()
{
$this->addFunction(ExpressionFunction::fromPhp('constant'));

$this->addFunction(new ExpressionFunction('enum',
static fn ($str): string => sprintf("(\constant(\$v = (%1\$s))) instanceof \UnitEnum ? \constant(\$v) : throw new \TypeError(\sprintf('The string \"%%s\" is not the name of a valid enum case.', \$v))", $str),
Copy link
Member

@nicolas-grekas nicolas-grekas Dec 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be just %s instead of %1$s

Copy link
Contributor Author

@alexandre-daubois alexandre-daubois Dec 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! 🙂

@alexandre-daubois alexandre-daubois force-pushed the feat/enum-expression-language branch from 0223cea to 53bfee4 Compare Dec 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Using Enum objects in expression language
5 participants