khanhicetea / twigeval
Evaluate expression using Twig
Installs: 2 771
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 0
Open Issues: 0
Requires
- php: >=7.2.5
- twig/twig: ^3.0
Requires (Dev)
- phpunit/phpunit: ^6.0
README
SAFE processing expression of variables from string without using eval
(seems to be evil).
Core
Using twig template engine to produce the result, so you can use any twig syntax and its filters.
Usage
$ composer require khanhicetea/twigeval
/* $cacheDir could be : - false : no use cache (mean use eval function), be carefully ! - null : use sys_get_temp_dir() to get system temp directory as cache dir - string : cache directory path */ $cacheDir = null; $calculator = new KhanhIceTea\Twigeval\Calculator($cacheDir); $math = $calculator->number('a / 4 + b * 3', ['a' => 16, 'b' => 3]); // => 13 $boolean1 = $calculator->isTrue('(a and b) or c', ['a' => false, 'b' => true, 'c' => false]); // => false $boolean2 = $calculator->isFalse('(a and b) or c', ['a' => false, 'b' => true, 'c' => false]); // => true $string = $calculator->calculate('{{ a|reverse }} world !', ['a' => 'hello']); // => olleh world !
LICENSE
The MIT License (MIT) Copyright (c) 2018 KhanhIceTea