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