summaryrefslogtreecommitdiff
path: root/vendor/aws/aws-sdk-php/src/Arn/Arn.php
blob: 50367c65c4b9b2af63feb27eb8f04737408b35b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
namespace Aws\Arn;

use Aws\Arn\Exception\InvalidArnException;

/**
 * Amazon Resource Names (ARNs) uniquely identify AWS resources. The Arn class
 * parses and stores a generic ARN object representation that can apply to any
 * service resource.
 *
 * @internal
 */
class Arn implements ArnInterface
{
    protected $data;
    protected $string;

    public static function parse($string)
    {
        $data = [
            'arn' => null,
            'partition' => null,
            'service' => null,
            'region' => null,
            'account_id' => null,
            'resource' => null,
        ];

        $length = strlen($string);
        $lastDelim = 0;
        $numComponents = 0;
        for ($i = 0; $i < $length; $i++) {

            if (($numComponents < 5 && $string[$i] === ':')) {
                // Split components between delimiters
                $data[key($data)] = substr($string, $lastDelim, $i - $lastDelim);

                // Do not include delimiter character itself
                $lastDelim = $i + 1;
                next($data);
                $numComponents++;
            }

            if ($i === $length - 1) {
                // Put the remainder in the last component.
                if (in_array($numComponents, [5])) {
                    $data['resource'] = substr($string, $lastDelim);
                } else {
                    // If there are < 5 components, put remainder in current
                    // component.
                    $data[key($data)] = substr($string, $lastDelim);
                }
            }
        }

        return $data;
    }

    public function __construct($data)
    {
        if (is_array($data)) {
            $this->data = $data;
        } elseif (is_string($data)) {
            $this->data = static::parse($data);
        } else {
            throw new InvalidArnException('Constructor accepts a string or an'
                . ' array as an argument.');
        }

        static::validate($this->data);
    }

    public function __toString()
    {
        if (!isset($this->string)) {
            $components = [
                $this->getPrefix(),
                $this->getPartition(),
                $this->getService(),
                $this->getRegion(),
                $this->getAccountId(),
                $this->getResource(),
            ];

            $this->string = implode(':', $components);
        }
        return $this->string;
    }

    public function getPrefix()
    {
        return $this->data['arn'];
    }

    public function getPartition()
    {
        return $this->data['partition'];
    }

    public function getService()
    {
        return $this->data['service'];
    }

    public function getRegion()
    {
        return $this->data['region'];
    }

    public function getAccountId()
    {
        return $this->data['account_id'];
    }

    public function getResource()
    {
        return $this->data['resource'];
    }

    public function toArray()
    {
        return $this->data;
    }

    /**
     * Minimally restrictive generic ARN validation
     *
     * @param array $data
     */
    protected static function validate(array $data)
    {
        if ($data['arn'] !== 'arn') {
            throw new InvalidArnException("The 1st component of an ARN must be"
                . " 'arn'.");
        }

        if (empty($data['partition'])) {
            throw new InvalidArnException("The 2nd component of an ARN"
                . " represents the partition and must not be empty.");
        }

        if (empty($data['service'])) {
            throw new InvalidArnException("The 3rd component of an ARN"
                . " represents the service and must not be empty.");
        }

        if (empty($data['resource'])) {
            throw new InvalidArnException("The 6th component of an ARN"
                . " represents the resource information and must not be empty."
                . " Individual service ARNs may include additional delimiters"
                . " to further qualify resources.");
        }
    }

    protected static function validateAccountId($data, $arnName)
    {
        if (!self::isValidHostLabel($data['account_id'])) {
            throw new InvalidArnException("The 5th component of a {$arnName}"
                . " is required, represents the account ID, and"
                . " must be a valid host label.");
        }
    }

    protected static function validateRegion($data, $arnName)
    {
        if (empty($data['region'])) {
            throw new InvalidArnException("The 4th component of a {$arnName}"
                . " represents the region and must not be empty.");
        }
    }

    /**
     * Validates whether a string component is a valid host label
     *
     * @param $string
     * @return bool
     */
    protected static function isValidHostLabel($string)
    {
        if (empty($string) || strlen($string) > 63) {
            return false;
        }
        if ($value = preg_match("/^[a-zA-Z0-9-]+$/", $string)) {
            return true;
        }
        return false;
    }
}