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

feat(shortcodes): update logic for if shortcode, use expression service

This commit is contained in:
Awilum
2022-05-27 08:59:04 +03:00
parent 115c1c1095
commit bc5477bd57
2 changed files with 8 additions and 119 deletions

View File

@@ -17,109 +17,17 @@ declare(strict_types=1);
namespace Flextype\Parsers\Shortcodes;
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use function parsers;
use function registry;
// Shortcode: [if]
// Shortcode: if
// Usage: (if:'(var:score) < (var:level1)') Show something... (/if)
parsers()->shortcodes()->addHandler('if', static function (ShortcodeInterface $s) {
if (! registry()->get('flextype.settings.parsers.shortcodes.shortcodes.if.enabled')) {
return '';
}
$prepareValue = function($value) {
if(strings($value)->isInteger()) {
$value = strings($value)->toInteger();
} elseif(strings($value)->isFloat()) {
$value = strings($value)->toFloat();
} elseif(strings($value)->isBoolean()) {
$value = strings($value)->toBoolean();
} elseif(strings($value)->isNull()) {
$value = strings($value)->toNull();
} else {
$value = (string) $value;
}
return $value;
};
$result = false;
$operator = $s->getParameter('operator');
$val1 = $prepareValue($s->getParameter('val1'));
$val2 = $prepareValue($s->getParameter('val2'));
$encoding = $s->getParameter('encoding') ? $s->getParameter('encoding') : 'utf-8';
switch ($operator) {
case 'lt':
case '<':
$result = (bool) ($val1 < $val2);
break;
case 'gt':
case '>':
$result = (bool) ($val1 > $val2);
break;
case 'lte':
case '<=':
$result = (bool) ($val1 <= $val2);
break;
case 'gte':
case '>=':
$result = (bool) ($val1 >= $val2);
break;
case 'eq':
case '=':
$result = (bool) ($val1 === $val2);
break;
case 'neq':
case '<>':
case '!=':
$result = (bool) ($val1 !== $val2);
break;
case 'contains':
case 'like':
$result = (bool) (mb_strpos((string) $val1, (string) $val2, 0, $encoding) !== false);
break;
case 'ncontains':
case 'nlike':
$result = (bool) (mb_strpos((string) $val1, (string) $val2, 0, $encoding) === false);
break;
case 'starts_with':
$result = (bool) (strncmp((string) $val1, (string) $val2, mb_strlen((string) $val2)) === 0);
break;
case 'ends_with':
$result = (bool) (mb_substr((string) $val1, -mb_strlen((string) $val2), null, $encoding) === $val2);
break;
case 'newer':
$result = (bool) (strtotime((string) $val1) > strtotime((string) $val2));
break;
case 'older':
$result = (bool) (strtotime((string) $val1) < strtotime((string) $val2));
break;
case 'regexp':
$val2 = (string) $val2;
$result = (bool) (preg_match("/{$val2}/ium", (string) $val1));
break;
case 'nregexp':
$val2 = (string) $val2;
$result = (bool) (! preg_match("/{$val2}/ium", (string) $val1));
break;
default:
$result = false;
break;
}
return $result ? $s->getContent() : '';
return expression()->evaluate(parsers()->shortcodes()->parse((($s->getBbCode() != null) ? $s->getBbCode() : ''))) === true ? parsers()->shortcodes()->parse($s->getContent()) : '';
});

View File

@@ -2,27 +2,8 @@
declare(strict_types=1);
test('(if] shortcode', function () {
expect(parsers()->shortcodes()->parse("(if val1:'2' operator:'lt' val2:'5')yes(/if)"))->toBe("yes");
expect(parsers()->shortcodes()->parse("(if val1:'2' operator:'<' val2:'5')yes(/if)"))->toBe("yes");
expect(parsers()->shortcodes()->parse("(if val1:'5' operator:'gt' val2:'2')yes(/if)"))->toBe("yes");
expect(parsers()->shortcodes()->parse("(if val1:'2' operator:'lte' val2:'5')yes(/if)"))->toBe("yes");
expect(parsers()->shortcodes()->parse("(if val1:'2' operator:'<=' val2:'5')yes(/if)"))->toBe("yes");
expect(parsers()->shortcodes()->parse("(if val1:'5' operator:'gte' val2:'2')yes(/if)"))->toBe("yes");
expect(parsers()->shortcodes()->parse("(if val1:'5' operator:'>=' val2:'2')yes(/if)"))->toBe("yes");
expect(parsers()->shortcodes()->parse("(if val1:'5' operator:'eq' val2:'5')yes(/if)"))->toBe("yes");
expect(parsers()->shortcodes()->parse("(if val1:'5' operator:'=' val2:'5')yes(/if)"))->toBe("yes");
expect(parsers()->shortcodes()->parse("(if val1:'2' operator:'neq' val2:'5')yes(/if)"))->toBe("yes");
expect(parsers()->shortcodes()->parse("(if val1:'2' operator:'<>' val2:'5')yes(/if)"))->toBe("yes");
expect(parsers()->shortcodes()->parse("(if val1:'2' operator:'!=' val2:'5')yes(/if)"))->toBe("yes");
expect(parsers()->shortcodes()->parse("(if val1:'foobarfoo' operator:'contains' val2:'bar')yes(/if)"))->toBe("yes");
expect(parsers()->shortcodes()->parse("(if val1:'foobarfoo' operator:'like' val2:'bar')yes(/if)"))->toBe("yes");
expect(parsers()->shortcodes()->parse("(if val1:'foobarfoo' operator:'ncontains' val2:'zed')yes(/if)"))->toBe("yes");
expect(parsers()->shortcodes()->parse("(if val1:'foobarfoo' operator:'nlike' val2:'zed')yes(/if)"))->toBe("yes");
expect(parsers()->shortcodes()->parse("(if val1:'foobar' operator:'starts_with' val2:'foo')yes(/if)"))->toBe("yes");
expect(parsers()->shortcodes()->parse("(if val1:'foobar' operator:'ends_with' val2:'bar')yes(/if)"))->toBe("yes");
expect(parsers()->shortcodes()->parse("(if val1:'2020-11-12' operator:'newer' val2:'2020-11-11')yes(/if)"))->toBe("yes");
expect(parsers()->shortcodes()->parse("(if val1:'2020-11-11' operator:'older' val2:'2020-11-12')yes(/if)"))->toBe("yes");
expect(parsers()->shortcodes()->parse("(if val1:'PHP is the web scripting language of choice.' operator:'regexp' val2:'php')yes(/if)"))->toBe("yes");
expect(parsers()->shortcodes()->parse("(if val1:'PHP is the web scripting language of choice.' operator:'nregexp' val2:'delphi')yes(/if)"))->toBe("yes");
test('if shortcode', function () {
expect(parsers()->shortcodes()->parse("(if:'2 > 1')yes(/if)"))->toBe("yes");
expect(parsers()->shortcodes()->parse("(if:'2>1')yes(/if)"))->toBe("yes");
expect(parsers()->shortcodes()->parse("(if:'2<1')yes(/if)"))->toBe("");
});