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