From 4771031ce1524881fed2bda44fd9a2df93f5aa2a Mon Sep 17 00:00:00 2001 From: Awilum Date: Thu, 26 May 2022 08:40:58 +0300 Subject: [PATCH] feat(shortcodes): improve syntax for `var` shortcode --- .../core/Parsers/Shortcodes/VarShortcode.php | 29 +++++++++++++++++-- .../Parsers/Shortcodes/VarShortcodeTest.php | 10 +++++-- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/flextype/core/Parsers/Shortcodes/VarShortcode.php b/src/flextype/core/Parsers/Shortcodes/VarShortcode.php index 0dce4170..975ea499 100644 --- a/src/flextype/core/Parsers/Shortcodes/VarShortcode.php +++ b/src/flextype/core/Parsers/Shortcodes/VarShortcode.php @@ -20,11 +20,34 @@ use Thunder\Shortcode\Shortcode\ShortcodeInterface; use function registry; // Shortcode: var -// Usage: (var:foo) +// Usage: (var:foo) +// (var get:foo) +// (var set:foo value:Foo) +// (var set:foo) Foo (/var) parsers()->shortcodes()->addHandler('var', static function (ShortcodeInterface $s) { if (! registry()->get('flextype.settings.parsers.shortcodes.shortcodes.var.enabled')) { return ''; } + + $params = ($s->getParameters() != null) ? $s->getParameters() : ''; - return entries()->registry()->get('methods.fetch.result.vars.' . parsers()->shortcodes()->parse($s->getBBCode())); -}); + if (isset($params['set'])) { + if (isset($params['value'])) { + $value = $params['value']; + } else { + $value = ($s->getContent() != null) ? $s->getContent() : ''; + } + entries()->registry()->set('methods.fetch.result.vars.' . parsers()->shortcodes()->parse($params['set']), parsers()->shortcodes()->parse($value)); + return ''; + } + + if (isset($params['get'])) { + return entries()->registry()->get('methods.fetch.result.vars.' . parsers()->shortcodes()->parse($params['get'])); + } + + if ($s->getBBCode() !== null) { + return entries()->registry()->get('methods.fetch.result.vars.' . parsers()->shortcodes()->parse($s->getBBCode())); + } + + return ''; +}); \ No newline at end of file diff --git a/tests/src/flextype/core/Parsers/Shortcodes/VarShortcodeTest.php b/tests/src/flextype/core/Parsers/Shortcodes/VarShortcodeTest.php index 46dd9707..ee13f79c 100644 --- a/tests/src/flextype/core/Parsers/Shortcodes/VarShortcodeTest.php +++ b/tests/src/flextype/core/Parsers/Shortcodes/VarShortcodeTest.php @@ -11,6 +11,12 @@ afterEach(function () { }); test('var shortcode', function () { - expect(entries()->create('foo', ['vars' => ['foo' => 'Foo'], 'title' => '(var:foo)']))->toBeTrue(); - expect(entries()->fetch('foo')['title'])->toBe('Foo'); + expect(entries()->create('foo', ['vars' => ['foo' => 'Foo'], 'title' => '(var:foo) (var get:foo)']))->toBeTrue(); + expect(entries()->fetch('foo')['title'])->toBe('Foo Foo'); + + expect(entries()->create('bar', ['title' => '(var set:bar value:Bar)(var:bar)']))->toBeTrue(); + expect(entries()->fetch('bar')['title'])->toBe('Bar'); + + expect(entries()->create('zed', ['title' => '(var set:zed)Zed(/var)(var:zed)']))->toBeTrue(); + expect(entries()->fetch('zed')['title'])->toBe('Zed'); }); \ No newline at end of file