| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dudlewebs\WPMCS\s3\Aws\EndpointV2\Rule; |
| 4 |
|
| 5 |
use Dudlewebs\WPMCS\s3\Aws\EndpointV2\Ruleset\RulesetStandardLibrary; |
| 6 |
class TreeRule extends AbstractRule |
| 7 |
{ |
| 8 |
/** @var array */ |
| 9 |
private $rules; |
| 10 |
public function __construct(array $definition) |
| 11 |
{ |
| 12 |
parent::__construct($definition); |
| 13 |
$this->rules = $this->createRules($definition['rules']); |
| 14 |
} |
| 15 |
/** |
| 16 |
* @return array |
| 17 |
*/ |
| 18 |
public function getRules() |
| 19 |
{ |
| 20 |
return $this->rules; |
| 21 |
} |
| 22 |
/** |
| 23 |
* If a tree rule's conditions evaluate successfully, iterate over its |
| 24 |
* subordinate rules and return a result if there is one. If any of the |
| 25 |
* subsequent rules are trees, the function will recurse until it reaches |
| 26 |
* an error or an endpoint rule |
| 27 |
* |
| 28 |
* @return mixed |
| 29 |
*/ |
| 30 |
public function evaluate(array $inputParameters, RulesetStandardLibrary $standardLibrary) |
| 31 |
{ |
| 32 |
if ($this->evaluateConditions($inputParameters, $standardLibrary)) { |
| 33 |
foreach ($this->rules as $rule) { |
| 34 |
$inputParametersCopy = $inputParameters; |
| 35 |
$evaluation = $rule->evaluate($inputParametersCopy, $standardLibrary); |
| 36 |
if ($evaluation !== \false) { |
| 37 |
return $evaluation; |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
return \false; |
| 42 |
} |
| 43 |
private function createRules(array $rules) |
| 44 |
{ |
| 45 |
$rulesList = []; |
| 46 |
foreach ($rules as $rule) { |
| 47 |
$ruleType = RuleCreator::create($rule['type'], $rule); |
| 48 |
$rulesList[] = $ruleType; |
| 49 |
} |
| 50 |
return $rulesList; |
| 51 |
} |
| 52 |
} |
| 53 |
|