1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-06 13:16:45 +02:00

feat(expressions): add new round and abs expressions

This commit is contained in:
Awilum
2022-09-13 12:23:23 +03:00
parent f28f0e5a8a
commit e9e6111ccc
2 changed files with 12 additions and 0 deletions

View File

@@ -26,6 +26,8 @@ class MathExpression implements ExpressionFunctionProviderInterface
public function getFunctions()
{
return [
new ExpressionFunction('abs', static fn (int|float $num): mixed => '\abs($num)', static fn (array $arguments, int|float $num): mixed => \abs($num)),
new ExpressionFunction('round', static fn (int|float $num, int $precision = 0, int $mode = 1): mixed => '\round($num, $precision, $mode)', static fn (array $arguments, int|float $num, int $precision = 0, int $mode = 1): mixed => \round($num, $precision, $mode)),
new ExpressionFunction('ceil', static fn (int|float $num): mixed => '\ceil($num)', static fn (array $arguments, int|float $num): mixed => \ceil($num)),
new ExpressionFunction('floor', static fn (int|float $num): mixed => '\floor($num)', static fn (array $arguments, int|float $num): mixed => \floor($num)),
new ExpressionFunction('min', static fn (mixed ...$values): mixed => '\min($values)', static fn (array $arguments, mixed ...$values): mixed => \min($values)),

View File

@@ -30,4 +30,14 @@ test('min expression', function () {
test('max expression', function () {
entries()->create('max', ['test' => '[[ max(2, 3, 1, 6, 7) ]]']);
expect(entries()->fetch('max')['test'])->toBe('7');
});
test('abs expression', function () {
entries()->create('abs', ['test' => '[[ abs(-4.2) ]]']);
expect(entries()->fetch('abs')['test'])->toBe('4.2');
});
test('round expression', function () {
entries()->create('round', ['test' => '[[ round(3.4) ]]']);
expect(entries()->fetch('round')['test'])->toBe('3');
});