From 385b1c9f2335ba9ee02cca2366fbdf899381156c Mon Sep 17 00:00:00 2001 From: Awilum Date: Thu, 5 May 2022 08:21:05 +0300 Subject: [PATCH] feat(shortcodes): `[strings]` shortcode - add `containsAll` modifier --- .../core/Parsers/Shortcodes/StringsShortcode.php | 7 ++++++- .../core/Parsers/Shortcodes/StringsShortcodeTest.php | 9 ++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/flextype/core/Parsers/Shortcodes/StringsShortcode.php b/src/flextype/core/Parsers/Shortcodes/StringsShortcode.php index c73ee4b1..ed1ee6c1 100644 --- a/src/flextype/core/Parsers/Shortcodes/StringsShortcode.php +++ b/src/flextype/core/Parsers/Shortcodes/StringsShortcode.php @@ -28,6 +28,7 @@ parsers()->shortcodes()->addHandler('strings', static function (ShortcodeInterfa $content = $s->getContent(); $varsDelimeter = $s->getParameter('varsDelimeter') ?: '|'; + $itemsDelimeter = $s->getParameter('itemsDelimeter') ?: ','; foreach($s->getParameters() as $key => $value) { @@ -114,7 +115,11 @@ parsers()->shortcodes()->addHandler('strings', static function (ShortcodeInterfa } if ($key == 'contains') { - $content = strings($content)->{'contains'}(isset($vars[0]) ? (string) $vars[0] : '', isset($vars[1]) ? strings($vars[1])->toBoolean() : true) ? "true" : "false"; + $content = strings($content)->{'contains'}(isset($vars[0]) ? explode($itemsDelimeter, $vars[0]) : '', isset($vars[1]) ? strings($vars[1])->toBoolean() : true) ? "true" : "false"; + } + + if ($key == 'containsAll') { + $content = strings($content)->{'containsAll'}(isset($vars[0]) ? explode($itemsDelimeter, $vars[0]) : '', isset($vars[1]) ? strings($vars[1])->toBoolean() : true) ? "true" : "false"; } if ($key == 'count') { diff --git a/tests/src/flextype/core/Parsers/Shortcodes/StringsShortcodeTest.php b/tests/src/flextype/core/Parsers/Shortcodes/StringsShortcodeTest.php index 0ae0808c..c9554aba 100644 --- a/tests/src/flextype/core/Parsers/Shortcodes/StringsShortcodeTest.php +++ b/tests/src/flextype/core/Parsers/Shortcodes/StringsShortcodeTest.php @@ -63,12 +63,19 @@ 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("true", parsers()->shortcodes()->parse('[strings contains=SG-1,P9Y-3C3]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]')); + // containsAll + $this->assertEquals("true", parsers()->shortcodes()->parse('[strings containsAll=SG-1]SG-1 returns from an off-world mission to P9Y-3C3[/strings]')); + $this->assertEquals("true", parsers()->shortcodes()->parse('[strings containsAll=SG-1,P9Y-3C3]SG-1 returns from an off-world mission to P9Y-3C3[/strings]')); + $this->assertEquals("false", parsers()->shortcodes()->parse('[strings containsAll=SG-1,XXX-3C3]SG-1 returns from an off-world mission to P9Y-3C3[/strings]')); + $this->assertEquals("true", parsers()->shortcodes()->parse('[strings containsAll=sg-1,P9Y-3C3|false]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 + // countSubString $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]'));