1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-07 05:36:54 +02:00

feat(shortcodes): add calc shortcode

This commit is contained in:
Awilum
2022-05-25 19:21:43 +03:00
parent a60131607e
commit 39ff94f8a1
4 changed files with 55 additions and 2 deletions

View File

@@ -0,0 +1,31 @@
<?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 ChrisKonnertz\StringCalc\StringCalc;
use function registry;
// Shortcode: calc
// Usage: (calc:2+2)
parsers()->shortcodes()->addHandler('calc', static function (ShortcodeInterface $s) {
if (! registry()->get('flextype.settings.parsers.shortcodes.shortcodes.calc.enabled')) {
return '';
}
return (new StringCalc())->calculate(parsers()->shortcodes()->parse($s->getBBCode()));
});

View File

@@ -631,7 +631,10 @@ parsers:
field:
enabled: true
path: "/src/flextype/core/Parsers/Shortcodes/FieldShortcode.php"
calc:
enabled: true
path: "/src/flextype/core/Parsers/Shortcodes/CalcShortcode.php"
# CORS
#
# CORS (Cross-origin resource sharing) allows JavaScript web apps to make HTTP requests to other domains.

View File

@@ -612,7 +612,10 @@ parsers:
field:
enabled: true
path: "/src/flextype/core/Parsers/Shortcodes/FieldShortcode.php"
calc:
enabled: true
path: "/src/flextype/core/Parsers/Shortcodes/CalcShortcode.php"
# CORS
#
# CORS (Cross-origin resource sharing) allows JavaScript web apps to make HTTP requests to other domains.

View File

@@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
beforeEach(function() {
filesystem()->directory(PATH['project'] . '/entries')->ensureExists(0755, true);
});
afterEach(function () {
filesystem()->directory(PATH['project'] . '/entries')->delete();
});
test('calc shortcode', function () {
expect(entries()->create('foo', ['price' => '(calc:2+2)']))->toBeTrue();
expect(entries()->fetch('foo')['price'])->toBe('4');
});