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

feat(expressions): add new when and unless expressions

This commit is contained in:
Awilum
2022-09-13 11:59:02 +03:00
parent 289c25ca8d
commit f28f0e5a8a
5 changed files with 129 additions and 1 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\Parsers\Expressions;
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
use Glowy\Macroable\Macroable;
class WhenExpression implements ExpressionFunctionProviderInterface
{
public function getFunctions()
{
return [
new ExpressionFunction('unless',
static fn (bool $value, $result) => ($value === false) ? $result : '',
static fn ($arguments, bool $value, $result) => ($value === false) ? $result : ''
)
];
}
}

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\Parsers\Expressions;
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
use Glowy\Macroable\Macroable;
class WhenExpression implements ExpressionFunctionProviderInterface
{
public function getFunctions()
{
return [
new ExpressionFunction('when',
static fn (bool $value, $result) => ($value === true) ? $result : '',
static fn ($arguments, bool $value, $result) => ($value === true) ? $result : ''
)
];
}
}

View File

@@ -725,7 +725,13 @@ parsers:
url:
enabled: true
class: "Flextype\\Parsers\\Expressions\\UrlExpression"
when:
enabled: true
class: "Flextype\\Parsers\\Expressions\\WhenExpression"
unless:
enabled: true
class: "Flextype\\Parsers\\Expressions\\UnlessnExpression"
# CORS
#
# CORS (Cross-origin resource sharing) allows JavaScript web apps to make HTTP requests to other domains.

View File

@@ -0,0 +1,26 @@
<?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('unless expression', function () {
entries()->create('unless', [
'title' => 'Title',
'test-unless-positive' => '[[ unless(title == "Foo", "Yes!") ]]',
'test-unless-negative' => '[[ unless(title == "Title", "No!") ]]'
]);
expect(entries()->fetch('unless')['test-unless-positive'])->toBe('Yes!');
expect(entries()->fetch('unless')['test-unless-negative'])->toBe('No!');
});

View File

@@ -0,0 +1,26 @@
<?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('when expression', function () {
entries()->create('when', [
'title' => 'Title',
'test-when-positive' => '[[ when(title == "Title", "Yes!") ]]',
'test-when-negative' => '[[ when(title == "Foo", "No!") ]]'
]);
expect(entries()->fetch('when')['test-when-positive'])->toBe('Yes!');
expect(entries()->fetch('when')['test-when-negative'])->toBe('No!');
});