addgod/evaluator

Laravel 包,用于 symfony 表达式语言组件

v1.2.2 2020-01-13 08:30 UTC

This package is auto-updated.

Last update: 2024-09-13 19:25:25 UTC


README

Build Status Scrutinizer Code Quality Coverage Status Latest Stable Version Latest Unstable Version License SensioLabsInsight

一个 Laravel 包和 Orchestra 扩展,用于 symfony/expression-language 组件。

安装

composer require addgod/evaluator

配置

'providers' => [
	Orchestra\Memory\MemoryServiceProvider::class,
];

运行迁移

php artisan migrate

适配器

此包提供 Orchesta Memory 作为默认驱动。

如何使用

评估表达式

$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 Expression Language Component

条件

假设我们想要对我们的收藏夹实现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);