Ruleset.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace Aws\EndpointV2\Ruleset;
  3. use Aws\EndpointV2\Rule\RuleCreator;
  4. /**
  5. * A collection of rules, parameter definitions and a class of helper functions
  6. * used to resolve either an endpoint or an error.
  7. */
  8. class Ruleset
  9. {
  10. /** @var string */
  11. private $version;
  12. /** @var array */
  13. private $parameters;
  14. /** @var array */
  15. private $rules;
  16. /** @var RulesetStandardLibrary */
  17. public $standardLibrary;
  18. public function __construct(array $ruleset, array $partitions)
  19. {
  20. $this->version = $ruleset['version'];
  21. $this->parameters = $this->createParameters($ruleset['parameters']);
  22. $this->rules = $this->createRules($ruleset['rules']);
  23. $this->standardLibrary = new RulesetStandardLibrary($partitions);
  24. }
  25. /**
  26. * @return mixed
  27. */
  28. public function getVersion()
  29. {
  30. return $this->version;
  31. }
  32. /**
  33. * @return array
  34. */
  35. public function getParameters()
  36. {
  37. return $this->parameters;
  38. }
  39. /**
  40. * @return array
  41. */
  42. public function getRules()
  43. {
  44. return $this->rules;
  45. }
  46. /**
  47. * Evaluate the ruleset against the input parameters.
  48. * Return the first rule the parameters match against.
  49. *
  50. * @return mixed
  51. */
  52. public function evaluate(array $inputParameters)
  53. {
  54. $this->validateInputParameters($inputParameters);
  55. foreach($this->rules as $rule) {
  56. $evaluation = $rule->evaluate($inputParameters, $this->standardLibrary);
  57. if ($evaluation !== false) {
  58. return $evaluation;
  59. }
  60. }
  61. return false;
  62. }
  63. /**
  64. * Ensures all corresponding client-provided parameters match
  65. * the Ruleset parameter's specified type.
  66. *
  67. * @return void
  68. */
  69. private function validateInputParameters(array &$inputParameters)
  70. {
  71. foreach($this->parameters as $paramName => $param) {
  72. $inputParam = isset($inputParameters[$paramName]) ? $inputParameters[$paramName] : null;
  73. if (is_null($inputParam) && !is_null($param->getDefault())) {
  74. $inputParameters[$paramName] = $param->getDefault();
  75. } elseif (!is_null($inputParam)) {
  76. $param->validateInputParam($inputParam);
  77. }
  78. }
  79. }
  80. private function createParameters(array $parameters)
  81. {
  82. $parameterList = [];
  83. foreach($parameters as $name => $definition) {
  84. $parameterList[$name] = new RulesetParameter($name, $definition);
  85. }
  86. return $parameterList;
  87. }
  88. private function createRules(array $rules)
  89. {
  90. $rulesList = [];
  91. forEach($rules as $rule) {
  92. $ruleObj = RuleCreator::create($rule['type'], $rule);
  93. $rulesList[] = $ruleObj;
  94. }
  95. return $rulesList;
  96. }
  97. }