ArnParser.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace Aws\Arn;
  3. use Aws\Arn\S3\AccessPointArn as S3AccessPointArn;
  4. use Aws\Arn\S3\OutpostsBucketArn;
  5. use Aws\Arn\S3\RegionalBucketArn;
  6. use Aws\Arn\S3\OutpostsAccessPointArn;
  7. /**
  8. * This class provides functionality to parse ARN strings and return a
  9. * corresponding ARN object. ARN-parsing logic may be subject to change in the
  10. * future, so this should not be relied upon for external customer usage.
  11. *
  12. * @internal
  13. */
  14. class ArnParser
  15. {
  16. /**
  17. * @param $string
  18. * @return bool
  19. */
  20. public static function isArn($string)
  21. {
  22. return strpos($string, 'arn:') === 0;
  23. }
  24. /**
  25. * Parses a string and returns an instance of ArnInterface. Returns a
  26. * specific type of Arn object if it has a specific class representation
  27. * or a generic Arn object if not.
  28. *
  29. * @param $string
  30. * @return ArnInterface
  31. */
  32. public static function parse($string)
  33. {
  34. $data = Arn::parse($string);
  35. $resource = self::explodeResourceComponent($data['resource']);
  36. if ($resource[0] === 'outpost') {
  37. if (isset($resource[2]) && $resource[2] === 'bucket') {
  38. return new OutpostsBucketArn($string);
  39. }
  40. if (isset($resource[2]) && $resource[2] === 'accesspoint') {
  41. return new OutpostsAccessPointArn($string);
  42. }
  43. }
  44. if ($resource[0] === 'accesspoint') {
  45. if ($data['service'] === 's3') {
  46. return new S3AccessPointArn($string);
  47. }
  48. return new AccessPointArn($string);
  49. }
  50. return new Arn($data);
  51. }
  52. private static function explodeResourceComponent($resource)
  53. {
  54. return preg_split("/[\/:]/", $resource);
  55. }
  56. }