RulesetParameter.php 3.5 KB

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