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.

76 lines
1.7 KiB

5 years ago
  1. <?php
  2. namespace Zitec\RuleEngineBundle\Service;
  3. use Zitec\RuleEngineBundle\Conditions\ConditionInterface;
  4. /**
  5. * Class RuleConditionsManager
  6. */
  7. class RuleConditionsManager
  8. {
  9. /**
  10. * @var ConditionInterface[]
  11. */
  12. protected $conditions = [];
  13. /**
  14. * @var RuleContextInterface
  15. */
  16. protected $context;
  17. /**
  18. * RuleConditionsManager constructor.
  19. *
  20. * @param RuleContextInterface $context
  21. */
  22. public function __construct(RuleContextInterface $context)
  23. {
  24. $this->context = $context;
  25. }
  26. /**
  27. * @param ConditionInterface $condition
  28. */
  29. public function addSupportedCondition(ConditionInterface $condition): void
  30. {
  31. $key = $condition->getName();
  32. if (isset($this->conditions[$key])) {
  33. throw new \InvalidArgumentException('A condition with the same machine name was already added!');
  34. }
  35. if (!is_callable([$this->getContext(), $this->getContext()->getMethodName($key)])) {
  36. throw new \InvalidArgumentException(sprintf('Missing getter for %s condition', $key));
  37. }
  38. $this->conditions[$key] = $condition;
  39. }
  40. /**
  41. * @return ConditionInterface[]
  42. */
  43. public function getSupportedConditions(): array
  44. {
  45. return $this->conditions;
  46. }
  47. /**
  48. * @param $name
  49. * @return mixed
  50. */
  51. public function getCondition($name): ConditionInterface
  52. {
  53. if (!isset($this->conditions[$name])) {
  54. throw new \InvalidArgumentException('Condition %s not supported by context.', $name);
  55. }
  56. return $this->conditions[$name];
  57. }
  58. /**
  59. * @return RuleContextInterface
  60. */
  61. public function getContext(): RuleContextInterface
  62. {
  63. return $this->context;
  64. }
  65. }