Skip to content

cyve/rule-engine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rule engine

Installation:

With Composer:

composer require cyve/rule-engine

Usage

use Cyve\RuleEngine\Engine\RuleEngine;
use Cyve\RuleEngine\Rule\ExpressionRule;

$engine = new RuleEngine([new ExpressionRule('subject * context["quantity"]')]);
$price = $engine->handle(100, ['quantity' => 2]); // 200

Use case: price calculator (for Symfony)

// src/Price/Rule/QuantityRule.php

namespace App\Price\Rule;

class QuantityRule
{
    public function __invoke($subject, array $context = [])
    {
        return $subject * $context['quantity'] ?? 1;
    }
}
// src/Price/Rule/PromoCodeRule.php

namespace App\Price\Rule;

class PromoCodeRule implements \Cyve\RuleEngine\Rule\RuleInterface
{
    public function supports($subject, array $context = []): bool
    {
        return isset($context['promoCode']);
    }

    public function handle($subject, array $context = [])
    {
        switch($context['promoCode']){
            case '10_PERCENT': return $subject * .9;
            default: return $subject;
        }
    }
}
// src/Price/PriceCalculator.php

namespace App\Price;

class PriceCalculator extends \Cyve\RuleEngine\Engine\RuleEngine
{}
# config/service.yml
# @see https://symfony.com/doc/4.4/service_container/tags.html#reference-tagged-services
App\Price\Rule\:
    resource: '../src/Price/Rule/*'
    tags: ['price.rule']
App\Price\PriceCalculator:
    public: true
    arguments:
        - !tagged_iterator price.rule
// src/Controller/DefaultController.php
...
public function priceAction()
{
    $unitPrice = 100;

    $calculator = $this->get(PriceCalculator::class);
    $price = $calculator->handle($unitPrice, ['quantity' => 2, 'promoCode' => '10_PERCENT'])); // 180
}
...

About

Rule Engine service

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages