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