| 1 |
<?php |
| 2 |
|
| 3 |
namespace Aws\EndpointV2\Bdd; |
| 4 |
|
| 5 |
use Aws\EndpointV2\Ruleset\RulesetParameter; |
| 6 |
use Aws\EndpointV2\Ruleset\RulesetStandardLibrary; |
| 7 |
use Aws\Exception\UnresolvedEndpointException; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
final class BddRuleset |
| 20 |
{ |
| 21 |
private const REQUIRED_FIELDS = [ |
| 22 |
'conditions', 'results', 'nodes', 'root', 'nodeCount' |
| 23 |
]; |
| 24 |
|
| 25 |
|
| 26 |
private array $parameters; |
| 27 |
|
| 28 |
|
| 29 |
private array $conditions; |
| 30 |
|
| 31 |
|
| 32 |
private array $results; |
| 33 |
|
| 34 |
|
| 35 |
private array $nodes; |
| 36 |
|
| 37 |
private int $root; |
| 38 |
|
| 39 |
public readonly RulesetStandardLibrary $standardLibrary; |
| 40 |
|
| 41 |
public function __construct(array $definition, array $partitions) |
| 42 |
{ |
| 43 |
foreach (self::REQUIRED_FIELDS as $key) { |
| 44 |
if (!array_key_exists($key, $definition)) { |
| 45 |
throw new UnresolvedEndpointException( |
| 46 |
"Endpoint BDD definition is missing `{$key}`." |
| 47 |
); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
$this->parameters = $this->buildParameters($definition['parameters'] ?? []); |
| 52 |
$this->conditions = $definition['conditions']; |
| 53 |
$this->results = $definition['results']; |
| 54 |
$this->root = (int) $definition['root']; |
| 55 |
$nodeCount = $definition['nodeCount']; |
| 56 |
$this->nodes = BddNodeDecoder::decode( |
| 57 |
(string) $definition['nodes'], |
| 58 |
$nodeCount |
| 59 |
); |
| 60 |
|
| 61 |
$this->standardLibrary = new RulesetStandardLibrary($partitions); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public function getParameters(): array |
| 68 |
{ |
| 69 |
return $this->parameters; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public function getConditions(): array |
| 76 |
{ |
| 77 |
return $this->conditions; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public function getResults(): array |
| 84 |
{ |
| 85 |
return $this->results; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public function getNodes(): array |
| 92 |
{ |
| 93 |
return $this->nodes; |
| 94 |
} |
| 95 |
|
| 96 |
public function getRoot(): int |
| 97 |
{ |
| 98 |
return $this->root; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public function applyParameterDefaults(array &$inputParameters): void |
| 107 |
{ |
| 108 |
foreach ($this->parameters as $name => $param) { |
| 109 |
$value = $inputParameters[$name] ?? null; |
| 110 |
|
| 111 |
if (is_null($value) && !is_null($param->getDefault())) { |
| 112 |
$inputParameters[$name] = $param->getDefault(); |
| 113 |
} elseif (!is_null($value)) { |
| 114 |
$param->validateInputParam($value); |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
private function buildParameters(array $parameters): array |
| 120 |
{ |
| 121 |
$built = []; |
| 122 |
foreach ($parameters as $name => $definition) { |
| 123 |
$built[$name] = new RulesetParameter($name, $definition); |
| 124 |
} |
| 125 |
return $built; |
| 126 |
} |
| 127 |
} |