TreeRule.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace Aws\EndpointV2\Rule;
  3. use Aws\EndpointV2\Ruleset\RulesetStandardLibrary;
  4. class TreeRule extends AbstractRule
  5. {
  6. /** @var array */
  7. private $rules;
  8. public function __construct(array $definition)
  9. {
  10. parent::__construct($definition);
  11. $this->rules = $this->createRules($definition['rules']);
  12. }
  13. /**
  14. * @return array
  15. */
  16. public function getRules()
  17. {
  18. return $this->rules;
  19. }
  20. /**
  21. * If a tree rule's conditions evaluate successfully, iterate over its
  22. * subordinate rules and return a result if there is one. If any of the
  23. * subsequent rules are trees, the function will recurse until it reaches
  24. * an error or an endpoint rule
  25. *
  26. * @return mixed
  27. */
  28. public function evaluate(
  29. array $inputParameters,
  30. RulesetStandardLibrary $standardLibrary
  31. )
  32. {
  33. if ($this->evaluateConditions($inputParameters, $standardLibrary)) {
  34. foreach($this->rules as $rule) {
  35. $inputParametersCopy = $inputParameters;
  36. $evaluation = $rule->evaluate($inputParametersCopy, $standardLibrary);
  37. if ($evaluation !== false) {
  38. return $evaluation;
  39. }
  40. }
  41. }
  42. return false;
  43. }
  44. private function createRules(array $rules)
  45. {
  46. $rulesList = [];
  47. forEach($rules as $rule) {
  48. $ruleType = RuleCreator::create($rule['type'], $rule);
  49. $rulesList[] = $ruleType;
  50. }
  51. return $rulesList;
  52. }
  53. }