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

feat(shortcodes): [strings] shortcode - add more modifiers

This commit is contained in:
Awilum
2022-05-05 14:06:38 +03:00
parent d0aaab465f
commit 6d680b2e8b
2 changed files with 297 additions and 1 deletions

View File

@@ -139,7 +139,7 @@ parsers()->shortcodes()->addHandler('strings', static function (ShortcodeInterfa
}
if ($key == 'endsWith') {
$content = strings($content)->{'endsWith'}(isset($vars[0]) ? (string) $vars[0] : '') ? "true" : "false";
$content = strings($content)->{'endsWith'}(isset($vars[0]) ? (string) $vars[0] : '') ? "true" : "false";
}
if ($key == 'finish') {
@@ -156,6 +156,179 @@ parsers()->shortcodes()->addHandler('strings', static function (ShortcodeInterfa
$content = strings($content)->{'format'}(...$formatVars)->toString();
}
}
if ($key == 'getEncoding') {
$content = strings($content)->{'getEncoding'}();
}
if ($key == 'setEncoding') {
$content = strings($content)->{'setEncoding'}(isset($vars[0]) ? (string) $vars[0] : '');
}
if ($key == 'hash') {
$content = strings($content)->{'hash'}(isset($vars[0]) ? (string) $vars[0] : 'md5', isset($vars[1]) ? strings($vars[1])->toBoolean() : false)->toString();
}
if ($key == 'increment') {
$content = strings($content)->{'increment'}(isset($vars[0]) ? (int) $vars[0] : 1, isset($vars[1]) ? (string) $vars[1] : '_')->toString();
}
if ($key == 'indexOf') {
$content = strings($content)->{'indexOf'}(isset($vars[0]) ? (string) $vars[0] : '', isset($vars[1]) ? (int) $vars[1] : 0, isset($vars[2]) ? strings($vars[2])->toBoolean() : true);
}
if ($key == 'indexOfLast') {
$content = strings($content)->{'indexOfLast'}(isset($vars[0]) ? (string) $vars[0] : '', isset($vars[1]) ? (int) $vars[1] : 0, isset($vars[2]) ? strings($vars[2])->toBoolean() : true);
}
if ($key == 'insert') {
$content = strings($content)->{'insert'}(isset($vars[0]) ? (string) $vars[0] : '', isset($vars[1]) ? (int) $vars[1] : 0);
}
if ($key == 'isAlpha') {
$content = strings($content)->{'isAlpha'}() ? "true" : "false";
}
if ($key == 'isAlphanumeric') {
$content = strings($content)->{'isAlphanumeric'}() ? "true" : "false";
}
if ($key == 'isAscii') {
$content = strings($content)->{'isAscii'}() ? "true" : "false";
}
if ($key == 'isBase64') {
$content = strings($content)->{'isBase64'}() ? "true" : "false";
}
if ($key == 'isBlank') {
$content = strings($content)->{'isBlank'}() ? "true" : "false";
}
if ($key == 'isBoolean') {
$content = strings($content)->{'isBoolean'}() ? "true" : "false";
}
if ($key == 'isDigit') {
$content = strings($content)->{'isDigit'}() ? "true" : "false";
}
if ($key == 'isEmail') {
$content = strings($content)->{'isEmail'}() ? "true" : "false";
}
if ($key == 'isEmpty') {
$content = strings($content)->{'isEmpty'}() ? "true" : "false";
}
if ($key == 'isEqual') {
$content = strings($content)->{'isEqual'}(isset($vars[0]) ? (string) $vars[0] : '') ? "true" : "false";
}
if ($key == 'isFalse') {
$content = strings($content)->{'isFalse'}() ? "true" : "false";
}
if ($key == 'isTrue') {
$content = strings($content)->{'isTrue'}() ? "true" : "false";
}
if ($key == 'isHexadecimal') {
$content = strings($content)->{'isHexadecimal'}() ? "true" : "false";
}
if ($key == 'isHTML') {
$content = strings($content)->{'isHTML'}() ? "true" : "false";
}
if ($key == 'isIP') {
$content = strings($content)->{'isIP'}() ? "true" : "false";
}
if ($key == 'isJson') {
$content = strings($content)->{'isJson'}() ? "true" : "false";
}
if ($key == 'isUpper') {
$content = strings($content)->{'isUpper'}() ? "true" : "false";
}
if ($key == 'isLower') {
$content = strings($content)->{'isLower'}() ? "true" : "false";
}
if ($key == 'isMAC') {
$content = strings($content)->{'isMAC'}() ? "true" : "false";
}
if ($key == 'isNumeric') {
$content = strings($content)->{'isNumeric'}() ? "true" : "false";
}
if ($key == 'isPrintable') {
$content = strings($content)->{'isPrintable'}() ? "true" : "false";
}
if ($key == 'isPunctuation') {
$content = strings($content)->{'isPunctuation'}() ? "true" : "false";
}
if ($key == 'isUrl') {
$content = strings($content)->{'isUrl'}() ? "true" : "false";
}
if ($key == 'isSimilar') {
$content = strings($content)->{'isSimilar'}(isset($vars[0]) ? (string) $vars[0] : '', isset($vars[1]) ? (float) $vars[1] : 80.0) ? "true" : "false";
}
if ($key == 'isSerialized') {
$content = strings($content)->{'isSerialized'}() ? "true" : "false";
}
if ($key == 'kebab') {
$content = strings($content)->{'kebab'}()->toString();
}
if ($key == 'lastSegment') {
$content = strings($content)->{'lastSegment'}(isset($vars[0]) ? (string) $vars[0] : ' ')->toString();
}
if ($key == 'length') {
$content = strings($content)->{'length'}();
}
if ($key == 'limit') {
$content = strings($content)->{'limit'}(isset($vars[0]) ? strings($vars[0])->toInteger() : 100, isset($vars[1]) ? (string) $vars[1] : '...')->toString();
}
if ($key == 'lines') {
$content = serializers()->json()->encode(strings($content)->{'lines'}());
}
if ($key == 'md5') {
$content = strings($content)->{'md5'}()->toString();
}
if ($key == 'move') {
$content = strings($content)->{'move'}(isset($vars[0]) ? strings($vars[0])->toInteger() : 0, isset($vars[1]) ? strings($vars[1])->toInteger() : 0, isset($vars[2]) ? strings($vars[2])->toInteger() : 0)->toString();
}
if ($key == 'normalizeNewLines') {
$content = strings($content)->{'normalizeNewLines'}()->toString();
}
if ($key == 'normalizeSpaces') {
$content = strings($content)->{'normalizeSpaces'}()->toString();
}
if ($key == 'offsetExists') {
$content = strings($content)->{'offsetExists'}(isset($vars[0]) ? strings($vars[0])->toInteger() : 0) ? "true" : "false";
}
if ($key == 'offsetGet') {
$content = strings($content)->{'offsetGet'}(isset($vars[0]) ? strings($vars[0])->toString() : 0);
}
}
return (string) $content;

View File

@@ -101,4 +101,127 @@ test('[strings] shortcode', function () {
// 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]'));
// getEncoding
$this->assertEquals("UTF-8", parsers()->shortcodes()->parse('[strings getEncoding]Foo[/strings]'));
// hash
$this->assertEquals("1356c67d7ad1638d816bfb822dd2c25d", parsers()->shortcodes()->parse('[strings hash]Foo[/strings]'));
$this->assertEquals("201a6b3053cc1422d2c3670b62616221d2290929", parsers()->shortcodes()->parse('[strings hash=sha1]Foo[/strings]'));
// 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]'));
// indexOf
$this->assertEquals(1, parsers()->shortcodes()->parse('[strings indexOf=e]hello[/strings]'));
// indexOfLast
$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]'));
// isAlpha
$this->assertEquals("true", parsers()->shortcodes()->parse('[strings isAlpha]foo[/strings]'));
// isAlphanumeric
$this->assertEquals("true", parsers()->shortcodes()->parse('[strings isAlphanumeric]fòôbàřs12345[/strings]'));
// isAscii
$this->assertEquals("true", parsers()->shortcodes()->parse('[strings isAscii]#@$%[/strings]'));
// isBase64
$this->assertEquals("true", parsers()->shortcodes()->parse('[strings isBlank][/strings]'));
// isDigit
$this->assertEquals("true", parsers()->shortcodes()->parse('[strings isDigit]01234569[/strings]'));
// isEmail
$this->assertEquals("true", parsers()->shortcodes()->parse('[strings isEmail]awilum@msn.com[/strings]'));
// isEmpty
$this->assertEquals("true", parsers()->shortcodes()->parse('[strings isEmpty][/strings]'));
// isEqual
$this->assertEquals("true", parsers()->shortcodes()->parse('[strings isEqual=Foo]Foo[/strings]'));
// isTrue
$this->assertEquals("true", parsers()->shortcodes()->parse('[strings isTrue]true[/strings]'));
// isFalse
$this->assertEquals("true", parsers()->shortcodes()->parse('[strings isFalse]false[/strings]'));
// isHexadecimal
$this->assertEquals("true", parsers()->shortcodes()->parse('[strings isHexadecimal]19FDE[/strings]'));
// isHTML
$this->assertEquals("true", parsers()->shortcodes()->parse('[strings isHTML]<p>Hello</p>[/strings]'));
// isIP
$this->assertEquals("true", parsers()->shortcodes()->parse('[strings isIP]127.0.0.1[/strings]'));
// isJSON
$this->assertEquals("true", parsers()->shortcodes()->parse('[strings isJson]{"foo":"bar"}[/strings]'));
// isLower
$this->assertEquals("true", parsers()->shortcodes()->parse('[strings isLower]foo[/strings]'));
// isMAC
$this->assertEquals("true", parsers()->shortcodes()->parse('[strings isMAC]00:00:00:00:00:00[/strings]'));
// isUpper
$this->assertEquals("true", parsers()->shortcodes()->parse('[strings isUpper]FOO[/strings]'));
// isNumeric
$this->assertEquals("true", parsers()->shortcodes()->parse('[strings isNumeric]12345[/strings]'));
// isPrintable
$this->assertEquals("true", parsers()->shortcodes()->parse('[strings isPrintable]!@#$%^&*()_+-=[]{};\':\",./<>?\\|`~"[/strings]'));
// isPunctuation
$this->assertEquals("true", parsers()->shortcodes()->parse('[strings isPunctuation],[/strings]'));
// isUrl
$this->assertEquals("true", parsers()->shortcodes()->parse('[strings isUrl]http://awilum.github.io[/strings]'));
$this->assertEquals("true", parsers()->shortcodes()->parse('[strings isUrl]https://awilum.github.io[/strings]'));
// isSimilar
$this->assertEquals("true", parsers()->shortcodes()->parse('[strings isSimilar=Foo]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]'));
// kebab
$this->assertEquals("foo-bar", parsers()->shortcodes()->parse('[strings kebab]Foo Bar[/strings]'));
// lastSegment
$this->assertEquals("baz", parsers()->shortcodes()->parse('[strings lastSegment]foo bar baz[/strings]'));
$this->assertEquals("baz", parsers()->shortcodes()->parse('[strings lastSegment="/"]foo/bar/baz[/strings]'));
// length
$this->assertEquals(11, parsers()->shortcodes()->parse('[strings length]foo bar baz[/strings]'));
// lines
$this->assertEquals('["Fòô òô"," fòô fò fò ","fò"]', parsers()->shortcodes()->parse("[strings lines]Fòô òô\n fòô fò fò \n\r[/strings]"));
// md5
$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]'));
// normalizeNewLines
$this->assertEquals("\n \n", parsers()->shortcodes()->parse("[strings normalizeNewLines]\r\n \r[/strings]"));
// normalizeSpaces
$this->assertEquals("foo bar baz", parsers()->shortcodes()->parse("[strings normalizeSpaces]foo bar baz[/strings]"));
// offsetExists
$this->assertEquals("true", parsers()->shortcodes()->parse('[strings offsetExists=0]foo bar baz[/strings]'));
// offsetGet
$this->assertEquals("f", parsers()->shortcodes()->parse('[strings offsetGet=0]foo bar baz[/strings]'));
});