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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/ExpressionLanguage/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.3
---

* Add `enum` expression function

6.2
---

Expand Down
13 changes: 13 additions & 0 deletions src/Symfony/Component/ExpressionLanguage/ExpressionLanguage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (%s))) instanceof \UnitEnum ? \constant(\$v) : throw new \TypeError(\sprintf('The string \"%%s\" is not the name of a valid enum case.', \$v))", $str),
static function ($arguments, $str): \UnitEnum {
$value = \constant($str);

if (!$value instanceof \UnitEnum) {
throw new \TypeError(sprintf('The string "%s" is not the name of a valid enum case.', $str));
}

return $value;
}
));
}

private function getLexer(): Lexer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\ExpressionLanguage\ParsedExpression;
use Symfony\Component\ExpressionLanguage\SyntaxError;
use Symfony\Component\ExpressionLanguage\Tests\Fixtures\FooBackedEnum;
use Symfony\Component\ExpressionLanguage\Tests\Fixtures\FooEnum;
use Symfony\Component\ExpressionLanguage\Tests\Fixtures\TestProvider;

class ExpressionLanguageTest extends TestCase
Expand Down Expand Up @@ -78,6 +80,53 @@ public function testConstantFunction()
$this->assertEquals('\constant("PHP_VERSION")', $expressionLanguage->compile('constant("PHP_VERSION")'));
}

public function testEnumFunctionWithConstantThrows()
{
$this->expectException(\TypeError::class);
$this->expectExceptionMessage('The string "PHP_VERSION" is not the name of a valid enum case.');
$expressionLanguage = new ExpressionLanguage();
$expressionLanguage->evaluate('enum("PHP_VERSION")');
}

public function testCompiledEnumFunctionWithConstantThrows()
{
$this->expectException(\TypeError::class);
$this->expectExceptionMessage('The string "PHP_VERSION" is not the name of a valid enum case.');
$expressionLanguage = new ExpressionLanguage();
eval($expressionLanguage->compile('enum("PHP_VERSION")').';');
}

public function testEnumFunction()
{
$expressionLanguage = new ExpressionLanguage();
$this->assertSame(FooEnum::Foo, $expressionLanguage->evaluate('enum("Symfony\\\\Component\\\\ExpressionLanguage\\\\Tests\\\\Fixtures\\\\FooEnum::Foo")'));
}

public function testCompiledEnumFunction()
{
$result = null;
$expressionLanguage = new ExpressionLanguage();
eval(sprintf('$result = %s;', $expressionLanguage->compile('enum("Symfony\\\\Component\\\\ExpressionLanguage\\\\Tests\\\\Fixtures\\\\FooEnum::Foo")')));

$this->assertSame(FooEnum::Foo, $result);
}

public function testBackedEnumFunction()
{
$expressionLanguage = new ExpressionLanguage();
$this->assertSame(FooBackedEnum::Bar, $expressionLanguage->evaluate('enum("Symfony\\\\Component\\\\ExpressionLanguage\\\\Tests\\\\Fixtures\\\\FooBackedEnum::Bar")'));
$this->assertSame('Foo', $expressionLanguage->evaluate('enum("Symfony\\\\Component\\\\ExpressionLanguage\\\\Tests\\\\Fixtures\\\\FooBackedEnum::Bar").value'));
nicolas-grekas marked this conversation as resolved.
Show resolved Hide resolved
}

public function testCompiledEnumFunctionWithBackedEnum()
{
$result = null;
$expressionLanguage = new ExpressionLanguage();
eval(sprintf('$result = %s;', $expressionLanguage->compile('enum("Symfony\\\\Component\\\\ExpressionLanguage\\\\Tests\\\\Fixtures\\\\FooBackedEnum::Bar")')));

$this->assertSame(FooBackedEnum::Bar, $result);
}

public function testProviders()
{
$expressionLanguage = new ExpressionLanguage(null, [new TestProvider()]);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Symfony\Component\ExpressionLanguage\Tests\Fixtures;

enum FooBackedEnum: string
{
case Bar = 'Foo';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Symfony\Component\ExpressionLanguage\Tests\Fixtures;

enum FooEnum
{
case Foo;
}