1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* League.Uri (https://uri.thephpleague.com) |
5
|
|
|
* |
6
|
|
|
* (c) Ignace Nyamagana Butera <[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
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace League\Uri; |
15
|
|
|
|
16
|
|
|
use JsonSerializable; |
17
|
|
|
use League\Uri\Contract\UriInterface; |
18
|
|
|
use League\Uri\Exception\SyntaxError; |
19
|
|
|
use Psr\Http\Message\UriInterface as Psr7UriInterface; |
20
|
|
|
use function is_scalar; |
21
|
|
|
use function method_exists; |
22
|
|
|
use function sprintf; |
23
|
|
|
|
24
|
|
|
final class Http implements Psr7UriInterface, JsonSerializable |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var UriInterface |
28
|
|
|
*/ |
29
|
|
|
private $uri; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* New instance. |
33
|
|
|
*/ |
34
|
222 |
|
public function __construct(UriInterface $uri) |
35
|
|
|
{ |
36
|
222 |
|
$this->validate($uri); |
37
|
222 |
|
$this->uri = $uri; |
38
|
222 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Validate the submitted uri against PSR-7 UriInterface. |
42
|
|
|
* |
43
|
|
|
* @throws SyntaxError if the given URI does not follow PSR-7 UriInterface rules |
44
|
|
|
*/ |
45
|
228 |
|
private function validate(UriInterface $uri): void |
46
|
|
|
{ |
47
|
228 |
|
$scheme = $uri->getScheme(); |
48
|
228 |
|
if (null === $scheme && '' === $uri->getHost()) { |
49
|
2 |
|
throw new SyntaxError(sprintf('an URI without scheme can not contains a empty host string according to PSR-7: %s', (string) $uri)); |
50
|
|
|
} |
51
|
|
|
|
52
|
228 |
|
$port = $uri->getPort(); |
53
|
228 |
|
if (null !== $port && ($port < 0 || $port > 65535)) { |
54
|
2 |
|
throw new SyntaxError(sprintf('The URI port is outside the established TCP and UDP port ranges: %s', (string) $uri->getPort())); |
55
|
|
|
} |
56
|
228 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Static method called by PHP's var export. |
60
|
|
|
* |
61
|
|
|
* @return static |
62
|
|
|
*/ |
63
|
18 |
|
public static function __set_state(array $components): self |
64
|
|
|
{ |
65
|
18 |
|
return new self($components['uri']); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Create a new instance from a string. |
70
|
|
|
* |
71
|
|
|
* @param string|mixed $uri |
72
|
|
|
*/ |
73
|
200 |
|
public static function createFromString($uri = ''): self |
74
|
|
|
{ |
75
|
200 |
|
return new self(Uri::createFromString($uri)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Create a new instance from a hash of parse_url parts. |
80
|
|
|
* |
81
|
|
|
* @param array $components a hash representation of the URI similar |
82
|
|
|
* to PHP parse_url function result |
83
|
|
|
*/ |
84
|
2 |
|
public static function createFromComponents(array $components): self |
85
|
|
|
{ |
86
|
2 |
|
return new self(Uri::createFromComponents($components)); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Create a new instance from the environment. |
91
|
|
|
*/ |
92
|
26 |
|
public static function createFromServer(array $server): self |
93
|
|
|
{ |
94
|
26 |
|
return new self(Uri::createFromServer($server)); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Create a new instance from a URI and a Base URI. |
99
|
|
|
* |
100
|
|
|
* The returned URI must be absolute. |
101
|
|
|
* |
102
|
|
|
* @param mixed $uri the input URI to create |
103
|
|
|
* @param mixed $base_uri the base URI used for reference |
104
|
|
|
*/ |
105
|
2 |
|
public static function createFromBaseUri($uri, $base_uri = null): self |
106
|
|
|
{ |
107
|
2 |
|
return new self(Uri::createFromBaseUri($uri, $base_uri)); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritdoc} |
112
|
|
|
*/ |
113
|
186 |
|
public function getScheme(): string |
114
|
|
|
{ |
115
|
186 |
|
return (string) $this->uri->getScheme(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritdoc} |
120
|
|
|
*/ |
121
|
166 |
|
public function getAuthority(): string |
122
|
|
|
{ |
123
|
166 |
|
return (string) $this->uri->getAuthority(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritdoc} |
128
|
|
|
*/ |
129
|
76 |
|
public function getUserInfo(): string |
130
|
|
|
{ |
131
|
76 |
|
return (string) $this->uri->getUserInfo(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* {@inheritdoc} |
136
|
|
|
*/ |
137
|
196 |
|
public function getHost(): string |
138
|
|
|
{ |
139
|
196 |
|
return (string) $this->uri->getHost(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritdoc} |
144
|
|
|
*/ |
145
|
76 |
|
public function getPort(): ?int |
146
|
|
|
{ |
147
|
76 |
|
return $this->uri->getPort(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* {@inheritdoc} |
152
|
|
|
*/ |
153
|
170 |
|
public function getPath(): string |
154
|
|
|
{ |
155
|
170 |
|
return $this->uri->getPath(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* {@inheritdoc} |
160
|
|
|
*/ |
161
|
44 |
|
public function getQuery(): string |
162
|
|
|
{ |
163
|
44 |
|
return (string) $this->uri->getQuery(); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* {@inheritdoc} |
168
|
|
|
*/ |
169
|
24 |
|
public function getFragment(): string |
170
|
|
|
{ |
171
|
24 |
|
return (string) $this->uri->getFragment(); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* {@inheritdoc} |
176
|
|
|
*/ |
177
|
10 |
|
public function withScheme($scheme): self |
178
|
|
|
{ |
179
|
10 |
|
$scheme = $this->filterInput($scheme); |
180
|
10 |
|
if ('' === $scheme) { |
181
|
10 |
|
$scheme = null; |
182
|
|
|
} |
183
|
|
|
|
184
|
10 |
|
$uri = $this->uri->withScheme($scheme); |
185
|
10 |
|
if ($uri->getScheme() === $this->uri->getScheme()) { |
186
|
2 |
|
return $this; |
187
|
|
|
} |
188
|
|
|
|
189
|
8 |
|
return new self($uri); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Safely stringify input when possible. |
194
|
|
|
* |
195
|
|
|
* @param mixed $str the value to evaluate as a string |
196
|
|
|
* |
197
|
|
|
* @throws SyntaxError if the submitted data can not be converted to string |
198
|
|
|
* |
199
|
|
|
* @return string|mixed |
200
|
|
|
*/ |
201
|
94 |
|
private function filterInput($str) |
202
|
|
|
{ |
203
|
94 |
|
if (is_scalar($str) || method_exists($str, '__toString')) { |
204
|
92 |
|
return (string) $str; |
205
|
|
|
} |
206
|
|
|
|
207
|
2 |
|
return $str; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* {@inheritdoc} |
212
|
|
|
*/ |
213
|
8 |
|
public function withUserInfo($user, $password = null): self |
214
|
|
|
{ |
215
|
8 |
|
$user = $this->filterInput($user); |
216
|
8 |
|
if ('' === $user) { |
217
|
8 |
|
$user = null; |
218
|
|
|
} |
219
|
|
|
|
220
|
8 |
|
$uri = $this->uri->withUserInfo($user, $password); |
221
|
8 |
|
if ($uri->getUserInfo() === $this->uri->getUserInfo()) { |
222
|
8 |
|
return $this; |
223
|
|
|
} |
224
|
|
|
|
225
|
2 |
|
return new self($uri); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* {@inheritdoc} |
230
|
|
|
*/ |
231
|
90 |
|
public function withHost($host): self |
232
|
|
|
{ |
233
|
90 |
|
$host = $this->filterInput($host); |
234
|
90 |
|
if ('' === $host) { |
235
|
12 |
|
$host = null; |
236
|
|
|
} |
237
|
|
|
|
238
|
90 |
|
$uri = $this->uri->withHost($host); |
239
|
90 |
|
if ($uri->getHost() === $this->uri->getHost()) { |
240
|
88 |
|
return $this; |
241
|
|
|
} |
242
|
|
|
|
243
|
8 |
|
return new self($uri); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* {@inheritdoc} |
248
|
|
|
*/ |
249
|
8 |
|
public function withPort($port): self |
250
|
|
|
{ |
251
|
8 |
|
$uri = $this->uri->withPort($port); |
252
|
8 |
|
if ($uri->getPort() === $this->uri->getPort()) { |
253
|
8 |
|
return $this; |
254
|
|
|
} |
255
|
|
|
|
256
|
2 |
|
return new self($uri); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* {@inheritdoc} |
261
|
|
|
*/ |
262
|
38 |
|
public function withPath($path): self |
263
|
|
|
{ |
264
|
38 |
|
$uri = $this->uri->withPath($path); |
265
|
30 |
|
if ($uri->getPath() === $this->uri->getPath()) { |
266
|
24 |
|
return $this; |
267
|
|
|
} |
268
|
|
|
|
269
|
26 |
|
return new self($uri); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* {@inheritdoc} |
274
|
|
|
*/ |
275
|
22 |
|
public function withQuery($query): self |
276
|
|
|
{ |
277
|
22 |
|
$query = $this->filterInput($query); |
278
|
22 |
|
if ('' === $query) { |
279
|
22 |
|
$query = null; |
280
|
|
|
} |
281
|
|
|
|
282
|
22 |
|
$uri = $this->uri->withQuery($query); |
283
|
22 |
|
if ($uri->getQuery() === $this->uri->getQuery()) { |
284
|
22 |
|
return $this; |
285
|
|
|
} |
286
|
|
|
|
287
|
4 |
|
return new self($uri); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* {@inheritdoc} |
292
|
|
|
*/ |
293
|
22 |
|
public function withFragment($fragment): self |
294
|
|
|
{ |
295
|
22 |
|
$fragment = $this->filterInput($fragment); |
296
|
22 |
|
if ('' === $fragment) { |
297
|
20 |
|
$fragment = null; |
298
|
|
|
} |
299
|
|
|
|
300
|
22 |
|
$uri = $this->uri->withFragment($fragment); |
301
|
22 |
|
if ($uri->getFragment() === $this->uri->getFragment()) { |
302
|
22 |
|
return $this; |
303
|
|
|
} |
304
|
|
|
|
305
|
2 |
|
return new self($uri); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* {@inheritdoc} |
310
|
|
|
*/ |
311
|
48 |
|
public function __toString(): string |
312
|
|
|
{ |
313
|
48 |
|
return $this->uri->__toString(); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* {@inheritdoc} |
318
|
|
|
*/ |
319
|
2 |
|
public function jsonSerialize(): string |
320
|
|
|
{ |
321
|
2 |
|
return $this->uri->__toString(); |
322
|
|
|
} |
323
|
|
|
} |
324
|
|
|
|