|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of alchemy/resource-component. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Alchemy <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Alchemy\Resource; |
|
13
|
|
|
|
|
14
|
|
|
final class ResourceUri |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
const DEFAULT_PROTOCOL = 'file'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @param $uri |
|
21
|
|
|
* @return array |
|
22
|
|
|
*/ |
|
23
|
|
|
private static function getNonEmptyParts($uri) |
|
24
|
|
|
{ |
|
25
|
168 |
|
$nonEmptyStringFilter = function ($value) { |
|
26
|
168 |
|
return $value != ''; |
|
27
|
168 |
|
}; |
|
28
|
|
|
|
|
29
|
168 |
|
return array_filter(explode(self::PROTOCOL_SEPARATOR, $uri, 2), $nonEmptyStringFilter); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
const PROTOCOL_SEPARATOR = '://'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param $parts |
|
36
|
|
|
* @return bool |
|
37
|
|
|
*/ |
|
38
|
168 |
|
private static function validateResourceParts($parts) |
|
39
|
|
|
{ |
|
40
|
168 |
|
return count($parts) === 2; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param $protocol |
|
45
|
|
|
* @param $resource |
|
46
|
|
|
* @return self |
|
47
|
|
|
*/ |
|
48
|
4 |
|
public static function fromProtocolAndResource($protocol, $resource) |
|
49
|
|
|
{ |
|
50
|
4 |
|
return new self($protocol . self::PROTOCOL_SEPARATOR . $resource); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param $uri |
|
55
|
|
|
* @return bool |
|
56
|
|
|
*/ |
|
57
|
180 |
|
public static function isValidUri($uri) |
|
58
|
|
|
{ |
|
59
|
180 |
|
if (strpos($uri, self::PROTOCOL_SEPARATOR) === false) { |
|
60
|
12 |
|
return false; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
168 |
|
$parts = self::getNonEmptyParts($uri); |
|
64
|
|
|
|
|
65
|
168 |
|
if (! self::validateResourceParts($parts)) { |
|
66
|
44 |
|
return false; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
144 |
|
if (strpos($parts[1], self::PROTOCOL_SEPARATOR) !== false) { |
|
70
|
40 |
|
return self::isValidUri($parts[1]); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
128 |
|
return true; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
92 |
|
public static function fromString($uri) |
|
77
|
|
|
{ |
|
78
|
92 |
|
if (strpos($uri, self::PROTOCOL_SEPARATOR) === false) { |
|
79
|
28 |
|
$uri = self::DEFAULT_PROTOCOL . self::PROTOCOL_SEPARATOR . $uri; |
|
80
|
21 |
|
} |
|
81
|
|
|
|
|
82
|
92 |
|
return new self($uri); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
8 |
|
public static function fromStringArray(array $uris) |
|
86
|
|
|
{ |
|
87
|
8 |
|
$resourceUris = []; |
|
88
|
|
|
|
|
89
|
8 |
|
foreach ($uris as $uri) { |
|
90
|
8 |
|
$resourceUris[] = self::fromString($uri); |
|
91
|
6 |
|
} |
|
92
|
|
|
|
|
93
|
|
|
|
|
94
|
4 |
|
return $resourceUris; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @var string |
|
99
|
|
|
*/ |
|
100
|
|
|
private $uri; |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @var string |
|
104
|
|
|
*/ |
|
105
|
|
|
private $protocol; |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @var string |
|
109
|
|
|
*/ |
|
110
|
|
|
private $resource; |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param string $uri |
|
114
|
|
|
*/ |
|
115
|
124 |
|
public function __construct($uri) |
|
116
|
|
|
{ |
|
117
|
124 |
|
if (! self::isValidUri($uri)) { |
|
118
|
24 |
|
throw new \InvalidArgumentException(sprintf( |
|
119
|
24 |
|
'Malformed URI: required format is "protocol://resource", got "%s"', |
|
120
|
|
|
$uri |
|
121
|
18 |
|
)); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
104 |
|
$this->uri = $uri; |
|
125
|
|
|
|
|
126
|
104 |
|
list ($this->protocol, $this->resource) = explode('://', $uri, 2); |
|
127
|
104 |
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @return string |
|
131
|
|
|
*/ |
|
132
|
56 |
|
public function getUri() |
|
133
|
|
|
{ |
|
134
|
56 |
|
return $this->uri; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @return string |
|
139
|
|
|
*/ |
|
140
|
24 |
|
public function getProtocol() |
|
141
|
|
|
{ |
|
142
|
24 |
|
return $this->protocol; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @return string |
|
147
|
|
|
*/ |
|
148
|
8 |
|
public function getResource() |
|
149
|
|
|
{ |
|
150
|
8 |
|
return $this->resource; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @return bool |
|
155
|
|
|
*/ |
|
156
|
8 |
|
public function hasChainedResource() |
|
157
|
|
|
{ |
|
158
|
8 |
|
return self::isValidUri($this->resource); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @return self |
|
163
|
|
|
*/ |
|
164
|
8 |
|
public function getChainedResource() |
|
165
|
|
|
{ |
|
166
|
8 |
|
return new self($this->resource); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @param ResourceUri $other |
|
171
|
|
|
* @return bool |
|
172
|
|
|
*/ |
|
173
|
8 |
|
public function equals(ResourceUri $other) |
|
174
|
|
|
{ |
|
175
|
8 |
|
return $this->getUri() == $other->getUri(); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
public function chain($containerProtocol) |
|
179
|
|
|
{ |
|
180
|
|
|
return self::fromProtocolAndResource($containerProtocol, (string) $this); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
public function child($childRelativePath) |
|
184
|
|
|
{ |
|
185
|
|
|
return self::fromProtocolAndResource($this->protocol, $this->resource . '/' . $childRelativePath); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* @return string |
|
190
|
|
|
*/ |
|
191
|
28 |
|
public function __toString() |
|
192
|
|
|
{ |
|
193
|
28 |
|
return $this->uri; |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|