| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dudlewebs\WPMCS\s3\Aws\EndpointV2\Rule; |
| 4 |
|
| 5 |
use Dudlewebs\WPMCS\s3\Aws\EndpointV2\Ruleset\RulesetStandardLibrary; |
| 6 |
use Dudlewebs\WPMCS\s3\Aws\EndpointV2\Ruleset\RulesetEndpoint; |
| 7 |
class EndpointRule extends AbstractRule |
| 8 |
{ |
| 9 |
/** @var array */ |
| 10 |
private $endpoint; |
| 11 |
public function __construct(array $definition) |
| 12 |
{ |
| 13 |
parent::__construct($definition); |
| 14 |
$this->endpoint = $definition['endpoint']; |
| 15 |
} |
| 16 |
/** |
| 17 |
* @return array |
| 18 |
*/ |
| 19 |
public function getEndpoint() |
| 20 |
{ |
| 21 |
return $this->endpoint; |
| 22 |
} |
| 23 |
/** |
| 24 |
* If all the rule's conditions are met, return the resolved |
| 25 |
* endpoint object. |
| 26 |
* |
| 27 |
* @return RulesetEndpoint | null |
| 28 |
*/ |
| 29 |
public function evaluate(array $inputParameters, RulesetStandardLibrary $standardLibrary) |
| 30 |
{ |
| 31 |
if ($this->evaluateConditions($inputParameters, $standardLibrary)) { |
| 32 |
return $this->resolve($inputParameters, $standardLibrary); |
| 33 |
} |
| 34 |
return \false; |
| 35 |
} |
| 36 |
/** |
| 37 |
* Given input parameters, resolve an endpoint in its entirety. |
| 38 |
* |
| 39 |
* @return RulesetEndpoint |
| 40 |
*/ |
| 41 |
private function resolve(array $inputParameters, RulesetStandardLibrary $standardLibrary) |
| 42 |
{ |
| 43 |
$uri = $standardLibrary->resolveValue($this->endpoint['url'], $inputParameters); |
| 44 |
$properties = isset($this->endpoint['properties']) ? $this->resolveProperties($this->endpoint['properties'], $inputParameters, $standardLibrary) : null; |
| 45 |
$headers = $this->resolveHeaders($inputParameters, $standardLibrary); |
| 46 |
return new RulesetEndpoint($uri, $properties, $headers); |
| 47 |
} |
| 48 |
/** |
| 49 |
* Recurse through an endpoint's `properties` attribute, resolving template |
| 50 |
* strings when found. Return the fully resolved attribute. |
| 51 |
* |
| 52 |
* @return array |
| 53 |
*/ |
| 54 |
private function resolveProperties($properties, array $inputParameters, RulesetStandardLibrary $standardLibrary) |
| 55 |
{ |
| 56 |
if (\is_array($properties)) { |
| 57 |
$propertiesArr = []; |
| 58 |
foreach ($properties as $key => $val) { |
| 59 |
$propertiesArr[$key] = $this->resolveProperties($val, $inputParameters, $standardLibrary); |
| 60 |
} |
| 61 |
return $propertiesArr; |
| 62 |
} elseif ($standardLibrary->isTemplate($properties)) { |
| 63 |
return $standardLibrary->resolveTemplateString($properties, $inputParameters); |
| 64 |
} |
| 65 |
return $properties; |
| 66 |
} |
| 67 |
/** |
| 68 |
* If present, iterate through an endpoint's headers attribute resolving |
| 69 |
* values along the way. Return the fully resolved attribute. |
| 70 |
* |
| 71 |
* @return array |
| 72 |
*/ |
| 73 |
private function resolveHeaders(array $inputParameters, RulesetStandardLibrary $standardLibrary) |
| 74 |
{ |
| 75 |
$headers = isset($this->endpoint['headers']) ? $this->endpoint['headers'] : null; |
| 76 |
if (\is_null($headers)) { |
| 77 |
return null; |
| 78 |
} |
| 79 |
$resolvedHeaders = []; |
| 80 |
foreach ($headers as $headerName => $headerValues) { |
| 81 |
$resolvedValues = []; |
| 82 |
foreach ($headerValues as $value) { |
| 83 |
$resolvedValue = $standardLibrary->resolveValue($value, $inputParameters, $standardLibrary); |
| 84 |
$resolvedValues[] = $resolvedValue; |
| 85 |
} |
| 86 |
$resolvedHeaders[$headerName] = $resolvedValues; |
| 87 |
} |
| 88 |
return $resolvedHeaders; |
| 89 |
} |
| 90 |
} |
| 91 |
|