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 UriInterface|string $uri |
54
|
|
|
* @return UriInterface |
55
|
|
|
*/ |
56
|
44 |
|
public function __construct($uri = '') |
57
|
|
|
{ |
58
|
|
|
|
59
|
44 |
|
if ($uri instanceof UriInterface) { |
60
|
|
|
return $uri; |
61
|
|
|
} |
62
|
|
|
|
63
|
44 |
|
$components = array_merge(array( |
64
|
44 |
|
'scheme' => '', |
65
|
22 |
|
'host' => '', |
66
|
22 |
|
'port' => '', |
67
|
22 |
|
'user' => '', |
68
|
22 |
|
'pass' => '', |
69
|
22 |
|
'path' => '', |
70
|
22 |
|
'query' => '', |
71
|
|
|
'fragment' => '' |
72
|
44 |
|
), parse_url($uri)); |
73
|
|
|
|
74
|
44 |
|
$this->scheme = $components['scheme']; |
75
|
44 |
|
$this->host = $components['host']; |
76
|
44 |
|
$this->port = $this->isStandartPort($components['port']); |
77
|
44 |
|
$this->path = $components['path']; |
78
|
44 |
|
$this->query = $components['query']; |
79
|
44 |
|
$this->fragment = $components['fragment']; |
80
|
44 |
|
$this->userInfo = implode(':', array_filter([$components['user'], $components['pass']])); |
81
|
44 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param int $port |
85
|
|
|
* @return int|null |
86
|
|
|
*/ |
87
|
44 |
|
public function isStandartPort($port) |
88
|
|
|
{ |
89
|
44 |
|
if (!in_array($port, [self::$schemes['http'], self::$schemes['https']])) { |
90
|
44 |
|
return $port; |
91
|
|
|
} |
92
|
|
|
return null; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
public function __toString() |
99
|
|
|
{ |
100
|
|
|
$uri = $this->scheme; |
101
|
|
|
|
102
|
|
|
if ($this->userInfo !== null) { |
103
|
|
|
$uri .= "//{$this->userInfo}"; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if ($this->port !== null && !in_array($this->port, [self::$schemes['http'], self::$schemes['https']])) { |
107
|
|
|
$uri .= ":{$this->port}"; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$uri .= $this->path; |
111
|
|
|
|
112
|
|
|
if ($this->query !== null) { |
113
|
|
|
$uri .= "?{$this->query}"; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if ($this->fragment !== null) { |
117
|
|
|
$uri .= "#{$this->fragment}"; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $uri; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
|
|
public function getAuthority() |
127
|
|
|
{ |
128
|
|
|
return ($this->userInfo ?: $this->userInfo) . $this->host . ($this->port ?: ":{$this->port}"); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
2 |
|
public function getFragment() |
135
|
|
|
{ |
136
|
2 |
|
return $this->fragment; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
32 |
|
public function getHost() |
143
|
|
|
{ |
144
|
32 |
|
return $this->host; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
4 |
|
public function getPath() |
151
|
|
|
{ |
152
|
4 |
|
return $this->path; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @return null|int |
157
|
|
|
*/ |
158
|
4 |
|
public function getPort() |
159
|
|
|
{ |
160
|
4 |
|
return $this->port; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @return string |
165
|
|
|
*/ |
166
|
4 |
|
public function getQuery() |
167
|
|
|
{ |
168
|
4 |
|
return $this->query; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @return string |
173
|
|
|
*/ |
174
|
4 |
|
public function getScheme() |
175
|
|
|
{ |
176
|
4 |
|
return $this->scheme; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @return string |
181
|
|
|
*/ |
182
|
4 |
|
public function getUserInfo() |
183
|
|
|
{ |
184
|
4 |
|
return $this->userInfo; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param string $fragment |
189
|
|
|
* @return self |
190
|
|
|
*/ |
191
|
|
|
public function withFragment($fragment) |
192
|
|
|
{ |
193
|
|
|
if ($fragment === $this->fragment) { |
194
|
|
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
$clone = clone $this; |
198
|
|
|
$clone->fragment = $fragment; |
199
|
|
|
|
200
|
|
|
return $clone; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param string $host |
205
|
|
|
* @return self |
206
|
|
|
*/ |
207
|
2 |
|
public function withHost($host) |
208
|
|
|
{ |
209
|
2 |
|
if ($host === $this->host) { |
210
|
|
|
return $this; |
211
|
|
|
} |
212
|
|
|
|
213
|
2 |
|
$clone = clone $this; |
214
|
2 |
|
$clone->host = $host; |
215
|
|
|
|
216
|
2 |
|
return $clone; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @param string $path |
221
|
|
|
* @return self |
222
|
|
|
*/ |
223
|
2 |
|
public function withPath($path) |
224
|
|
|
{ |
225
|
2 |
|
if ($path === $this->path) { |
226
|
|
|
return $this; |
227
|
|
|
} |
228
|
|
|
|
229
|
2 |
|
$clone = clone $this; |
230
|
2 |
|
$clone->path = $path; |
231
|
|
|
|
232
|
2 |
|
return $clone; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @param int $port |
237
|
|
|
* @return self |
238
|
|
|
*/ |
239
|
2 |
|
public function withPort($port) |
240
|
|
|
{ |
241
|
2 |
|
if ($port === $this->port) { |
242
|
|
|
return $this; |
243
|
|
|
} |
244
|
|
|
|
245
|
2 |
|
$clone = clone $this; |
246
|
2 |
|
$clone->port = $port; |
247
|
|
|
|
248
|
2 |
|
return $clone; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @param string $query |
253
|
|
|
* @return self |
254
|
|
|
*/ |
255
|
2 |
|
public function withQuery($query) |
256
|
|
|
{ |
257
|
2 |
|
if ($query === $this->query) { |
258
|
|
|
return $this; |
259
|
|
|
} |
260
|
|
|
|
261
|
2 |
|
$clone = clone $this; |
262
|
2 |
|
$clone->query = $query; |
263
|
|
|
|
264
|
2 |
|
return $clone; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @param string $scheme |
269
|
|
|
* @return self |
270
|
|
|
*/ |
271
|
2 |
|
public function withScheme($scheme) |
272
|
|
|
{ |
273
|
2 |
|
if ($scheme === $this->scheme) { |
274
|
|
|
return $this; |
275
|
|
|
} |
276
|
|
|
|
277
|
2 |
|
$clone = clone $this; |
278
|
2 |
|
$clone->scheme = $scheme; |
279
|
|
|
|
280
|
2 |
|
return $clone; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* @param string $user |
285
|
|
|
* @param string|null $password |
286
|
|
|
* @return self |
287
|
|
|
*/ |
288
|
2 |
|
public function withUserInfo($user, $password = null) |
289
|
|
|
{ |
290
|
2 |
|
$userInfo = $user; |
291
|
2 |
|
if (null !== $password) { |
292
|
|
|
$userInfo .= ":{$password}"; |
293
|
|
|
} |
294
|
|
|
|
295
|
2 |
|
if ($userInfo === $this->userInfo) { |
296
|
|
|
return $this; |
297
|
|
|
} |
298
|
|
|
|
299
|
2 |
|
$clone = clone $this; |
300
|
2 |
|
$clone->userInfo = $userInfo; |
301
|
|
|
|
302
|
2 |
|
return $clone; |
303
|
|
|
} |
304
|
|
|
} |
305
|
|
|
|