1. Go to this page and download the library: Download cyve/rule-engine 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/ */
cyve / rule-engine example snippets
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
// 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
{}