8000 [ExpressionLanguage] Add ``min`` and ``max`` php functions by maxbeckers · Pull Request #53728 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[ExpressionLanguage] Add min and max php functions #53728

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

Merged
merged 1 commit into from
Feb 3, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
=========

7.1
---

* Add support for php `min` and `max` functions
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* Add support for php `min` and `max` functions
* Add support for PHP `min` and `max` functions


7.0
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,10 @@ public function registerProvider(ExpressionFunctionProviderInterface $provider):
*/
protected function registerFunctions()
{
$this->addFunction(ExpressionFunction::fromPhp('constant'));
$basicPhpFunctions = ['constant', 'min', 'max'];
foreach ($basicPhpFunctions as $function) {
$this->addFunction(ExpressionFunction::fromPhp($function));
}

$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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,23 @@ public function testCachedParse()
$this->assertSame($savedParsedExpression, $parsedExpression);
}

public function testConstantFunction()
/**
* @dataProvider basicPhpFunctionProvider
*/
public function testBasicPhpFunction($expression, $expected, $compiled)
{
$expressionLanguage = new ExpressionLanguage();
$this->assertEquals(\PHP_VERSION, $expressionLanguage->evaluate('constant("PHP_VERSION")'));
$this->assertEquals($expected, $expressionLanguage->evaluate($expression));
$this->assertEquals($compiled, $expressionLanguage->compile($expression));
}

$expressionLanguage = new ExpressionLanguage();
$this->assertEquals('\constant("PHP_VERSION")', $expressionLanguage->compile('constant("PHP_VERSION")'));
public static function basicPhpFunctionProvider()
{
return [
['constant("PHP_VERSION")', \PHP_VERSION, '\constant("PHP_VERSION")'],
['min(1,2,3)', 1, '\min(1, 2, 3)'],
['max(1,2,3)', 3, '\max(1, 2, 3)'],
];
}

public function testEnumFunctionWithConstantThrows()
Expand Down
0