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

feat(shortcodes): simplify logic for tr shortcode

This commit is contained in:
Awilum
2022-05-25 20:17:49 +03:00
parent 4f362dd9b4
commit 6a26599399
2 changed files with 9 additions and 5 deletions

View File

@@ -21,21 +21,25 @@ use Thunder\Shortcode\Shortcode\ShortcodeInterface;
use function parsers;
use function registry;
// Shortcode: [tr]
// Shortcode: tr
// Usage: (tr:foo)
parsers()->shortcodes()->addHandler('tr', static function (ShortcodeInterface $s) {
if (! registry()->get('flextype.settings.parsers.shortcodes.shortcodes.i18n.enabled')) {
return '';
}
$varsDelimeter = $s->getParameter('varsDelimeter') ?: ',';
$varsDelimeter = ($s->getParameter('varsDelimeter') != null) ? parsers()->shortcodes()->parse($s->getParameter('varsDelimeter')) : ',';
if ($s->getParameter('find') != null) {
if ($s->getBbCode() != null) {
// Get vars
foreach($s->getParameters() as $key => $value) {
$vars = $value !== null ? strings($value)->contains($varsDelimeter) ? explode($varsDelimeter, $value) : [$value] : [];
}
// Parse shortcodes for each var.
$vars = array_map(fn($v) => parsers()->shortcodes()->parse(is_string($v) ? $v : ''), $vars);
return __($vars[0], collectionFromQueryString($vars[1] ?? '')->toArray(), $vars[2] ?? null);
}

View File

@@ -2,6 +2,6 @@
declare(strict_types=1);
test('[tr] shortcode', function () {
expect(parsers()->shortcodes()->parse('(tr find:foo)'))->toBe('foo');
test('tr shortcode', function () {
expect(parsers()->shortcodes()->parse('(tr:foo)'))->toBe('foo');
});