1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Uri package. |
4
|
|
|
* |
5
|
|
|
* @author Daniel Schröder <[email protected]> |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace GravityMedia\Uri; |
9
|
|
|
|
10
|
|
|
use Psr\Http\Message\UriInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* URI class. |
14
|
|
|
* |
15
|
|
|
* @package GravityMedia\Uri |
16
|
|
|
*/ |
17
|
|
|
class Uri implements UriInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Unreserved characters used in paths, query strings, and fragments. |
21
|
|
|
* |
22
|
|
|
* @const string |
23
|
|
|
*/ |
24
|
|
|
const CHAR_UNRESERVED = 'A-Za-z0-9\-\._~'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Sub-delimiters used in query strings and fragments. |
28
|
|
|
* |
29
|
|
|
* @const string |
30
|
|
|
*/ |
31
|
|
|
const CHAR_SUB_DELIMS = '!\$&\'\(\)\*\+,;='; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Pattern for path filtering. |
35
|
|
|
* |
36
|
|
|
* @const string |
37
|
|
|
*/ |
38
|
|
|
const PATH_FILTER_PATTERN = '/(?:[^' . self::CHAR_UNRESERVED . self::CHAR_SUB_DELIMS |
39
|
|
|
. '%:@\/]+|%(?![A-Fa-f0-9]{2}))/'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Pattern for query or fragment filtering. |
43
|
|
|
* |
44
|
|
|
* @const string |
45
|
|
|
*/ |
46
|
|
|
const QUERY_OR_FRAGMENT_FILTER_PATTERN = '/(?:[^' . self::CHAR_UNRESERVED . self::CHAR_SUB_DELIMS |
47
|
|
|
. '%:@\/\?]+|%(?![A-Fa-f0-9]{2}))/'; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* The scheme. |
51
|
|
|
* |
52
|
|
|
* @var null|string |
53
|
|
|
*/ |
54
|
|
|
protected $scheme; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* The user info. |
58
|
|
|
* |
59
|
|
|
* @var null|string |
60
|
|
|
*/ |
61
|
|
|
protected $userInfo; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* The host. |
65
|
|
|
* |
66
|
|
|
* @var null|string |
67
|
|
|
*/ |
68
|
|
|
protected $host; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* The port. |
72
|
|
|
* |
73
|
|
|
* @var null|int |
74
|
|
|
*/ |
75
|
|
|
protected $port; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* The path. |
79
|
|
|
* |
80
|
|
|
* @var null|string |
81
|
|
|
*/ |
82
|
|
|
protected $path; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* The query. |
86
|
|
|
* |
87
|
|
|
* @var null|string |
88
|
|
|
*/ |
89
|
|
|
protected $query; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* The fragment. |
93
|
|
|
* |
94
|
|
|
* @var null|string |
95
|
|
|
*/ |
96
|
|
|
protected $fragment; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Create URI object from array. |
100
|
|
|
* |
101
|
|
|
* @param array $array |
102
|
|
|
* |
103
|
|
|
* @return static |
104
|
|
|
* @throws \InvalidArgumentException |
105
|
|
|
*/ |
106
|
8 |
|
public static function fromArray(array $array = []) |
107
|
|
|
{ |
108
|
8 |
|
$uri = new static(); |
109
|
|
|
|
110
|
8 |
|
if (isset($array['scheme'])) { |
111
|
6 |
|
$uri = $uri->withScheme($array['scheme']); |
112
|
3 |
|
} |
113
|
|
|
|
114
|
8 |
|
if (isset($array['user'])) { |
115
|
2 |
|
$password = null; |
116
|
2 |
|
if (isset($array['pass'])) { |
117
|
2 |
|
$password = $array['pass']; |
118
|
1 |
|
} |
119
|
|
|
|
120
|
2 |
|
$uri = $uri->withUserInfo($array['user'], $password); |
121
|
1 |
|
} |
122
|
|
|
|
123
|
8 |
|
if (isset($array['host'])) { |
124
|
4 |
|
$uri = $uri->withHost($array['host']); |
125
|
2 |
|
} |
126
|
|
|
|
127
|
8 |
|
if (isset($array['port'])) { |
128
|
4 |
|
$uri = $uri->withPort($array['port']); |
129
|
2 |
|
} |
130
|
|
|
|
131
|
8 |
|
if (isset($array['path'])) { |
132
|
6 |
|
$uri = $uri->withPath($array['path']); |
133
|
3 |
|
} |
134
|
|
|
|
135
|
8 |
|
if (isset($array['query'])) { |
136
|
2 |
|
$uri = $uri->withQuery($array['query']); |
137
|
1 |
|
} |
138
|
|
|
|
139
|
8 |
|
if (isset($array['fragment'])) { |
140
|
2 |
|
$uri = $uri->withFragment($array['fragment']); |
141
|
1 |
|
} |
142
|
|
|
|
143
|
8 |
|
return $uri; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Create URI object from string. |
148
|
|
|
* |
149
|
|
|
* @param string $string |
150
|
|
|
* |
151
|
|
|
* @return static |
152
|
|
|
* @throws \InvalidArgumentException |
153
|
|
|
*/ |
154
|
10 |
|
public static function fromString($string) |
155
|
|
|
{ |
156
|
10 |
|
$array = parse_url($string); |
157
|
10 |
|
if (false === $array) { |
158
|
2 |
|
throw new \InvalidArgumentException('The string argument appears to be malformed'); |
159
|
|
|
} |
160
|
|
|
|
161
|
8 |
|
return static::fromArray($array); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* {@inheritdoc} |
166
|
|
|
*/ |
167
|
8 |
|
public function getScheme() |
168
|
|
|
{ |
169
|
8 |
|
if (null === $this->scheme) { |
170
|
2 |
|
return ''; |
171
|
|
|
} |
172
|
|
|
|
173
|
6 |
|
return strtolower($this->scheme); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* {@inheritdoc} |
178
|
|
|
*/ |
179
|
8 |
|
public function getAuthority() |
180
|
|
|
{ |
181
|
8 |
|
$authority = $this->getHost(); |
182
|
8 |
|
if (0 === strlen($authority)) { |
183
|
4 |
|
return ''; |
184
|
|
|
} |
185
|
|
|
|
186
|
4 |
|
$userInfo = $this->getUserInfo(); |
187
|
4 |
|
if (strlen($userInfo) > 0) { |
188
|
2 |
|
$authority = $userInfo . '@' . $authority; |
189
|
1 |
|
} |
190
|
|
|
|
191
|
4 |
|
$port = $this->getPort(); |
192
|
4 |
|
if (is_int($port)) { |
193
|
2 |
|
$authority .= ':' . $port; |
194
|
1 |
|
} |
195
|
|
|
|
196
|
4 |
|
return $authority; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* {@inheritdoc} |
201
|
|
|
*/ |
202
|
8 |
|
public function getUserInfo() |
203
|
|
|
{ |
204
|
8 |
|
if (null === $this->userInfo) { |
205
|
6 |
|
return ''; |
206
|
|
|
} |
207
|
|
|
|
208
|
2 |
|
return $this->userInfo; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* {@inheritdoc} |
213
|
|
|
*/ |
214
|
8 |
|
public function getHost() |
215
|
|
|
{ |
216
|
8 |
|
if (null === $this->host) { |
217
|
4 |
|
return ''; |
218
|
|
|
} |
219
|
|
|
|
220
|
4 |
|
return strtolower($this->host); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* {@inheritdoc} |
225
|
|
|
*/ |
226
|
8 |
|
public function getPort() |
227
|
|
|
{ |
228
|
8 |
|
$scheme = $this->getScheme(); |
229
|
8 |
|
if (0 === strlen($scheme) && null === $this->port) { |
230
|
2 |
|
return null; |
231
|
|
|
} |
232
|
|
|
|
233
|
6 |
|
if (!SchemeRegistry::isSchemeRegistered($scheme) || SchemeRegistry::isStandardPort($scheme, $this->port)) { |
234
|
4 |
|
return null; |
235
|
|
|
} |
236
|
|
|
|
237
|
2 |
|
return $this->port; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* {@inheritdoc} |
242
|
|
|
*/ |
243
|
8 |
|
public function getPath() |
244
|
|
|
{ |
245
|
8 |
|
if (null === $this->path) { |
246
|
2 |
|
return ''; |
247
|
|
|
} |
248
|
|
|
|
249
|
6 |
|
return $this->filterPathValue($this->path); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* {@inheritdoc} |
254
|
|
|
*/ |
255
|
8 |
|
public function getQuery() |
256
|
|
|
{ |
257
|
8 |
|
if (null === $this->query) { |
258
|
6 |
|
return ''; |
259
|
|
|
} |
260
|
|
|
|
261
|
2 |
|
return $this->filterQueryOrFragmentValue($this->query); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* {@inheritdoc} |
266
|
|
|
*/ |
267
|
8 |
|
public function getFragment() |
268
|
|
|
{ |
269
|
8 |
|
if (null === $this->fragment) { |
270
|
6 |
|
return ''; |
271
|
|
|
} |
272
|
|
|
|
273
|
2 |
|
return $this->filterQueryOrFragmentValue($this->fragment); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Filter path value. |
278
|
|
|
* |
279
|
|
|
* @param string $value |
280
|
|
|
* |
281
|
|
|
* @return string |
282
|
|
|
*/ |
283
|
6 |
|
protected function filterPathValue($value) |
284
|
|
|
{ |
285
|
6 |
|
return preg_replace_callback(static::PATH_FILTER_PATTERN, [$this, 'urlEncodeFirstMatch'], $value); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Filter query or fragment value. |
290
|
|
|
* |
291
|
|
|
* @param string $value |
292
|
|
|
* |
293
|
|
|
* @return string |
294
|
|
|
*/ |
295
|
2 |
|
protected function filterQueryOrFragmentValue($value) |
296
|
|
|
{ |
297
|
2 |
|
return preg_replace_callback(static::QUERY_OR_FRAGMENT_FILTER_PATTERN, [$this, 'urlEncodeFirstMatch'], $value); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* URL encode a the first match returned by a regex. |
302
|
|
|
* |
303
|
|
|
* @param array $matches |
304
|
|
|
* |
305
|
|
|
* @return string |
306
|
|
|
*/ |
307
|
2 |
|
protected function urlEncodeFirstMatch(array $matches) |
308
|
|
|
{ |
309
|
2 |
|
return rawurlencode($matches[0]); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* {@inheritdoc} |
314
|
|
|
*/ |
315
|
6 |
|
public function withScheme($scheme) |
316
|
|
|
{ |
317
|
6 |
|
if (!is_string($scheme)) { |
318
|
|
|
throw new \InvalidArgumentException('Invalid scheme argument'); |
319
|
|
|
} |
320
|
|
|
|
321
|
6 |
|
$uri = clone $this; |
322
|
6 |
|
$uri->scheme = $scheme; |
323
|
|
|
|
324
|
6 |
|
return $uri; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* {@inheritdoc} |
329
|
|
|
*/ |
330
|
2 |
|
public function withUserInfo($user, $password = null) |
331
|
|
|
{ |
332
|
2 |
|
if (!is_string($user)) { |
333
|
|
|
throw new \InvalidArgumentException('Invalid user argument'); |
334
|
|
|
} |
335
|
|
|
|
336
|
2 |
|
if (null !== $password && !is_string($password)) { |
337
|
|
|
throw new \InvalidArgumentException('Invalid password argument'); |
338
|
|
|
} |
339
|
|
|
|
340
|
2 |
|
$userInfo = $user; |
341
|
2 |
|
if (null !== $password) { |
342
|
2 |
|
$userInfo .= ':' . $password; |
343
|
1 |
|
} |
344
|
|
|
|
345
|
2 |
|
$uri = clone $this; |
346
|
2 |
|
$uri->userInfo = $userInfo; |
347
|
|
|
|
348
|
2 |
|
return $uri; |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* {@inheritdoc} |
353
|
|
|
*/ |
354
|
4 |
View Code Duplication |
public function withHost($host) |
|
|
|
|
355
|
|
|
{ |
356
|
4 |
|
if (!is_string($host)) { |
357
|
|
|
throw new \InvalidArgumentException('Invalid host argument'); |
358
|
|
|
} |
359
|
|
|
|
360
|
4 |
|
$uri = clone $this; |
361
|
4 |
|
$uri->host = $host; |
362
|
|
|
|
363
|
4 |
|
return $uri; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* {@inheritdoc} |
368
|
|
|
*/ |
369
|
4 |
|
public function withPort($port) |
370
|
|
|
{ |
371
|
4 |
|
if (null !== $port) { |
372
|
4 |
|
if (!is_numeric($port)) { |
373
|
|
|
throw new \InvalidArgumentException('Invalid port argument'); |
374
|
|
|
} |
375
|
|
|
|
376
|
4 |
|
$port = (int)$port; |
377
|
2 |
|
} |
378
|
|
|
|
379
|
4 |
|
$uri = clone $this; |
380
|
4 |
|
$uri->port = $port; |
381
|
|
|
|
382
|
4 |
|
return $uri; |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* {@inheritdoc} |
387
|
|
|
*/ |
388
|
6 |
View Code Duplication |
public function withPath($path) |
|
|
|
|
389
|
|
|
{ |
390
|
6 |
|
if (!is_string($path)) { |
391
|
|
|
throw new \InvalidArgumentException('Invalid path argument'); |
392
|
|
|
} |
393
|
|
|
|
394
|
6 |
|
$uri = clone $this; |
395
|
6 |
|
$uri->path = $path; |
396
|
|
|
|
397
|
6 |
|
return $uri; |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* {@inheritdoc} |
402
|
|
|
*/ |
403
|
2 |
|
public function withQuery($query) |
404
|
|
|
{ |
405
|
2 |
|
if (!is_string($query)) { |
406
|
|
|
throw new \InvalidArgumentException('Invalid query argument'); |
407
|
|
|
} |
408
|
|
|
|
409
|
2 |
|
$uri = clone $this; |
410
|
2 |
|
$uri->query = $query; |
411
|
|
|
|
412
|
2 |
|
return $uri; |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
/** |
416
|
|
|
* {@inheritdoc} |
417
|
|
|
*/ |
418
|
2 |
View Code Duplication |
public function withFragment($fragment) |
|
|
|
|
419
|
|
|
{ |
420
|
2 |
|
if (!is_string($fragment)) { |
421
|
|
|
throw new \InvalidArgumentException('Invalid fragment argument'); |
422
|
|
|
} |
423
|
|
|
|
424
|
2 |
|
$uri = clone $this; |
425
|
2 |
|
$uri->fragment = $fragment; |
426
|
|
|
|
427
|
2 |
|
return $uri; |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
/** |
431
|
|
|
* {@inheritdoc} |
432
|
|
|
*/ |
433
|
8 |
|
public function __toString() |
434
|
|
|
{ |
435
|
8 |
|
return $this->toString(); |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
/** |
439
|
|
|
* Convert URI into a string representation. |
440
|
|
|
* |
441
|
|
|
* @return string |
442
|
|
|
*/ |
443
|
8 |
|
public function toString() |
444
|
|
|
{ |
445
|
8 |
|
$uri = $this->getScheme(); |
446
|
8 |
|
if (strlen($uri) > 0) { |
447
|
6 |
|
$uri .= ':'; |
448
|
3 |
|
} |
449
|
|
|
|
450
|
8 |
|
$authority = $this->getAuthority(); |
451
|
8 |
|
$path = $this->getPath(); |
452
|
8 |
|
if (strlen($authority) > 0) { |
453
|
4 |
|
if (strlen($path) > 0) { |
454
|
2 |
|
$path = '/' . ltrim($path, '/'); |
455
|
1 |
|
} |
456
|
|
|
|
457
|
4 |
|
$uri .= '//' . $authority; |
458
|
2 |
|
} |
459
|
|
|
|
460
|
8 |
|
$uri .= $path; |
461
|
|
|
|
462
|
8 |
|
$query = $this->getQuery(); |
463
|
8 |
|
if (strlen($query) > 0) { |
464
|
2 |
|
$uri .= '?' . $query; |
465
|
1 |
|
} |
466
|
|
|
|
467
|
8 |
|
$fragment = $this->getFragment(); |
468
|
8 |
|
if (strlen($fragment) > 0) { |
469
|
2 |
|
$uri .= '#' . $fragment; |
470
|
1 |
|
} |
471
|
|
|
|
472
|
8 |
|
return $uri; |
473
|
|
|
} |
474
|
|
|
} |
475
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.