You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.4 KiB
49 lines
1.4 KiB
<?php
|
|
|
|
namespace Zitec\RuleEngineBundle\Service;
|
|
|
|
use Zitec\RuleEngineBundle\Entity\Rule;
|
|
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
|
|
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
|
|
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
|
|
|
|
/**
|
|
* Class RuleEvaluator
|
|
* Rule expression evaluator that uses the ExpressionLanguage service to evaluate rules.
|
|
*/
|
|
class RuleEvaluator implements RuleEvaluatorInterface
|
|
{
|
|
/**
|
|
* @var ExpressionLanguage
|
|
*/
|
|
protected $expressionLanguage;
|
|
|
|
/**
|
|
* RuleEvaluator constructor.
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->expressionLanguage = new ExpressionLanguage(new FilesystemAdapter('RuleEngine'));
|
|
}
|
|
|
|
/**
|
|
* @param ExpressionFunctionProviderInterface $provider
|
|
*/
|
|
public function addExpressionFunctionProvider(ExpressionFunctionProviderInterface $provider)
|
|
{
|
|
$this->expressionLanguage->registerProvider($provider);
|
|
}
|
|
|
|
/**
|
|
* @param Rule $rule
|
|
* @param RuleContextInterface $contextObject
|
|
* @return boolean
|
|
*/
|
|
public function evaluate(Rule $rule, RuleContextInterface $contextObject): bool
|
|
{
|
|
$expression = $rule->getExpression();
|
|
$values = [$contextObject->getContextObjectKey() => $contextObject];
|
|
|
|
return $rule->getActive() && $this->expressionLanguage->evaluate($expression, $values);
|
|
}
|
|
}
|