Arn.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace Aws\Arn;
  3. use Aws\Arn\Exception\InvalidArnException;
  4. /**
  5. * Amazon Resource Names (ARNs) uniquely identify AWS resources. The Arn class
  6. * parses and stores a generic ARN object representation that can apply to any
  7. * service resource.
  8. *
  9. * @internal
  10. */
  11. class Arn implements ArnInterface
  12. {
  13. protected $data;
  14. protected $string;
  15. public static function parse($string)
  16. {
  17. $data = [
  18. 'arn' => null,
  19. 'partition' => null,
  20. 'service' => null,
  21. 'region' => null,
  22. 'account_id' => null,
  23. 'resource' => null,
  24. ];
  25. $length = strlen($string);
  26. $lastDelim = 0;
  27. $numComponents = 0;
  28. for ($i = 0; $i < $length; $i++) {
  29. if (($numComponents < 5 && $string[$i] === ':')) {
  30. // Split components between delimiters
  31. $data[key($data)] = substr($string, $lastDelim, $i - $lastDelim);
  32. // Do not include delimiter character itself
  33. $lastDelim = $i + 1;
  34. next($data);
  35. $numComponents++;
  36. }
  37. if ($i === $length - 1) {
  38. // Put the remainder in the last component.
  39. if (in_array($numComponents, [5])) {
  40. $data['resource'] = substr($string, $lastDelim);
  41. } else {
  42. // If there are < 5 components, put remainder in current
  43. // component.
  44. $data[key($data)] = substr($string, $lastDelim);
  45. }
  46. }
  47. }
  48. return $data;
  49. }
  50. public function __construct($data)
  51. {
  52. if (is_array($data)) {
  53. $this->data = $data;
  54. } elseif (is_string($data)) {
  55. $this->data = static::parse($data);
  56. } else {
  57. throw new InvalidArnException('Constructor accepts a string or an'
  58. . ' array as an argument.');
  59. }
  60. static::validate($this->data);
  61. }
  62. public function __toString()
  63. {
  64. if (!isset($this->string)) {
  65. $components = [
  66. $this->getPrefix(),
  67. $this->getPartition(),
  68. $this->getService(),
  69. $this->getRegion(),
  70. $this->getAccountId(),
  71. $this->getResource(),
  72. ];
  73. $this->string = implode(':', $components);
  74. }
  75. return $this->string;
  76. }
  77. public function getPrefix()
  78. {
  79. return $this->data['arn'];
  80. }
  81. public function getPartition()
  82. {
  83. return $this->data['partition'];
  84. }
  85. public function getService()
  86. {
  87. return $this->data['service'];
  88. }
  89. public function getRegion()
  90. {
  91. return $this->data['region'];
  92. }
  93. public function getAccountId()
  94. {
  95. return $this->data['account_id'];
  96. }
  97. public function getResource()
  98. {
  99. return $this->data['resource'];
  100. }
  101. public function toArray()
  102. {
  103. return $this->data;
  104. }
  105. /**
  106. * Minimally restrictive generic ARN validation
  107. *
  108. * @param array $data
  109. */
  110. protected static function validate(array $data)
  111. {
  112. if ($data['arn'] !== 'arn') {
  113. throw new InvalidArnException("The 1st component of an ARN must be"
  114. . " 'arn'.");
  115. }
  116. if (empty($data['partition'])) {
  117. throw new InvalidArnException("The 2nd component of an ARN"
  118. . " represents the partition and must not be empty.");
  119. }
  120. if (empty($data['service'])) {
  121. throw new InvalidArnException("The 3rd component of an ARN"
  122. . " represents the service and must not be empty.");
  123. }
  124. if (empty($data['resource'])) {
  125. throw new InvalidArnException("The 6th component of an ARN"
  126. . " represents the resource information and must not be empty."
  127. . " Individual service ARNs may include additional delimiters"
  128. . " to further qualify resources.");
  129. }
  130. }
  131. protected static function validateAccountId($data, $arnName)
  132. {
  133. if (!self::isValidHostLabel($data['account_id'])) {
  134. throw new InvalidArnException("The 5th component of a {$arnName}"
  135. . " is required, represents the account ID, and"
  136. . " must be a valid host label.");
  137. }
  138. }
  139. protected static function validateRegion($data, $arnName)
  140. {
  141. if (empty($data['region'])) {
  142. throw new InvalidArnException("The 4th component of a {$arnName}"
  143. . " represents the region and must not be empty.");
  144. }
  145. }
  146. /**
  147. * Validates whether a string component is a valid host label
  148. *
  149. * @param $string
  150. * @return bool
  151. */
  152. protected static function isValidHostLabel($string)
  153. {
  154. if (empty($string) || strlen($string) > 63) {
  155. return false;
  156. }
  157. if ($value = preg_match("/^[a-zA-Z0-9-]+$/", $string)) {
  158. return true;
  159. }
  160. return false;
  161. }
  162. }