1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-08 06:06:45 +02:00

feat(shortcodes): use , for vars delimeter by default

This commit is contained in:
Awilum
2022-05-14 17:27:44 +03:00
parent 4e9acc350e
commit 0f929bcb9d
5 changed files with 44 additions and 30 deletions

View File

@@ -27,7 +27,7 @@ parsers()->shortcodes()->addHandler('tr', static function (ShortcodeInterface $s
return '';
}
$varsDelimeter = $s->getParameter('varsDelimeter') ?: '|';
$varsDelimeter = $s->getParameter('varsDelimeter') ?: ',';
if ($s->getParameter('find') != null) {

View File

@@ -27,7 +27,7 @@ parsers()->shortcodes()->addHandler('registry', static function (ShortcodeInterf
return '';
}
$varsDelimeter = $s->getParameter('varsDelimeter') ?: '|';
$varsDelimeter = $s->getParameter('varsDelimeter') ?: ',';
if ($s->getParameter('get') != null) {

View File

@@ -27,8 +27,7 @@ parsers()->shortcodes()->addHandler('strings', static function (ShortcodeInterfa
}
$content = $s->getContent();
$varsDelimeter = $s->getParameter('varsDelimeter') ?: '|';
$itemsDelimeter = $s->getParameter('itemsDelimeter') ?: ',';
$varsDelimeter = $s->getParameter('varsDelimeter') ?: ',';
foreach($s->getParameters() as $key => $value) {
@@ -115,15 +114,30 @@ parsers()->shortcodes()->addHandler('strings', static function (ShortcodeInterfa
}
if ($key == 'contains') {
$content = strings($content)->{'contains'}(isset($vars[0]) ? explode($itemsDelimeter, $vars[0]) : '', isset($vars[1]) ? strings($vars[1])->toBoolean() : true) ? "true" : "false";
if (isset($vars[0])) {
parse_str($vars[0], $values);
} else {
$values = [];
}
$content = strings($content)->{'contains'}(array_keys($values), (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 (isset($vars[0])) {
parse_str($vars[0], $values);
} else {
$values = [];
}
$content = strings($content)->{'containsAll'}(array_keys($values), (isset($vars[1]) ? strings($vars[1])->toBoolean() : true)) ? "true" : "false";
}
if ($key == 'containsAny') {
$content = strings($content)->{'containsAny'}(isset($vars[0]) ? explode($itemsDelimeter, $vars[0]) : '', isset($vars[1]) ? strings($vars[1])->toBoolean() : true) ? "true" : "false";
if (isset($vars[0])) {
parse_str($vars[0], $values);
} else {
$values = [];
}
$content = strings($content)->{'containsAny'}(array_keys($values), isset($vars[1]) ? strings($vars[1])->toBoolean() : true) ? "true" : "false";
}
if ($key == 'count') {
@@ -151,7 +165,7 @@ parsers()->shortcodes()->addHandler('strings', static function (ShortcodeInterfa
}
if ($key == 'format') {
$formatVars = isset($vars[0]) ? explode($itemsDelimeter, $vars[0]) : [];
$formatVars = $vars;
if (count($formatVars) > 0) {
$content = strings($content)->{'format'}(...$formatVars)->toString();
}

View File

@@ -5,6 +5,6 @@ declare(strict_types=1);
test('[registry] shortcode', function () {
expect(strings(parsers()->shortcodes()->parse('[registry get="flextype.manifest"]'))->isJson())->toBeTrue();
expect(parsers()->shortcodes()->parse('[registry get="flextype.manifest.name"]'))->toBe('Flextype');
expect(parsers()->shortcodes()->parse('[registry get="flextype.manifest.foo|Default"]'))->toBe('Default');
expect(parsers()->shortcodes()->parse('[registry get="flextype.manifest.foo,Default"]'))->toBe('Default');
});

View File

@@ -33,7 +33,7 @@ test('[strings] shortcode', function () {
// 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]'));
$this->assertEquals("foo >>>", parsers()->shortcodes()->parse('[strings wordsLimit="1, >>>"]foo bar zed[/strings]'));
// at
$this->assertEquals("a", parsers()->shortcodes()->parse('[strings at=0]abc[/strings]'));
@@ -47,7 +47,7 @@ test('[strings] shortcode', function () {
$this->assertEquals("dGVzdA==", parsers()->shortcodes()->parse('[strings base64Encode]test[/strings]'));
// between
$this->assertEquals("b", parsers()->shortcodes()->parse('[strings between=a|c]abc[/strings]'));
$this->assertEquals("b", parsers()->shortcodes()->parse('[strings between=a,c]abc[/strings]'));
// camel
$this->assertEquals("fooBar", parsers()->shortcodes()->parse('[strings camel]foo_bar[/strings]'));
@@ -68,22 +68,22 @@ test('[strings] shortcode', function () {
// 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]'));
$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]'));
// containsAny
$this->assertEquals("true", parsers()->shortcodes()->parse('[strings containsAny=SG-1]SG-1 returns from an off-world mission to P9Y-3C3[/strings]'));
$this->assertEquals("true", parsers()->shortcodes()->parse('[strings containsAny=SG-1,P9Y-3C3]SG-1 returns from an off-world mission to P9Y-3C3[/strings]'));
$this->assertEquals("true", parsers()->shortcodes()->parse('[strings containsAny=SG-1,XXX-3C3]SG-1 returns from an off-world mission to P9Y-3C3[/strings]'));
$this->assertEquals("true", parsers()->shortcodes()->parse('[strings containsAny=sg-1,P9Y-3C3|false]SG-1 returns from an off-world mission to P9Y-3C3[/strings]'));
$this->assertEquals("true", parsers()->shortcodes()->parse('[strings containsAny=SG-1&P9Y-3C3]SG-1 returns from an off-world mission to P9Y-3C3[/strings]'));
$this->assertEquals("true", parsers()->shortcodes()->parse('[strings containsAny=SG-1&XXX-3C3]SG-1 returns from an off-world mission to P9Y-3C3[/strings]'));
$this->assertEquals("true", parsers()->shortcodes()->parse('[strings containsAny=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]'));
// 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]'));
$this->assertEquals(2, parsers()->shortcodes()->parse('[strings countSubString=test,false]Test string here for test[/strings]'));
// crc32
$this->assertEquals(3632233996, parsers()->shortcodes()->parse('[strings crc32]test[/strings]'));
@@ -100,7 +100,7 @@ test('[strings] shortcode', function () {
$this->assertEquals("SG", parsers()->shortcodes()->parse('[strings firstSegment="-"]SG-1 returns from an off-world mission[/strings]'));
// format
$this->assertEquals("There are 5 monkeys in the tree", parsers()->shortcodes()->parse('[strings format=5,tree]There are %d monkeys in the %s[/strings]'));
$this->assertEquals("There are 5 monkeys in the tree", parsers()->shortcodes()->parse('[strings format="5,tree"]There are %d monkeys in the %s[/strings]'));
// getEncoding
$this->assertEquals("UTF-8", parsers()->shortcodes()->parse('[strings getEncoding]Foo[/strings]'));
@@ -111,7 +111,7 @@ test('[strings] shortcode', function () {
// increment
$this->assertEquals("Page_2", parsers()->shortcodes()->parse('[strings increment]Page_1[/strings]'));
$this->assertEquals("Page-2", parsers()->shortcodes()->parse('[strings increment=1|-]Page-1[/strings]'));
$this->assertEquals("Page-2", parsers()->shortcodes()->parse('[strings increment=1,-]Page-1[/strings]'));
// indexOf
$this->assertEquals(1, parsers()->shortcodes()->parse('[strings indexOf=e]hello[/strings]'));
@@ -120,8 +120,8 @@ test('[strings] shortcode', function () {
$this->assertEquals(3, parsers()->shortcodes()->parse('[strings indexOfLast=l]hello[/strings]'));
// insert
$this->assertEquals("hello world", parsers()->shortcodes()->parse('[strings insert="hello |0"]world[/strings]'));
$this->assertEquals("hello world", parsers()->shortcodes()->parse('[strings insert=" world|5"]hello[/strings]'));
$this->assertEquals("hello world", parsers()->shortcodes()->parse('[strings insert="hello ,0"]world[/strings]'));
$this->assertEquals("hello world", parsers()->shortcodes()->parse('[strings insert=" world,5"]hello[/strings]'));
// isAlpha
$this->assertEquals("true", parsers()->shortcodes()->parse('[strings isAlpha]foo[/strings]'));
@@ -178,7 +178,7 @@ test('[strings] shortcode', function () {
$this->assertEquals("true", parsers()->shortcodes()->parse('[strings isNumeric]12345[/strings]'));
// isPrintable
$this->assertEquals("true", parsers()->shortcodes()->parse('[strings isPrintable]!@#$%^&*()_+-=[]{};\':\",./<>?\\|`~"[/strings]'));
$this->assertEquals("true", parsers()->shortcodes()->parse('[strings isPrintable]!@#$%^&*()_+-=[]{};\':\"./<>?\\`~"[/strings]'));
// isPunctuation
$this->assertEquals("true", parsers()->shortcodes()->parse('[strings isPunctuation],[/strings]'));
@@ -189,7 +189,7 @@ test('[strings] shortcode', function () {
// isSimilar
$this->assertEquals("true", parsers()->shortcodes()->parse('[strings isSimilar=Foo]Foo[/strings]'));
$this->assertEquals("true", parsers()->shortcodes()->parse('[strings isSimilar=Foo|50]Foo[/strings]'));
$this->assertEquals("true", parsers()->shortcodes()->parse('[strings isSimilar=Foo,50]Foo[/strings]'));
// isSerialized
$this->assertEquals("true", parsers()->shortcodes()->parse('[strings isSerialized]a:1:{i:0;s:3:"foo";}[/strings]'));
@@ -211,7 +211,7 @@ test('[strings] shortcode', function () {
$this->assertEquals("01677e4c0ae5468b9b8b823487f14524", parsers()->shortcodes()->parse('[strings md5]Foo Bar[/strings]'));
// move
$this->assertEquals("worldhello", parsers()->shortcodes()->parse('[strings move=0|5|10]helloworld[/strings]'));
$this->assertEquals("worldhello", parsers()->shortcodes()->parse('[strings move=0,5,10]helloworld[/strings]'));
// normalizeNewLines
$this->assertEquals("\n \n", parsers()->shortcodes()->parse("[strings normalizeNewLines]\r\n \r[/strings]"));
@@ -240,7 +240,7 @@ test('[strings] shortcode', function () {
// random
$test1 = parsers()->shortcodes()->parse('[strings random /]');
$test2 = parsers()->shortcodes()->parse('[strings random=10 /]');
$test3 = parsers()->shortcodes()->parse('[strings random=4|1234 /]');
$test3 = parsers()->shortcodes()->parse('[strings random=4,1234 /]');
$this->assertEquals(64, strings($test1)->length());
$this->assertEquals(10, strings($test2)->length());
$this->assertEquals(4, strings($test3)->length());
@@ -252,17 +252,17 @@ test('[strings] shortcode', function () {
$this->assertEquals("foofoofoo", parsers()->shortcodes()->parse('[strings repeat=3]foo[/strings]'));
// replace
$this->assertEquals("bar baz", parsers()->shortcodes()->parse('[strings replace=foo|bar]foo baz[/strings]'));
$this->assertEquals("bar baz", parsers()->shortcodes()->parse('[strings replace=foo,bar]foo baz[/strings]'));
// replaceDashes
$this->assertEquals("foobarbaz", parsers()->shortcodes()->parse('[strings replaceDashes]foo-bar-baz[/strings]'));
$this->assertEquals("foo_bar_baz", parsers()->shortcodes()->parse('[strings replaceDashes="_"]foo-bar-baz[/strings]'));
// replaceFirst
$this->assertEquals("bar foo bar", parsers()->shortcodes()->parse('[strings replaceFirst=foo|bar]foo foo bar[/strings]'));
$this->assertEquals("bar foo bar", parsers()->shortcodes()->parse('[strings replaceFirst=foo,bar]foo foo bar[/strings]'));
// replaceLast
$this->assertEquals("foo bar bar", parsers()->shortcodes()->parse('[strings replaceLast=foo|bar]foo foo bar[/strings]'));
$this->assertEquals("foo bar bar", parsers()->shortcodes()->parse('[strings replaceLast=foo,bar]foo foo bar[/strings]'));
// replaceNonAlpha
$this->assertEquals("foo baz bar", parsers()->shortcodes()->parse('[strings replaceNonAlpha]foo 123 baz 345 bar[/strings]'));
@@ -278,7 +278,7 @@ test('[strings] shortcode', function () {
// segement
$this->assertEquals("foo", parsers()->shortcodes()->parse('[strings segment=0]foo bar baz[/strings]'));
$this->assertEquals("foo", parsers()->shortcodes()->parse('[strings segment="0|/"]foo/bar/baz[/strings]'));
$this->assertEquals("foo", parsers()->shortcodes()->parse('[strings segment="0,/"]foo/bar/baz[/strings]'));
// segements
$this->assertEquals('["foo","bar","baz"]', parsers()->shortcodes()->parse('[strings segments]foo bar baz[/strings]'));