1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-06 05:07:41 +02:00

feat(expressions): add date expression function

This commit is contained in:
Awilum
2022-07-11 13:45:00 +03:00
parent 4400657e30
commit fbc61dbbe9
2 changed files with 68 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
/**
* Flextype - Hybrid Content Management System with the freedom of a headless CMS
* and with the full functionality of a traditional CMS!
*
* Copyright (c) Sergey Romanenko (https://awilum.github.io)
*
* Licensed under The MIT License.
*
* For full copyright and license information, please see the LICENSE
* Redistributions of files must retain the above copyright notice.
*/
namespace Flextype\Entries\Expressions;
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
use function Flextype\registry;
class MathExpression implements ExpressionFunctionProviderInterface
{
public function getFunctions()
{
return [
new ExpressionFunction('ceil', static fn (int|float $num): float => '\ceil($num)', static fn (array $arguments, int|float $num): float => \ceil($num)),
new ExpressionFunction('floor', static fn (int|float $num): float => '\floor($num)', static fn (array $arguments, int|float $num): float => \floor($num)),
new ExpressionFunction('min', static fn (mixed ...$values): mixed => '\min($values)', static fn (array $arguments, mixed ...$values): mixed => \min($values)),
new ExpressionFunction('max', static fn (mixed ...$values): mixed => '\max($values)', static fn (array $arguments, mixed ...$values): mixed => \max($values)),
];
}
}

View File

@@ -0,0 +1,33 @@
<?php
use Flextype\Component\Filesystem\Filesystem;
use function Glowy\Filesystem\filesystem;
use function Flextype\entries;
beforeEach(function() {
filesystem()->directory(FLEXTYPE_PATH_PROJECT . '/entries')->create();
});
afterEach(function (): void {
filesystem()->directory(FLEXTYPE_PATH_PROJECT . '/entries')->delete();
});
test('ceil expression', function () {
entries()->create('ceil', ['test' => '[[ ceil(42.1) ]]']);
expect(entries()->fetch('ceil')['test'])->toBe('43');
});
test('floor expression', function () {
entries()->create('floor', ['test' => '[[ floor(4.3) ]]']);
expect(entries()->fetch('floor')['test'])->toBe('4');
});
test('min expression', function () {
entries()->create('min', ['test' => '[[ min(2, 3, 1, 6, 7) ]]']);
expect(entries()->fetch('min')['test'])->toBe('1');
});
test('max expression', function () {
entries()->create('max', ['test' => '[[ max(2, 3, 1, 6, 7) ]]']);
expect(entries()->fetch('max')['test'])->toBe('7');
});