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