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

feat(expressions): improve Var and Field Expressions #583

This commit is contained in:
Awilum
2022-09-03 10:47:22 +03:00
parent 56e68bfc78
commit 9060fa30a4
3 changed files with 85 additions and 5 deletions

View File

@@ -20,6 +20,7 @@ use function Flextype\emitter;
use function Flextype\entries;
use function Flextype\expression;
use function Flextype\registry;
use function Flextype\collection;
// Directive: [[ ]]
emitter()->addListener('onEntriesFetchSingleField', static function (): void {
@@ -39,7 +40,13 @@ emitter()->addListener('onEntriesFetchSingleField', static function (): void {
if (is_string($field['value'])) {
$field['value'] = preg_replace_callback('/' . $selfQuote($openingTag) . ' (.*?) ' . $selfQuote($closingTag) . '/s', function ($matches) {
return expression()->evaluate($matches[1]);
// Prepare vars
foreach (json_decode(json_encode((object) entries()->registry()->get('methods.fetch.result')), false) as $key => $value) {
$vars[$key] = $value;
}
return expression()->evaluate($matches[1], $vars);
}, $field['value']);
}

View File

@@ -20,11 +20,65 @@ use Symfony\Component\ExpressionLanguage\ExpressionFunction;
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
use function Flextype\entries;
use function Glowy\Strings\strings;
class VarExpression implements ExpressionFunctionProviderInterface
{
public function getFunctions()
{
return [new ExpressionFunction('var', static fn (string $var) => "\Flextype\entries()->registry()->get('methods.fetch.result.vars.' . $var . ')'", static fn ($arguments, string $var) => entries()->registry()->get('methods.fetch.result.vars.' . $var))];
return [
new ExpressionFunction('var',
static function (string $var) {
$var = strings($var)->stripQuotes()->toString();
$code = "\Flextype\\entries()->registry()->get('methods.fetch.result.$var');";
return $code;
},
static function ($arguments, string $var) {
return entries()->registry()->get('methods.fetch.result.vars.' . $var);
}
),
new ExpressionFunction('set',
static function (string $var, $value) {
$var = strings($var)->stripQuotes()->toString();
$value = strings($value)->stripQuotes()->toString();
$code = "\Flextype\\entries()->registry()->set('methods.fetch.result.$var', '$value');";
return $code;
},
static function ($arguments, string $var, $value) {
entries()->registry()->set('methods.fetch.result.' . $var, $value);
}
),
new ExpressionFunction('get',
static function (string $var, $default = '') {
$var = strings($var)->stripQuotes()->toString();
$default = strings($default)->stripQuotes()->toString();
$code = "\Flextype\\entries()->registry()->get('methods.fetch.result.$var'" . ($default ? ", '$default'" : '') . ");";
return $code;
},
static function ($arguments, string $var, $default = '') {
return entries()->registry()->get('methods.fetch.result.' . $var, $default);
}
),
new ExpressionFunction('delete',
static function (string $var) {
$var = strings($var)->stripQuotes()->toString();
$code = "\Flextype\\entries()->registry()->delete('methods.fetch.result.$var');";
return $code;
},
static function ($arguments, string $var) {
entries()->registry()->delete('methods.fetch.result.' . $var);
}
),
new ExpressionFunction('unset',
static function (string $var) {
$var = strings($var)->stripQuotes()->toString();
$code = "\Flextype\\entries()->registry()->set('methods.fetch.result.$var', null);";
return $code;
},
static function ($arguments, string $var) {
entries()->registry()->set('methods.fetch.result.' . $var, null);
}
),
];
}
}

View File

@@ -12,7 +12,26 @@ afterEach(function (): void {
filesystem()->directory(FLEXTYPE_PATH_PROJECT . '/entries')->delete();
});
test('var expression', function () {
entries()->create('var', ['vars' => ['foo' => 'Foo'], 'test' => '[[ var("foo") ]]']);
expect(entries()->fetch('var')['test'])->toBe('Foo');
test('var + field expression', function () {
entries()->create('var', [
'title' => 'Foo',
'vars' => [
'foo' => 'Foo'
],
'test1' => '[[ var("foo") ]]',
'test2' => "[[ get('vars.foo', 'Foo') ]]",
'test3' => '[[ vars.foo ]]',
'test4' => '[[ set("bar", "Bar") ]][[ bar ]]',
'test5' => '[[ unset("bar") ]]',
'test6' => '[[ delete("bar") ]]',
'test7' => '[[ title ]] [[ get("title") ]]',
]);
expect(entries()->fetch('var')['test1'])->toBe('Foo');
expect(entries()->fetch('var')['test2'])->toBe('Foo');
expect(entries()->fetch('var')['test3'])->toBe('Foo');
expect(entries()->fetch('var')['test4'])->toBe('Bar');
expect(entries()->fetch('var')['test5'])->toBe('');
expect(entries()->fetch('var')['test6'])->toBe('');
expect(entries()->fetch('var')['test7'])->toBe('Foo Foo');
});