8000 feature #53728 [ExpressionLanguage] Add ``min`` and ``max`` php funct… · symfony/symfony@06c7b81 · GitHub
[go: up one dir, main page]

Skip to content

Commit 06c7b81

Browse files
committed
feature #53728 [ExpressionLanguage] Add min and max php functions (maxbeckers)
This PR was merged into the 7.1 branch. Discussion ---------- [ExpressionLanguage] Add ``min`` and ``max`` php functions | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? |no | New feature? | yes | Deprecations? | no | Issues | Fix #53627 | License | MIT Add support for php `min` and `max` functions in the ExpressionLanguage. I'll create also a pr for the docs when it's merged. Commits ------- f28f610 [ExpressionLanguage] Add ``min`` and ``max`` php functions
2 parents 272f021 + f28f610 commit 06c7b81

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

src/Symfony/Component/ExpressionLanguage/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.1
5+
---
6+
7+
* Add support for php `min` and `max` functions
8000
8+
49
7.0
510
---
611

src/Symfony/Component/ExpressionLanguage/ExpressionLanguage.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,10 @@ public function registerProvider(ExpressionFunctionProviderInterface $provider):
140140
*/
141141
protected function registerFunctions()
142142
{
143-
$this->addFunction(ExpressionFunction::fromPhp('constant'));
143+
$basicPhpFunctions = ['constant', 'min', 'max'];
144+
foreach ($basicPhpFunctions as $function) {
145+
$this->addFunction(ExpressionFunction::fromPhp($function));
146+
}
144147

145148
$this->addFunction(new ExpressionFunction('enum',
146149
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),

src/Symfony/Component/ExpressionLanguage/Tests/ExpressionLanguageTest.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,23 @@ public function testCachedParse()
7171
$this->assertSame($savedParsedExpression, $parsedExpression);
7272
}
7373

74-
public function testConstantFunction()
74+
/**
75+
* @dataProvider basicPhpFunctionProvider
76+
*/
77+
public function testBasicPhpFunction($expression, $expected, $compiled)
7578
{
7679
$expressionLanguage = new ExpressionLanguage();
77-
$this->assertEquals(\PHP_VERSION, $expressionLanguage->evaluate('constant("PHP_VERSION")'));
80+
$this->assertEquals($expected, $expressionLanguage->evaluate($expression));
81+
$this->assertEquals($compiled, $expressionLanguage->compile($expression));
82+
}
7883

79-
$expressionLanguage = new ExpressionLanguage();
80-
$this->assertEquals('\constant("PHP_VERSION")', $expressionLanguage->compile('constant("PHP_VERSION")'));
84+
public static function basicPhpFunctionProvider()
85+
{
86+
return [
87+
['constant("PHP_VERSION")', \PHP_VERSION, '\constant("PHP_VERSION")'],
88+
['min(1,2,3)', 1, '\min(1, 2, 3)'],
89+
['max(1,2,3)', 3, '\max(1, 2, 3)'],
90+
];
8191
}
8292

8393
public function testEnumFunctionWithConstantThrows()

0 commit comments

Comments
 (0)
0