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

feat(shortcodes): [strings] shortcode - add format modifier

This commit is contained in:
Awilum
2022-05-05 09:28:43 +03:00
parent f2cb8548ee
commit d0aaab465f
2 changed files with 12 additions and 2 deletions

View File

@@ -38,7 +38,7 @@ parsers()->shortcodes()->addHandler('strings', static function (ShortcodeInterfa
if (is_iterable($vars)) {
$content = strings($content)->{'append'}(...$vars)->toString();
} else {
$content = strings($content)->{'append'}($vars)->toString();
$content = strings($content)->{'append'}($vars[0])->toString();
}
}
@@ -46,7 +46,7 @@ parsers()->shortcodes()->addHandler('strings', static function (ShortcodeInterfa
if (is_iterable($vars)) {
$content = strings($content)->{'prepend'}(...$vars)->toString();
} else {
$content = strings($content)->{'prepend'}($vars)->toString();
$content = strings($content)->{'prepend'}($vars[0])->toString();
}
}
@@ -149,6 +149,13 @@ parsers()->shortcodes()->addHandler('strings', static function (ShortcodeInterfa
if ($key == 'firstSegment') {
$content = strings($content)->{'firstSegment'}(isset($vars[0]) ? (string) $vars[0] : ' ')->toString();
}
if ($key == 'format') {
$formatVars = isset($vars[0]) ? explode($itemsDelimeter, $vars[0]) : [];
if (count($formatVars) > 0) {
$content = strings($content)->{'format'}(...$formatVars)->toString();
}
}
}
return (string) $content;

View File

@@ -98,4 +98,7 @@ test('[strings] shortcode', function () {
// firstSegment
$this->assertEquals("SG-1", parsers()->shortcodes()->parse('[strings firstSegment]SG-1 returns from an off-world mission[/strings]'));
$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]'));
});