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:
31
src/flextype/core/Parsers/Shortcodes/CalcShortcode.php
Normal file
31
src/flextype/core/Parsers/Shortcodes/CalcShortcode.php
Normal 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()));
|
||||
});
|
@@ -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.
|
||||
|
5
tests/fixtures/settings/settings.yaml
vendored
5
tests/fixtures/settings/settings.yaml
vendored
@@ -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.
|
||||
|
@@ -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');
|
||||
});
|
Reference in New Issue
Block a user