-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUri.php
More file actions
479 lines (450 loc) · 14 KB
/
Copy pathUri.php
File metadata and controls
479 lines (450 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
<?php
/**
* This file is part of HttpMessage
*
* @package bdk/http-message
* @author Brad Kent <bkfake-github@yahoo.com>
* @license http://opensource.org/licenses/MIT MIT
* @copyright 2014-2024 Brad Kent
* @version v1.0
*/
namespace bdk\HttpMessage;
use bdk\HttpMessage\AbstractUri;
use bdk\HttpMessage\Utility\Uri as UriUtil;
use InvalidArgumentException;
use Psr\Http\Message\UriInterface;
/**
* Value object representing a URI.
*/
class Uri extends AbstractUri implements UriInterface
{
/** @var string Uri scheme. */
private $scheme = '';
/** @var string The URI user information, in "username[:password]" format. (or empty string) */
private $userInfo = '';
/** @var string Uri host. */
private $host = '';
/** @var null|int Uri port. */
private $port;
/** @var string Uri path. */
private $path = '';
/** @var string Uri query string. */
private $query = '';
/** @var string Uri fragment. */
private $fragment = '';
/**
* Constructor
*
* @param string|null $uri Uri to wrap
*
* @throws InvalidArgumentException
*/
public function __construct($uri = null)
{
if ($uri === null) {
$uri = '';
}
$this->assertString($uri, 'uri');
if ($uri === '') {
return;
}
$parts = UriUtil::parseUrl($uri);
if ($parts === false) {
throw new InvalidArgumentException('Unable to parse URI: ' . $uri);
}
$this->setUrlParts($parts);
}
/**
* Return stringified value
*
* @return string
*/
public function __toString(): string
{
$uri = '';
if ($this->scheme !== '') {
$uri .= $this->scheme . ':';
}
$authority = $this->getAuthority();
if ($authority !== '') {
$uri .= '//' . $authority;
}
$uri .= self::createUriPath($authority, $this->path);
if ($this->query !== '') {
$uri .= '?' . $this->query;
}
if ($this->fragment !== '') {
$uri .= '#' . $this->fragment;
}
return $uri;
}
/**
* Get a Uri populated with values from $_SERVER.
*
* @return self
*
* @SuppressWarnings(PHPMD.Superglobals)
*/
public static function fromGlobals(): UriInterface
{
return UriUtil::fromGlobals();
}
/**
* Retrieve the scheme component of the URI.
*
* @return string
*/
public function getScheme(): string
{
return $this->scheme;
}
/**
* Retrieve the authority component of the URI.
*
* If the port component is not set or is the standard port for the current
* scheme, it will not be included
*
* @see https://datatracker.ietf.org/doc/html/rfc3986#section-3.2
*
* @return string The URI authority, in "[user-info@]host[:port]" format. (or empty string)
*/
public function getAuthority(): string
{
if ($this->host === '') {
return '';
}
$authority = $this->host;
if ($this->userInfo !== '') {
$authority = $this->userInfo . '@' . $authority;
}
$port = $this->getPort();
if ($port !== null) {
$authority .= ':' . $port;
}
return $authority;
}
/**
* Retrieve the user information component of the URI.
*
* If a user is present in the URI, this will return that value;
* additionally, if the password is also present, it will be appended to the
* user value, with a colon (":") separating the values.
*
* The trailing "@" character is not part of the user information and will not be included
*
* @return string The URI user information, in "username[:password]" format. (or empty string)
*/
public function getUserInfo(): string
{
return $this->userInfo;
}
/**
* Retrieve the host component of the URI.
*
* The value returned will be normalized to lowercase, per RFC 3986
* Section 3.2.2.
*
* @see https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2
*
* @return string The URI host (or empty string).
*/
public function getHost(): string
{
return $this->host;
}
/**
* Retrieve the port component of the URI.
*
* If the port is the standard port used with the current scheme, null will be returned.
*
* If no port is present, and no scheme is present, null will be returned
*
* If no port is present, but a scheme is present, null will be returned.
*
* @return null|int The URI port.
*/
public function getPort(): ?int
{
return $this->isStandardPort($this->scheme, $this->port)
? null
: $this->port;
}
/**
* Retrieve the path component of the URI.
*
* The path can either be
* empty or
* absolute (starting with a slash)
* rootless (not starting with a slash).
*
* Normally, the empty path "" and absolute path "/" are considered equal as
* defined in RFC 7230 Section 2.7.3. But this method does automatically
* do this normalization because in contexts with a trimmed base path, e.g.
* the front controller, this difference becomes significant.
* It's the task of the user to handle both "" and "/".
*
* The value returned will be percent-encoded, but will not double-encode
* any characters.
* see RFC 3986, Sections 2 and 3.3.
*
* As an example, if the value should include a slash ("/") not intended as
* delimiter between path segments, that value will be passed in encoded
* form (e.g., "%2F") to the instance.
*
* @see https://datatracker.ietf.org/doc/html/rfc3986#section-2
* @see https://datatracker.ietf.org/doc/html/rfc3986#section-3.3
*
* @return string The URI path.
*/
public function getPath(): string
{
return $this->path;
}
/**
* Retrieve the query string of the URI.
*
* The leading "?" character is not part of the query and will not be
* included.
*
* The value returned will be percent-encoded,
* but will not double-encode any characters.
* see RFC 3986, Sections 2 and 3.4.
*
* As an example, if a value in a key/value pair of the query string should
* include an ampersand ("&") not intended as a delimiter between values,
* that value MUST be passed in encoded form (e.g., "%26") to the instance.
*
* @see https://datatracker.ietf.org/doc/html/rfc3986#section-2
* @see https://datatracker.ietf.org/doc/html/rfc3986#section-3.4
*
* @return string The URI query string. (or empty string)
*/
public function getQuery(): string
{
return $this->query;
}
/**
* Retrieve the fragment component of the URI.
*
* The leading "#" character is not part of the fragment and MUST NOT be
* added.
*
* The value returned will be percent-encoded,
* but will not double-encode any characters.
* see RFC 3986, Sections 2 and 3.5.
*
* @see https://datatracker.ietf.org/doc/html/rfc3986#section-2
* @see https://datatracker.ietf.org/doc/html/rfc3986#section-3.5
*
* @return string The URI fragment. (or empty string)
*/
public function getFragment(): string
{
return $this->fragment;
}
/**
* Return an instance with the specified scheme.
*
* An empty scheme is equivalent to removing the scheme.
*
* @param string $scheme The scheme to use with the new instance.
*
* @return static A new instance with the specified scheme.
*
* @throws InvalidArgumentException for invalid schemes.
* @throws InvalidArgumentException for unsupported schemes.
*/
public function withScheme(string $scheme): static
{
$this->assertScheme($scheme);
$scheme = self::lowercase($scheme);
if ($scheme === $this->scheme) {
return $this;
}
$new = clone $this;
$new->scheme = $scheme;
$new->port = $new->filterPort($new->port);
return $new;
}
/**
* Return an instance with the specified user information.
*
* Password is optional, but the user information MUST include the
* user; an empty string for the user is equivalent to removing user
* information.
*
* @param string $user The user name to use for authority.
* @param null|string $password The password associated with $user.
*
* @return static A new instance with the specified user information.
*/
public function withUserInfo(string $user, ?string $password = null): static
{
$this->assertString($user, 'user');
$info = $user;
if ($password !== null && $password !== '') {
$this->assertString($password, 'password');
$info .= ':' . $password;
}
if ($info === $this->userInfo) {
return $this;
}
$new = clone $this;
$new->userInfo = $info;
return $new;
}
/**
* Return an instance with the specified host.
*
* An empty host value is equivalent to removing the host.
*
* @param string $host The hostname to use with the new instance.
*
* @return static A new instance with the specified host.
*
* @throws InvalidArgumentException for invalid hostnames.
*/
public function withHost(string $host): static
{
$this->assertHost($host);
$host = self::lowercase($host);
if ($host === $this->host) {
return $this;
}
$new = clone $this;
$new->host = $host;
return $new;
}
/**
* Return an instance with the specified port.
*
* A null value provided for the port is equivalent to removing the port information.
*
* @param null|int $port The port to use with the new instance;
* a null value removes the port information.
*
* @return static A new instance with the specified port.
* @throws InvalidArgumentException for invalid ports.
*/
public function withPort(?int $port): static
{
$port = $this->filterPort($port);
if ($port === $this->port) {
return $this;
}
$new = clone $this;
$new->port = $port;
return $new;
}
/**
* Return an instance with the specified path.
*
* The path can either be
* empty
* absolute (starting with a slash)
* rootless (not starting with a slash).
*
* If an HTTP path is intended to be host-relative rather than path-relative
* then it must begin with a slash ("/"). HTTP paths not starting with a slash
* are assumed to be relative to some base path known to the application or
* consumer.
*
* Users can provide both encoded and decoded path characters.
*
* @param string $path The path to use with the new instance.
*
* @return static A new instance with the specified path.
*
* @throws InvalidArgumentException for invalid paths.
*/
public function withPath(string $path): static
{
$path = $this->filterPath($path);
if ($path === $this->path) {
return $this;
}
$new = clone $this;
$new->path = $path;
return $new;
}
/**
* Return an instance with the specified query string.
*
* Users can provide both encoded and decoded query characters.
*
* An empty query string value is equivalent to removing the query string.
*
* @param string $query The query string to use with the new instance.
*
* @return static A new instance with the specified query string.
* @throws InvalidArgumentException for invalid query strings.
*/
public function withQuery(string $query): static
{
$this->assertString($query, 'query');
$query = $this->filterQueryAndFragment($query);
if ($query === $this->query) {
return $this;
}
$new = clone $this;
$new->query = $query;
return $new;
}
/**
* Return an instance with the specified URI fragment.
*
* Users can provide both encoded and decoded fragment characters.
*
* An empty fragment value is equivalent to removing the fragment.
*
* @param string $fragment The fragment to use with the new instance.
*
* @return static A new instance with the specified fragment.
*/
public function withFragment(string $fragment): static
{
$this->assertString($fragment, 'fragment');
$fragment = $this->filterQueryAndFragment($fragment);
if ($fragment === $this->fragment) {
return $this;
}
$new = clone $this;
$new->fragment = $fragment;
return $new;
}
/**
* Set properties from parsed url
*
* @param array<string,int|string> $urlParts Url parts parsed from parse_url
*
* @return void
*/
private function setUrlParts(array $urlParts)
{
$asserts = \array_intersect_key(array(
'scheme' => 'assertScheme',
), $urlParts);
$filters = \array_intersect_key(array(
'fragment' => 'filterQueryAndFragment',
'host' => 'lowercase',
'path' => 'filterPath',
'port' => 'filterPort',
'query' => 'filterQueryAndFragment',
'scheme' => 'lowercase',
), $urlParts);
foreach ($asserts as $part => $method) {
$val = $urlParts[$part];
$this->{$method}($val);
}
foreach ($filters as $part => $method) {
$val = $urlParts[$part];
$this->{$part} = $this->{$method}($val);
}
if (isset($urlParts['user'])) {
$this->userInfo = (string) $urlParts['user'];
}
if (isset($urlParts['pass'])) {
$this->userInfo .= ':' . $urlParts['pass'];
}
}
}