Debian Codegraph @

lib/3rdParty/composer/aws/aws-sdk-php/src/EndpointV2/Bdd/BddRuleset.php

raw symbols SCIP
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 * Parsed form of the `smithy.rules#endpointBdd` trait. Reuses
11 * {@see RulesetParameter} so parameter coercion and validation behave
12 * identically to the tree-based ruleset.
13 *
14 * Instances are immutable after construction. A single instance is shared
15 * across all requests for a given service/client pair.
16 *
17 * @internal
18 */
19 final class BddRuleset
20 {
21 private const REQUIRED_FIELDS = [
22 'conditions', 'results', 'nodes', 'root', 'nodeCount'
23 ];
24
25 /** @var array<string, RulesetParameter> */
26 private array $parameters;
27
28 /** @var array<int, array> */
29 private array $conditions;
30
31 /** @var array<int, array> */
32 private array $results;
33
34 /** @var int[] Flat triples: [condIdx, hi, lo, condIdx, hi, lo, ...] */
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 * @return array<string, RulesetParameter>
66 */
67 public function getParameters(): array
68 {
69 return $this->parameters;
70 }
71
72 /**
73 * @return array<int, array>
74 */
75 public function getConditions(): array
76 {
77 return $this->conditions;
78 }
79
80 /**
81 * @return array<int, array>
82 */
83 public function getResults(): array
84 {
85 return $this->results;
86 }
87
88 /**
89 * @return int[]
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 * Applies parameter defaults and type checks. Mirrors the tree ruleset so
103 * services migrating from one shape to the other see identical input
104 * validation behavior.
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 }