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