mirror of
https://github.com/flextype/flextype.git
synced 2025-08-07 13:46:42 +02:00
feat(shortcodes): add [if]
shortcode
This commit is contained in:
127
src/flextype/core/Parsers/Shortcodes/IfShortcode.php
Normal file
127
src/flextype/core/Parsers/Shortcodes/IfShortcode.php
Normal file
@@ -0,0 +1,127 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Flextype - Hybrid Content Management System with the freedom of a headless CMS
|
||||
* and with the full functionality of a traditional CMS!
|
||||
*
|
||||
* Copyright (c) Sergey Romanenko (https://awilum.github.io)
|
||||
*
|
||||
* Licensed under The MIT License.
|
||||
*
|
||||
* For full copyright and license information, please see the LICENSE
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*/
|
||||
|
||||
namespace Flextype\Parsers\Shortcodes;
|
||||
|
||||
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
|
||||
use Flextype\Entries\Entries;
|
||||
|
||||
use function entries;
|
||||
use function parsers;
|
||||
use function registry;
|
||||
|
||||
// Shortcode: [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() : '';
|
||||
});
|
@@ -605,7 +605,9 @@ parsers:
|
||||
i18n:
|
||||
enabled: true
|
||||
path: "/src/flextype/core/Parsers/Shortcodes/I18nShortcode.php"
|
||||
|
||||
if:
|
||||
enabled: true
|
||||
path: "/src/flextype/core/Parsers/Shortcodes/IfShortcode.php"
|
||||
|
||||
# CORS
|
||||
#
|
||||
|
4
tests/fixtures/settings/settings.yaml
vendored
4
tests/fixtures/settings/settings.yaml
vendored
@@ -594,6 +594,10 @@ parsers:
|
||||
i18n:
|
||||
enabled: true
|
||||
path: "/src/flextype/core/Parsers/Shortcodes/I18nShortcode.php"
|
||||
if:
|
||||
enabled: true
|
||||
path: "/src/flextype/core/Parsers/Shortcodes/IfShortcode.php"
|
||||
|
||||
# CORS
|
||||
#
|
||||
# CORS (Cross-origin resource sharing) allows JavaScript web apps to make HTTP requests to other domains.
|
||||
|
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
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');
|
||||
});
|
Reference in New Issue
Block a user