From 0fe947395c985c849c1dfb63222c42900ac963b2 Mon Sep 17 00:00:00 2001 From: Awilum Date: Thu, 5 May 2022 08:01:22 +0300 Subject: [PATCH] feat(shortcodes): `[strings]` shortcode - add `countSubString ` modifier and fix vars logic --- .../Parsers/Shortcodes/StringsShortcode.php | 21 +++++++++++-------- .../Shortcodes/StringsShortcodeTest.php | 6 +++++- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/flextype/core/Parsers/Shortcodes/StringsShortcode.php b/src/flextype/core/Parsers/Shortcodes/StringsShortcode.php index 31219faf..c73ee4b1 100644 --- a/src/flextype/core/Parsers/Shortcodes/StringsShortcode.php +++ b/src/flextype/core/Parsers/Shortcodes/StringsShortcode.php @@ -31,7 +31,7 @@ parsers()->shortcodes()->addHandler('strings', static function (ShortcodeInterfa foreach($s->getParameters() as $key => $value) { - $vars = $value !== null ? strings($value)->contains($varsDelimeter) ? explode($varsDelimeter, $value) : $value : []; + $vars = $value !== null ? strings($value)->contains($varsDelimeter) ? explode($varsDelimeter, $value) : [$value] : []; if ($key == 'append') { if (is_iterable($vars)) { @@ -50,19 +50,19 @@ parsers()->shortcodes()->addHandler('strings', static function (ShortcodeInterfa } if ($key == 'after') { - $content = strings($content)->{'after'}($vars)->toString(); + $content = strings($content)->{'after'}($vars[0])->toString(); } if ($key == 'afterLast') { - $content = strings($content)->{'afterLast'}($vars)->toString(); + $content = strings($content)->{'afterLast'}($vars[0])->toString(); } if ($key == 'before') { - $content = strings($content)->{'before'}($vars)->toString(); + $content = strings($content)->{'before'}($vars[0])->toString(); } if ($key == 'beforeLast') { - $content = strings($content)->{'beforeLast'}($vars)->toString(); + $content = strings($content)->{'beforeLast'}($vars[0])->toString(); } if ($key == 'lower') { @@ -74,15 +74,15 @@ parsers()->shortcodes()->addHandler('strings', static function (ShortcodeInterfa } if ($key == 'sort') { - $content = strings($content)->{'wordsSort' . strings($vars)->ucfirst()}()->toString(); + $content = strings($content)->{'wordsSort' . strings($vars[0])->ucfirst()}()->toString(); } if ($key == 'wordsLimit') { - $content = strings($content)->{'wordsLimit'}(isset($vars[0]) ? (int) $vars[0] : 100, isset($vars[1]) ? (string) $vars[1] : '...')->toString(); + $content = strings($content)->{'wordsLimit'}(isset($vars[0]) ? strings($vars[0])->toInteger() : 100, isset($vars[1]) ? (string) $vars[1] : '...')->toString(); } if ($key == 'at') { - $content = strings($content)->{'at'}((int) $vars)->toString(); + $content = strings($content)->{'at'}(strings($vars[0])->toInteger())->toString(); } if ($key == 'base64Decode') { @@ -114,13 +114,16 @@ parsers()->shortcodes()->addHandler('strings', static function (ShortcodeInterfa } if ($key == 'contains') { - $content = strings($content)->{'contains'}(isset($vars[0]) ? (string) $vars[0] : '', isset($vars[1]) ? (bool) $vars[1] : true) ? "true" : "false"; + $content = strings($content)->{'contains'}(isset($vars[0]) ? (string) $vars[0] : '', isset($vars[1]) ? strings($vars[1])->toBoolean() : true) ? "true" : "false"; } if ($key == 'count') { $content = (string) strings($content)->{'count'}(); } + if ($key == 'countSubString') { + $content = (string) strings($content)->{'countSubString'}(isset($vars[0]) ? (string) $vars[0] : '', isset($vars[1]) ? strings($vars[1])->toBoolean() : true); + } } return (string) $content; diff --git a/tests/src/flextype/core/Parsers/Shortcodes/StringsShortcodeTest.php b/tests/src/flextype/core/Parsers/Shortcodes/StringsShortcodeTest.php index 919b27ee..0ae0808c 100644 --- a/tests/src/flextype/core/Parsers/Shortcodes/StringsShortcodeTest.php +++ b/tests/src/flextype/core/Parsers/Shortcodes/StringsShortcodeTest.php @@ -63,9 +63,13 @@ test('[strings] shortcode', function () { // contains $this->assertEquals("true", parsers()->shortcodes()->parse('[strings contains=SG-1]SG-1 returns from an off-world mission to P9Y-3C3[/strings]')); - $this->assertEquals("false", parsers()->shortcodes()->parse('[strings contains=sg-1|false]SG-1 returns from an off-world mission to P9Y-3C3[/strings]')); + $this->assertEquals("false", parsers()->shortcodes()->parse('[strings contains=sg-1]SG-1 returns from an off-world mission to P9Y-3C3[/strings]')); // count $this->assertEquals(49, parsers()->shortcodes()->parse('[strings count]SG-1 returns from an off-world mission to P9Y-3C3[/strings]')); + // count + $this->assertEquals(1, parsers()->shortcodes()->parse('[strings countSubString=test]Test string here for test[/strings]')); + $this->assertEquals(2, parsers()->shortcodes()->parse('[strings countSubString=test|false]Test string here for test[/strings]')); + }); \ No newline at end of file