diff --git a/src/Symfony/Component/ExpressionLanguage/CHANGELOG.md b/src/Symfony/Component/ExpressionLanguage/CHANGELOG.md index f54f943ac15de..9d119be057dab 100644 --- a/src/Symfony/Component/ExpressionLanguage/CHANGELOG.md +++ b/src/Symfony/Component/ExpressionLanguage/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +7.1 +--- + + * Add support for php `min` and `max` functions + 7.0 --- diff --git a/src/Symfony/Component/ExpressionLanguage/ExpressionLanguage.php b/src/Symfony/Component/ExpressionLanguage/ExpressionLanguage.php index ac1b9a2cd7372..bd9dbfcc1944c 100644 --- a/src/Symfony/Component/ExpressionLanguage/ExpressionLanguage.php +++ b/src/Symfony/Component/ExpressionLanguage/ExpressionLanguage.php @@ -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), diff --git a/src/Symfony/Component/ExpressionLanguage/Tests/ExpressionLanguageTest.php b/src/Symfony/Component/ExpressionLanguage/Tests/ExpressionLanguageTest.php index 515b18d540f15..f7f712d8c5d46 100644 --- a/src/Symfony/Component/ExpressionLanguage/Tests/ExpressionLanguageTest.php +++ b/src/Symfony/Component/ExpressionLanguage/Tests/ExpressionLanguageTest.php @@ -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()