ArnParser.php 2.0 KB

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