2017 © Pedro Peláez
 

symfony-bundle rule-engine-bundle

Symfony Rule Engine Bundle

image

cyve/rule-engine-bundle

Symfony Rule Engine Bundle

  • Thursday, May 17, 2018
  • by cyve
  • Repository
  • 1 Watchers
  • 0 Stars
  • 23 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 475 % Grown

The README.md

Rule engine

Installation:

With Composer:, (*1)

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;
    }
}

```php // src/Price/Rule/PromoCodeRule.php, (*2)

namespace App\Price\Rule;, (*3)

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

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

}, (*5)

```php
// src/Price/PriceCalculator.php

namespace App\Price;

class PriceCalculator extends \Cyve\RuleEngine\Engine\RuleEngine
{}

```yaml, (*6)

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, (*7)

```php
// src/Controller/DefaultController.php
...
public function priceAction()
{
    $unitPrice = 100;

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

The Versions

17/05 2018

dev-master

9999999-dev

Symfony Rule Engine Bundle

  Sources   Download

MIT

The Requires

  • php ^7.0

 

rule engine

17/05 2018

1.0

1.0.0.0

Symfony Rule Engine Bundle

  Sources   Download

MIT

The Requires

  • php ^7.0

 

rule engine