此包的最新版本(v1.0)没有可用的许可证信息。

业务规则

v1.0 2021-08-17 16:30 UTC

This package is auto-updated.

Last update: 2024-09-17 23:10:08 UTC


README

安装

通过 composer require antonforwork/rules 安装

使用方法

使用谓词创建规则

$rule1 = new \Rules\Rule(['id'=>1, 'name'=>'cart discount', '...etc']);
$rule1->withPredicate(new \Rules\Predicates\Primitives\Gte('total_amount', 4500));

$rule2 = new \Rules\Rule(['id'=>2, 'name'=>'country based promo', '...etc']);
$rule2->withPredicate(new \Rules\Predicates\Primitives\Equals('country','US'));

// build context
$context = new \Rules\Contexts\ArrayContext([
    'country'=> 'US',
    'total_amount'=> 3000,
]);

$manager = new \Rules\Manager();
$manager
    ->addRule($rule1)
    ->addRule($rule2)
    // ...
    ;
    
$matchedRules = $manager->inspect($context); // will return only 1 item. Rule #2, country based promo 

原始谓词

等于

(new \Rules\Predicates\Primitives\Equals('a', 1));
// Context: a=>1 - true, a=>'1' - true, 'a'=>2 - false

严格等于

(new \Rules\Predicates\Primitives\EqualsStrict('a', 1));
// Context: a=>1 - true, a=>'1' - false, 'a'=>2 - false

大于

(new \Rules\Predicates\Primitives\Gt('a', 1));
// Context: a=>0 - false, a=>1 - false, a=>2 - true

大于等于

(new \Rules\Predicates\Primitives\Gte('a', 1));
// Context: a=>0 - false, a=>1 - true, a=>2 - true

小于

(new \Rules\Predicates\Primitives\Lt('a', 1));
// Context: a=>0 - true, a=>1 - false, a=>2 - false

小于等于

(new \Rules\Predicates\Primitives\Lte('a', 1));
// Context: a=>0 - true, a=>1 - true, a=>2 - false

在范围内

(new \Rules\Predicates\Primitives\InRange('a', 1, 10));
// Context: a=>1 - true, a=>10 - true, a=>20 - false

逻辑谓词

逻辑与

new \Rules\Predicates\Logical\LogicalAnd(
    (new \Rules\Predicates\Primitives\Equals('a', 1))
    (new \Rules\Predicates\Primitives\Equals('b', 2))
    //...
);
// Context:
//  [a=>1, b=>2] - true
//  [a=>1, b=>3] - false
//  [a=>0, b=>2] - false

逻辑或

new \Rules\Predicates\Logical\LogicalOr(
    (new \Rules\Predicates\Primitives\Equals('a', 1))
    (new \Rules\Predicates\Primitives\Equals('b', 2))
    //...
);
// Context:
//  [a=>1, b=>2] - true
//  [a=>1, b=>3] - true
//  [a=>0, b=>2] - true
//  [a=>0, b=>3] - false

逻辑非

new \Rules\Predicates\Logical\LogicalNot(
    new \Rules\Predicates\Logical\LogicalOr(
        (new \Rules\Predicates\Primitives\Equals('a', 1))
        (new \Rules\Predicates\Primitives\Equals('b', 2))
        //...
    )
);
// Context:
//  [a=>1, b=>2] - false
//  [a=>1, b=>3] - false
//  [a=>0, b=>2] - false
//  [a=>0, b=>3] - true