| 1 |
<?php |
| 2 |
|
| 3 |
namespace Aws\EndpointV2\Ruleset; |
| 4 |
|
| 5 |
use Aws\Exception\UnresolvedEndpointException; |
| 6 |
use function \Aws\is_associative; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
class RulesetParameter |
| 12 |
{ |
| 13 |
|
| 14 |
private $name; |
| 15 |
|
| 16 |
|
| 17 |
private $type; |
| 18 |
|
| 19 |
|
| 20 |
private $builtIn; |
| 21 |
|
| 22 |
|
| 23 |
private $default; |
| 24 |
|
| 25 |
|
| 26 |
private $required; |
| 27 |
|
| 28 |
|
| 29 |
private $documentation; |
| 30 |
|
| 31 |
|
| 32 |
private $deprecated; |
| 33 |
|
| 34 |
|
| 35 |
private static $typeMap = [ |
| 36 |
'String' => 'is_string', |
| 37 |
'Boolean' => 'is_bool', |
| 38 |
'StringArray' => 'isStringArray' |
| 39 |
]; |
| 40 |
|
| 41 |
public function __construct($name, array $definition) |
| 42 |
{ |
| 43 |
$type = ucfirst($definition['type']); |
| 44 |
if ($this->isValidType($type)) { |
| 45 |
$this->type = $type; |
| 46 |
} else { |
| 47 |
throw new UnresolvedEndpointException( |
| 48 |
'Unknown parameter type ' . "`{$type}`" . |
| 49 |
'. Parameters must be of type `String`, `Boolean` or `StringArray.' |
| 50 |
); |
| 51 |
} |
| 52 |
|
| 53 |
$this->name = $name; |
| 54 |
$this->builtIn = $definition['builtIn'] ?? null; |
| 55 |
$this->default = $definition['default'] ?? null; |
| 56 |
$this->required = $definition['required'] ?? false; |
| 57 |
$this->documentation = $definition['documentation'] ?? null; |
| 58 |
$this->deprecated = $definition['deprecated'] ?? false; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public function getName() |
| 65 |
{ |
| 66 |
return $this->name; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public function getType() |
| 73 |
{ |
| 74 |
return $this->type; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public function getBuiltIn() |
| 81 |
{ |
| 82 |
return $this->builtIn; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public function getDefault() |
| 89 |
{ |
| 90 |
return $this->default; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public function getRequired() |
| 97 |
{ |
| 98 |
return $this->required; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public function getDocumentation() |
| 105 |
{ |
| 106 |
return $this->documentation; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public function getDeprecated() |
| 113 |
{ |
| 114 |
return $this->deprecated; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
public function validateInputParam($inputParam) |
| 124 |
{ |
| 125 |
if (!$this->isValidInput($inputParam)) { |
| 126 |
throw new UnresolvedEndpointException( |
| 127 |
"Input parameter `{$this->name}` is the wrong type. Must be a {$this->type}." |
| 128 |
); |
| 129 |
} |
| 130 |
|
| 131 |
if ($this->deprecated) { |
| 132 |
$deprecated = $this->deprecated; |
| 133 |
$deprecationString = "{$this->name} has been deprecated "; |
| 134 |
$msg = $deprecated['message'] ?? null; |
| 135 |
$since = $deprecated['since'] ?? null; |
| 136 |
|
| 137 |
if (!is_null($since)){ |
| 138 |
$deprecationString .= 'since ' . $since . '. '; |
| 139 |
} |
| 140 |
if (!is_null($msg)) { |
| 141 |
$deprecationString .= $msg; |
| 142 |
} |
| 143 |
|
| 144 |
trigger_error($deprecationString, E_USER_WARNING); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
private function isValidType($type) |
| 149 |
{ |
| 150 |
return isset(self::$typeMap[$type]); |
| 151 |
} |
| 152 |
|
| 153 |
private function isValidInput($inputParam): bool |
| 154 |
{ |
| 155 |
$method = self::$typeMap[$this->type]; |
| 156 |
if (is_callable($method)) { |
| 157 |
return $method($inputParam); |
| 158 |
} elseif (method_exists($this, $method)) { |
| 159 |
return $this->$method($inputParam); |
| 160 |
} |
| 161 |
|
| 162 |
return false; |
| 163 |
} |
| 164 |
|
| 165 |
private function isStringArray(array $array): bool |
| 166 |
{ |
| 167 |
if (is_associative($array)) { |
| 168 |
return false; |
| 169 |
} |
| 170 |
|
| 171 |
foreach($array as $value) { |
| 172 |
if (!is_string($value)) { |
| 173 |
return false; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
return true; |
| 178 |
} |
| 179 |
} |