From 71e53a393b07e9564d1d164cee4f15ac455c1e48 Mon Sep 17 00:00:00 2001 From: Awilum Date: Wed, 4 May 2022 21:50:18 +0300 Subject: [PATCH] feat(shortcodes): `[strings]` shortcode implementation This new shortcode will allow to modify strings for entries fields. --- .../Parsers/Shortcodes/StringsShortcode.php | 86 +++++++++++++++++++ src/flextype/settings.yaml | 4 +- tests/fixtures/settings/settings.yaml | 5 +- .../Shortcodes/StringsShortcodeTest.php | 37 ++++++++ 4 files changed, 129 insertions(+), 3 deletions(-) create mode 100644 src/flextype/core/Parsers/Shortcodes/StringsShortcode.php create mode 100644 tests/src/flextype/core/Parsers/Shortcodes/StringsShortcodeTest.php diff --git a/src/flextype/core/Parsers/Shortcodes/StringsShortcode.php b/src/flextype/core/Parsers/Shortcodes/StringsShortcode.php new file mode 100644 index 00000000..6f05eee7 --- /dev/null +++ b/src/flextype/core/Parsers/Shortcodes/StringsShortcode.php @@ -0,0 +1,86 @@ +shortcodes()->addHandler('strings', static function (ShortcodeInterface $s) { + if (! registry()->get('flextype.settings.parsers.shortcodes.shortcodes.strings.enabled')) { + return ''; + } + + $content = $s->getContent(); + $varsDelimeter = $s->getParameter('delimeter') ?: '|'; + + foreach($s->getParameters() as $key => $value) { + + $values = $value !== null ? strings($value)->contains($varsDelimeter) ? explode($varsDelimeter, $value) : $value : []; + + if ($key == 'append') { + if (is_iterable($values)) { + $content = strings($content)->{'append'}(...$values)->toString(); + } else { + $content = strings($content)->{'append'}($values)->toString(); + } + } + + if ($key == 'prepend') { + if (is_iterable($values)) { + $content = strings($content)->{'prepend'}(...$values)->toString(); + } else { + $content = strings($content)->{'prepend'}($values)->toString(); + } + } + + if ($key == 'after') { + $content = strings($content)->{'after'}($values)->toString(); + } + + if ($key == 'afterLast') { + $content = strings($content)->{'afterLast'}($values)->toString(); + } + + if ($key == 'before') { + $content = strings($content)->{'before'}($values)->toString(); + } + + if ($key == 'beforeLast') { + $content = strings($content)->{'beforeLast'}($values)->toString(); + } + + if ($key == 'lower') { + $content = strings($content)->{'lower'}()->toString(); + } + + if ($key == 'upper') { + $content = strings($content)->{'upper'}()->toString(); + } + + if ($key == 'sort') { + $content = strings($content)->{'wordsSort' . strings($values)->ucfirst()}()->toString(); + } + + if ($key == 'wordsLimit') { + $content = strings($content)->{'wordsLimit'}(isset($values[0]) ? (int) $values[0] : 100, isset($values[1]) ? (string) $values[1] : '...')->toString(); + } + } + + return (string) $content; +}); \ No newline at end of file diff --git a/src/flextype/settings.yaml b/src/flextype/settings.yaml index 360ad7c8..05fa1b14 100644 --- a/src/flextype/settings.yaml +++ b/src/flextype/settings.yaml @@ -537,9 +537,9 @@ parsers: registry: enabled: true path: "/src/flextype/core/Parsers/Shortcodes/RegistryShortcode.php" - url: + strings: enabled: true - path: "/src/flextype/core/Parsers/Shortcodes/UrlShortcode.php" + path: "/src/flextype/core/Parsers/Shortcodes/StringsShortcode.php" # CORS # diff --git a/tests/fixtures/settings/settings.yaml b/tests/fixtures/settings/settings.yaml index 5a8e041d..620d7f5b 100644 --- a/tests/fixtures/settings/settings.yaml +++ b/tests/fixtures/settings/settings.yaml @@ -521,7 +521,10 @@ parsers: url: enabled: true path: "/src/flextype/core/Parsers/Shortcodes/UrlShortcode.php" - + strings: + enabled: true + path: "/src/flextype/core/Parsers/Shortcodes/StringsShortcode.php" + # CORS # # CORS (Cross-origin resource sharing) allows JavaScript web apps to make HTTP requests to other domains. diff --git a/tests/src/flextype/core/Parsers/Shortcodes/StringsShortcodeTest.php b/tests/src/flextype/core/Parsers/Shortcodes/StringsShortcodeTest.php new file mode 100644 index 00000000..961145ed --- /dev/null +++ b/tests/src/flextype/core/Parsers/Shortcodes/StringsShortcodeTest.php @@ -0,0 +1,37 @@ +assertEquals("zed foo bar", parsers()->shortcodes()->parse('[strings lower]zed foo bar[/strings]')); + + // upper + $this->assertEquals("ZED FOO BAR", parsers()->shortcodes()->parse('[strings upper]zed foo bar[/strings]')); + + // append + $this->assertEquals("zed foo bar", parsers()->shortcodes()->parse('[strings append=" bar"]zed foo[/strings]')); + + // prepend + $this->assertEquals("zed foo bar", parsers()->shortcodes()->parse('[strings prepend="zed "]foo bar[/strings]')); + + // sort + $this->assertEquals("a b c", parsers()->shortcodes()->parse('[strings sort="asc"]b a c[/strings]')); + $this->assertEquals("c b a", parsers()->shortcodes()->parse('[strings sort="desc"]b a c[/strings]')); + + // after + $this->assertEquals(" bar zed", parsers()->shortcodes()->parse('[strings after="foo"]foo bar zed[/strings]')); + + // afterLast + $this->assertEquals(" bar zed", parsers()->shortcodes()->parse('[strings afterLast="foo"]foo foo bar zed[/strings]')); + + // before + $this->assertEquals("foo bar ", parsers()->shortcodes()->parse('[strings before="zed"]foo bar zed[/strings]')); + + // beforeLast + $this->assertEquals("foo ", parsers()->shortcodes()->parse('[strings beforeLast="foo"]foo foo bar zed[/strings]')); + + // wordsLimit + $this->assertEquals("foo...", parsers()->shortcodes()->parse('[strings wordsLimit="1"]foo bar zed[/strings]')); + $this->assertEquals("foo >>>", parsers()->shortcodes()->parse('[strings wordsLimit="1| >>>"]foo bar zed[/strings]')); +}); \ No newline at end of file