| 1 |
<?php |
| 2 |
|
| 3 |
namespace Aws\EndpointV2\Bdd; |
| 4 |
|
| 5 |
use Aws\EndpointV2\Ruleset\RulesetEndpoint; |
| 6 |
use Aws\Exception\UnresolvedEndpointException; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 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 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 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 |
|
| 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 |
|
| 73 |
|
| 74 |
$ref = ($condResult xor $isComplement) |
| 75 |
? $nodes[$base + 1] |
| 76 |
: $nodes[$base + 2]; |
| 77 |
} |
| 78 |
} |
| 79 |
} |