From 9060fa30a4a395f99136f620923cb49c1d0df103 Mon Sep 17 00:00:00 2001 From: Awilum Date: Sat, 3 Sep 2022 10:47:22 +0300 Subject: [PATCH] feat(expressions): improve Var and Field Expressions #583 --- .../Directives/ExpressionsDirective.php | 9 ++- .../Entries/Expressions/VarExpression.php | 56 ++++++++++++++++++- .../Entries/Expressions/VarExpressionTest.php | 25 ++++++++- 3 files changed, 85 insertions(+), 5 deletions(-) diff --git a/src/flextype/core/Entries/Directives/ExpressionsDirective.php b/src/flextype/core/Entries/Directives/ExpressionsDirective.php index 26ce6c5b..140c712d 100644 --- a/src/flextype/core/Entries/Directives/ExpressionsDirective.php +++ b/src/flextype/core/Entries/Directives/ExpressionsDirective.php @@ -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']); } diff --git a/src/flextype/core/Entries/Expressions/VarExpression.php b/src/flextype/core/Entries/Expressions/VarExpression.php index 565fd0e6..26ec08ac 100644 --- a/src/flextype/core/Entries/Expressions/VarExpression.php +++ b/src/flextype/core/Entries/Expressions/VarExpression.php @@ -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); + } + ), + ]; } } diff --git a/tests/src/flextype/core/Entries/Expressions/VarExpressionTest.php b/tests/src/flextype/core/Entries/Expressions/VarExpressionTest.php index 9d0d834f..209c6adc 100644 --- a/tests/src/flextype/core/Entries/Expressions/VarExpressionTest.php +++ b/tests/src/flextype/core/Entries/Expressions/VarExpressionTest.php @@ -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'); }); \ No newline at end of file