Debian Codegraph @

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

raw symbols SCIP
1 <?php
2
3 namespace Aws\EndpointV2\Bdd;
4
5 use Aws\EndpointV2\Ruleset\RulesetEndpoint;
6 use Aws\Exception\UnresolvedEndpointException;
7
8 /**
9 * Walks an endpoint BDD to produce a {@see RulesetEndpoint} or throw an
10 * {@see UnresolvedEndpointException}.
11 *
12 * The traversal follows the smithy rules-engine reference algorithm:
13 * each reference is either a node pointer (optionally complemented with a
14 * negative sign), one of the two terminals (`1` / `-1`), or a result pointer
15 * (offset by {@see self::RESULT_OFFSET}). Nodes are laid out as contiguous
16 * triples `[conditionIndex, highRef, lowRef]` inside the ruleset's flat
17 * node array.
18 *
19 * @internal
20 */
21 final class BddEvaluator
22 {
23 private const TERMINAL_TRUE = 1;
24 private const TERMINAL_FALSE = -1;
25 private const RESULT_OFFSET = 100_000_000;
26
27 public function __construct(
28 private readonly BddRuleset $ruleset,
29 private readonly BddResultResolver $resultResolver
30 ) {
31 }
32
33 /**
34 * Resolves an endpoint from the BDD for the given input parameters.
35 *
36 * @throws UnresolvedEndpointException when resolution reaches the no-match
37 * terminal or an error result rule.
38 */
39 public function evaluate(array $inputParameters): RulesetEndpoint
40 {
41 $this->ruleset->applyParameterDefaults($inputParameters);
42
43 $nodes = $this->ruleset->getNodes();
44 $conditions = $this->ruleset->getConditions();
45 $library = $this->ruleset->standardLibrary;
46
47 $ref = $this->ruleset->getRoot();
48
49 while (true) {
50 if ($ref >= self::RESULT_OFFSET) {
51 return $this->resultResolver->resolve(
52 $ref - self::RESULT_OFFSET,
53 $inputParameters
54 );
55 }
56
57 if ($ref === self::TERMINAL_TRUE || $ref === self::TERMINAL_FALSE) {
58 // Throws exception
59 $this->resultResolver->resolveNoMatch($inputParameters);
60 }
61
62 $isComplement = $ref < 0;
63 $base = ($isComplement ? -$ref : $ref) * 3 - 3;
64
65 $condIndex = $nodes[$base];
66 $value = $library->callFunction(
67 $conditions[$condIndex],
68 $inputParameters
69 );
70
71 $condResult = ($value !== null && $value !== false);
72 // Complement edges invert the high/low selection without
73 // duplicating nodes in the BDD.
74 $ref = ($condResult xor $isComplement)
75 ? $nodes[$base + 1]
76 : $nodes[$base + 2];
77 }
78 }
79 }