antonio30111988/evaluator

Symfony 表达式语言用于 Laravel

1.3.3 2019-10-02 12:59 UTC

README

Laravel 的 Symfony 表达式语言 模块。

https://github.com/periloso/evaluator 分支而来。

安装

只需更新 composer.json 文件并运行 composer install

"require": {
	"antonio30111988/evaluator": "1.3.*"
}  

快速安装

composer require "antonio30111988/evaluator=1.3.*"

如何使用

评估一个表达式

$test = [
    'foo' => 10,
    'bar' => 5
];

echo Evaluator::evaluate('foo > bar', $test); //this will return true

您还可以保存表达式规则。

$test = [
    'foo' => 10,
    'bar' => 5
];

Evaluator::expression()->add('test', 'foo > bar');

echo Evaluator::evaluateRule('test', $test); //this will return true

有关支持的表达式,请访问 Symfony 表达式语言组件

条件

假设我们想要对我们的集合实现10%的税率。

$item = [
    'price' => 100
];

$condition = [
    'target' => 'price',
    'action' => '10%',
    'rule' => 'price > 50'
];

Evaluator::expression()->add('tax', $condition);

$calculated = Evaluator::condition('tax', $item);

具有乘数的项目。

$item = [
	'price' => 50,
	'quantity' => 2
];

$condition = [
    'target' => 'price',
    'action' => '10%',
    'rule' => 'price > 50',
    'multiplier' => 'quantity'
];

Evaluator::expression()->add('tax', $condition);

$calculated = Evaluator::condition('tax', $item);