Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

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