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