Licitator 1.0
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

5 years ago
  1. <?php
  2. namespace Zitec\RuleEngineBundle\Service;
  3. use Zitec\RuleEngineBundle\Entity\Rule;
  4. use Symfony\Component\Cache\Adapter\FilesystemAdapter;
  5. use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
  6. use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
  7. /**
  8. * Class RuleEvaluator
  9. * Rule expression evaluator that uses the ExpressionLanguage service to evaluate rules.
  10. */
  11. class RuleEvaluator implements RuleEvaluatorInterface
  12. {
  13. /**
  14. * @var ExpressionLanguage
  15. */
  16. protected $expressionLanguage;
  17. /**
  18. * RuleEvaluator constructor.
  19. */
  20. public function __construct()
  21. {
  22. $this->expressionLanguage = new ExpressionLanguage(new FilesystemAdapter('RuleEngine'));
  23. }
  24. /**
  25. * @param ExpressionFunctionProviderInterface $provider
  26. */
  27. public function addExpressionFunctionProvider(ExpressionFunctionProviderInterface $provider)
  28. {
  29. $this->expressionLanguage->registerProvider($provider);
  30. }
  31. /**
  32. * @param Rule $rule
  33. * @param RuleContextInterface $contextObject
  34. * @return boolean
  35. */
  36. public function evaluate(Rule $rule, RuleContextInterface $contextObject): bool
  37. {
  38. $expression = $rule->getExpression();
  39. $values = [$contextObject->getContextObjectKey() => $contextObject];
  40. return $rule->getActive() && $this->expressionLanguage->evaluate($expression, $values);
  41. }
  42. }