|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CodeJet\Http; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Http\Message\UriInterface; |
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
|
|
8
|
|
|
class Uri implements UriInterface |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var string |
|
12
|
|
|
*/ |
|
13
|
|
|
private $scheme = ''; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
private $userInfo = ''; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
private $host = ''; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var int |
|
27
|
|
|
*/ |
|
28
|
|
|
private $port; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
private $path = ''; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
private $query = ''; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
private $fragment = ''; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var int[] Default Ports for known Schemes. |
|
47
|
|
|
*/ |
|
48
|
|
|
private $defaultSchemePorts = [ |
|
49
|
57 |
|
'http' => 80, |
|
50
|
|
|
'https' => 443, |
|
51
|
57 |
|
]; |
|
52
|
42 |
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param string|array $uri |
|
55
|
57 |
|
* @throws InvalidArgumentException on non-string $uri argument |
|
56
|
36 |
|
*/ |
|
57
|
36 |
|
public function __construct($uri = '') |
|
58
|
6 |
|
{ |
|
59
|
6 |
|
if (is_array($uri)) { |
|
60
|
|
|
$this->parseUriParts($uri); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
30 |
|
if (is_string($uri) && !empty($uri)) { |
|
64
|
|
|
$parts = parse_url($uri); |
|
65
|
51 |
|
if (false === $parts) { |
|
66
|
|
|
throw new \InvalidArgumentException( |
|
67
|
27 |
|
'The source URI string appears to be malformed' |
|
68
|
|
|
); |
|
69
|
27 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
$this->parseUriParts($parts); |
|
72
|
48 |
|
} |
|
73
|
|
|
} |
|
74
|
48 |
|
|
|
75
|
|
|
public function __toString() |
|
76
|
48 |
|
{ |
|
77
|
27 |
|
return $this->getUriString(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
48 |
|
private function getUriString() |
|
81
|
|
|
{ |
|
82
|
48 |
|
$uri = ''; |
|
83
|
24 |
|
|
|
84
|
|
|
if ($this->hasScheme()) { |
|
85
|
|
|
$uri .= sprintf('%s://', $this->getScheme()); |
|
86
|
48 |
|
} |
|
87
|
24 |
|
|
|
88
|
|
|
$uri .= $this->getAuthority() . $this->getPath(); |
|
89
|
|
|
|
|
90
|
48 |
|
if ($this->hasQuery()) { |
|
91
|
|
|
$uri .= sprintf('?%s', $this->getQuery()); |
|
92
|
|
|
} |
|
93
|
51 |
|
|
|
94
|
|
|
if ($this->hasFragment()) { |
|
95
|
51 |
|
$uri .= sprintf('#%s', $this->getFragment()); |
|
96
|
51 |
|
} |
|
97
|
51 |
|
|
|
98
|
51 |
|
return $uri; |
|
99
|
51 |
|
} |
|
100
|
51 |
|
|
|
101
|
51 |
|
private function parseUriParts($parts) |
|
102
|
|
|
{ |
|
103
|
51 |
|
$this->scheme = isset($parts['scheme']) ? $parts['scheme'] : ''; |
|
104
|
30 |
|
$this->userInfo = isset($parts['user']) ? $parts['user'] : ''; |
|
105
|
|
|
$this->host = isset($parts['host']) ? $parts['host'] : ''; |
|
106
|
51 |
|
$this->port = isset($parts['port']) ? $parts['port'] : null; |
|
107
|
|
|
$this->path = isset($parts['path']) ? $parts['path'] : ''; |
|
108
|
51 |
|
$this->query = isset($parts['query']) ? $parts['query'] : ''; |
|
109
|
|
|
$this->fragment = isset($parts['fragment']) ? $parts['fragment'] : ''; |
|
110
|
51 |
|
|
|
111
|
33 |
|
if (isset($parts['pass'])) { |
|
112
|
|
|
$this->userInfo .= ':' . $parts['pass']; |
|
113
|
|
|
} |
|
114
|
21 |
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function hasScheme() |
|
117
|
30 |
|
{ |
|
118
|
|
|
if (!empty($this->scheme)) { |
|
119
|
30 |
|
return true; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
48 |
|
return false; |
|
123
|
|
|
} |
|
124
|
48 |
|
|
|
125
|
21 |
|
public function getScheme() |
|
126
|
|
|
{ |
|
127
|
|
|
return strtolower($this->scheme); |
|
128
|
27 |
|
} |
|
129
|
|
|
|
|
130
|
27 |
|
public function getAuthority() |
|
131
|
24 |
|
{ |
|
132
|
|
|
if (!$this->hasHost()) { |
|
133
|
|
|
return ''; |
|
134
|
27 |
|
} |
|
135
|
24 |
|
|
|
136
|
|
|
$authority = $this->getHost(); |
|
137
|
|
|
|
|
138
|
27 |
|
if ($this->hasUserInfo()) { |
|
139
|
|
|
$authority = $this->getUserInfo() . '@' . $authority; |
|
140
|
|
|
} |
|
141
|
27 |
|
|
|
142
|
|
|
if (!$this->isDefaultPort()) { |
|
143
|
27 |
|
$authority .= ':' . $this->getPort(); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
33 |
|
return $authority; |
|
147
|
|
|
} |
|
148
|
33 |
|
|
|
149
|
27 |
|
public function getUserInfo() |
|
150
|
|
|
{ |
|
151
|
|
|
return $this->userInfo; |
|
152
|
6 |
|
} |
|
153
|
|
|
|
|
154
|
|
|
public function hasUserInfo() |
|
155
|
51 |
|
{ |
|
156
|
|
|
if (!empty($this->userInfo)) { |
|
157
|
51 |
|
return true; |
|
158
|
33 |
|
} |
|
159
|
|
|
|
|
160
|
|
|
return false; |
|
161
|
21 |
|
} |
|
162
|
|
|
|
|
163
|
|
|
public function hasHost() |
|
164
|
30 |
|
{ |
|
165
|
|
|
if (!empty($this->host)) { |
|
166
|
30 |
|
return true; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
33 |
|
return false; |
|
170
|
|
|
} |
|
171
|
33 |
|
|
|
172
|
30 |
|
public function getHost() |
|
173
|
|
|
{ |
|
174
|
|
|
return strtolower($this->host); |
|
175
|
6 |
|
} |
|
176
|
|
|
|
|
177
|
|
|
public function hasPort() |
|
178
|
27 |
|
{ |
|
179
|
|
|
if (!empty($this->port)) { |
|
180
|
27 |
|
return true; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
9 |
|
return false; |
|
184
|
|
|
} |
|
185
|
9 |
|
|
|
186
|
6 |
|
public function getPort() |
|
187
|
|
|
{ |
|
188
|
|
|
if ($this->isDefaultPort()) { |
|
189
|
3 |
|
return null; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
48 |
|
return $this->port; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
48 |
|
private function isDefaultPort() |
|
196
|
|
|
{ |
|
197
|
|
|
if (!$this->hasPort()) { |
|
198
|
51 |
|
return true; |
|
199
|
|
|
} |
|
200
|
51 |
|
|
|
201
|
27 |
|
if (!$this->hasScheme()) { |
|
202
|
|
|
return false; |
|
203
|
|
|
} |
|
204
|
24 |
|
|
|
205
|
|
|
if (isset($this->defaultSchemePorts[$this->getScheme()]) |
|
206
|
|
|
&& $this->defaultSchemePorts[$this->getScheme()] == $this->port |
|
207
|
27 |
|
) { |
|
208
|
|
|
return true; |
|
209
|
27 |
|
} |
|
210
|
|
|
|
|
211
|
|
|
return false; |
|
212
|
51 |
|
} |
|
213
|
|
|
|
|
214
|
51 |
|
public function hasPath() |
|
215
|
27 |
|
{ |
|
216
|
|
|
if (!empty($this->path)) { |
|
217
|
|
|
return true; |
|
218
|
24 |
|
} |
|
219
|
|
|
|
|
220
|
|
|
return false; |
|
221
|
27 |
|
} |
|
222
|
|
|
|
|
223
|
27 |
|
public function getPath() |
|
224
|
|
|
{ |
|
225
|
|
|
|
|
226
|
6 |
|
return $this->path; |
|
227
|
|
|
} |
|
228
|
6 |
|
|
|
229
|
6 |
|
public function hasQuery() |
|
230
|
|
|
{ |
|
231
|
|
|
if (!empty($this->query)) { |
|
232
|
|
|
return true; |
|
233
|
6 |
|
} |
|
234
|
|
|
|
|
235
|
6 |
|
return false; |
|
236
|
|
|
} |
|
237
|
6 |
|
|
|
238
|
6 |
|
public function getQuery() |
|
239
|
|
|
{ |
|
240
|
|
|
return $this->query; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
6 |
|
public function hasFragment() |
|
244
|
|
|
{ |
|
245
|
6 |
|
if (!empty($this->fragment)) { |
|
246
|
6 |
|
return true; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
return false; |
|
250
|
6 |
|
} |
|
251
|
|
|
|
|
252
|
6 |
|
public function getFragment() |
|
253
|
6 |
|
{ |
|
254
|
|
|
return $this->fragment; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
6 |
|
public function withScheme($scheme) |
|
258
|
|
|
{ |
|
259
|
6 |
|
return $this->withModifiedParts( |
|
260
|
6 |
|
['scheme' => $scheme] |
|
261
|
|
|
); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
6 |
|
public function withUserInfo($user, $password = null) |
|
265
|
|
|
{ |
|
266
|
6 |
|
return $this->withModifiedParts( |
|
267
|
6 |
|
[ |
|
268
|
|
|
'user' => $user, |
|
269
|
|
|
'pass' => $password |
|
270
|
|
|
] |
|
271
|
6 |
|
); |
|
272
|
|
|
} |
|
273
|
6 |
|
|
|
274
|
6 |
|
public function withHost($host) |
|
275
|
|
|
{ |
|
276
|
|
|
return $this->withModifiedParts( |
|
277
|
|
|
['host' => $host] |
|
278
|
|
|
); |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
public function withPort($port) |
|
282
|
42 |
|
{ |
|
283
|
|
|
return $this->withModifiedParts( |
|
284
|
42 |
|
['port' => $port] |
|
285
|
|
|
); |
|
286
|
42 |
|
} |
|
287
|
|
|
|
|
288
|
42 |
|
public function withPath($path) |
|
289
|
|
|
{ |
|
290
|
|
|
return $this->withModifiedParts( |
|
291
|
|
|
['path' => $path] |
|
292
|
|
|
); |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
public function withQuery($query) |
|
296
|
|
|
{ |
|
297
|
|
|
return $this->withModifiedParts( |
|
298
|
|
|
['query' => $query] |
|
299
|
|
|
); |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
public function withFragment($fragment) |
|
303
|
|
|
{ |
|
304
|
|
|
return $this->withModifiedParts( |
|
305
|
|
|
['fragment' => $fragment] |
|
306
|
|
|
); |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
/** |
|
310
|
|
|
* @param array $modifiedParts |
|
311
|
|
|
* @return Uri |
|
312
|
|
|
*/ |
|
313
|
|
|
private function withModifiedParts($modifiedParts) |
|
314
|
|
|
{ |
|
315
|
|
|
$currentParts = parse_url($this->getUriString()); |
|
316
|
|
|
|
|
317
|
|
|
$newUriParts = array_merge($currentParts, $modifiedParts); |
|
318
|
|
|
|
|
319
|
|
|
return new self($newUriParts); |
|
320
|
|
|
} |
|
321
|
|
|
} |
|
322
|
|
|
|