PHP code example of antonforwork / rules

1. Go to this page and download the library: Download antonforwork/rules library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

antonforwork / rules example snippets


$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