forked from thephpleague/uri
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuilder.php
More file actions
358 lines (304 loc) · 10.1 KB
/
Builder.php
File metadata and controls
358 lines (304 loc) · 10.1 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
<?php
/**
* League.Uri (https://uri.thephpleague.com)
*
* (c) Ignace Nyamagana Butera <nyamsprod@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace League\Uri;
use BackedEnum;
use League\Uri\Contracts\Conditionable;
use League\Uri\Contracts\FragmentDirective;
use League\Uri\Contracts\Transformable;
use League\Uri\Contracts\UriComponentInterface;
use League\Uri\Exceptions\SyntaxError;
use SensitiveParameter;
use Stringable;
use Throwable;
use TypeError;
use Uri\Rfc3986\Uri as Rfc3986Uri;
use Uri\WhatWg\Url as WhatWgUrl;
use function is_bool;
use function str_replace;
use function strpos;
final class Builder implements Conditionable, Transformable
{
private ?string $scheme = null;
private ?string $username = null;
private ?string $password = null;
private ?string $host = null;
private ?int $port = null;
private ?string $path = null;
private ?string $query = null;
private ?string $fragment = null;
public function __construct(
BackedEnum|Stringable|string|null $scheme = null,
BackedEnum|Stringable|string|null $username = null,
#[SensitiveParameter] BackedEnum|Stringable|string|null $password = null,
BackedEnum|Stringable|string|null $host = null,
BackedEnum|int|null $port = null,
BackedEnum|Stringable|string|null $path = null,
BackedEnum|Stringable|string|null $query = null,
BackedEnum|Stringable|string|null $fragment = null,
) {
$this
->scheme($scheme)
->userInfo($username, $password)
->host($host)
->port($port)
->path($path)
->query($query)
->fragment($fragment);
}
/**
* @throws SyntaxError
*/
public function scheme(BackedEnum|Stringable|string|null $scheme): self
{
$scheme = $this->filterString($scheme);
if ($scheme !== $this->scheme) {
UriString::isValidScheme($scheme) || throw new SyntaxError('The scheme `'.$scheme.'` is invalid.');
$this->scheme = $scheme;
}
return $this;
}
/**
* @throws SyntaxError
*/
public function userInfo(
BackedEnum|Stringable|string|null $user,
#[SensitiveParameter] BackedEnum|Stringable|string|null $password = null
): static {
$username = Encoder::encodeUser($this->filterString($user));
$password = Encoder::encodePassword($this->filterString($password));
if ($username !== $this->username || $password !== $this->password) {
$this->username = $username;
$this->password = $password;
}
return $this;
}
/**
* @throws SyntaxError
*/
public function host(BackedEnum|Stringable|string|null $host): self
{
$host = $this->filterString($host);
if ($host !== $this->host) {
null === $host
|| HostRecord::isValid($host)
|| throw new SyntaxError('The host `'.$host.'` is invalid.');
$this->host = $host;
}
return $this;
}
/**
* @throws SyntaxError
* @throws TypeError
*/
public function port(BackedEnum|int|null $port): self
{
if ($port instanceof BackedEnum) {
1 === preg_match('/^\d+$/', (string) $port->value)
|| throw new TypeError('The port must be a valid BackedEnum containing a number.');
$port = (int) $port->value;
}
if ($port !== $this->port) {
null === $port
|| ($port >= 0 && $port < 65535)
|| throw new SyntaxError('The port value must be null or an integer between 0 and 65535.');
$this->port = $port;
}
return $this;
}
/**
* @throws SyntaxError
*/
public function authority(BackedEnum|Stringable|string|null $authority): self
{
['user' => $user, 'pass' => $pass, 'host' => $host, 'port' => $port] = UriString::parseAuthority($authority);
return $this
->userInfo($user, $pass)
->host($host)
->port($port);
}
/**
* @throws SyntaxError
*/
public function path(BackedEnum|Stringable|string|null $path): self
{
$path = $this->filterString($path);
if ($path !== $this->path) {
$this->path = null !== $path ? Encoder::encodePath($path) : null;
}
return $this;
}
/**
* @throws SyntaxError
*/
public function query(BackedEnum|Stringable|string|null $query): self
{
$query = $this->filterString($query);
if ($query !== $this->query) {
$this->query = Encoder::encodeQueryOrFragment($query);
}
return $this;
}
/**
* @throws SyntaxError
*/
public function fragment(BackedEnum|Stringable|string|null $fragment): self
{
$fragment = $this->filterString($fragment);
if ($fragment !== $this->fragment) {
$this->fragment = Encoder::encodeQueryOrFragment($fragment);
}
return $this;
}
/**
* Puts back the Builder in a freshly created state.
*/
public function reset(): self
{
$this->scheme = null;
$this->username = null;
$this->password = null;
$this->host = null;
$this->port = null;
$this->path = null;
$this->query = null;
$this->fragment = null;
return $this;
}
/**
* Executes the given callback with the current instance
* and returns the current instance.
*
* @param callable(self): self $callback
*/
public function transform(callable $callback): static
{
return $callback($this);
}
public function when(callable|bool $condition, callable $onSuccess, ?callable $onFail = null): static
{
if (!is_bool($condition)) {
$condition = $condition($this);
}
return match (true) {
$condition => $onSuccess($this),
null !== $onFail => $onFail($this),
default => $this,
} ?? $this;
}
/**
* @throws SyntaxError if the URI can not be build with the current Builder state
*/
public function guard(Rfc3986Uri|WhatWgUrl|BackedEnum|Stringable|string|null $baseUri = null): self
{
try {
$this->build($baseUri);
return $this;
} catch (Throwable $exception) {
throw new SyntaxError('The current builder cannot generate a valid URI.', previous: $exception);
}
}
/**
* Tells whether the URI can be built with the current Builder state.
*/
public function validate(Rfc3986Uri|WhatWgUrl|BackedEnum|Stringable|string|null $baseUri = null): bool
{
try {
$this->build($baseUri);
return true;
} catch (Throwable) {
return false;
}
}
public function build(Rfc3986Uri|WhatWgUrl|BackedEnum|Stringable|string|null $baseUri = null): Uri
{
$authority = $this->buildAuthority();
$path = $this->buildPath($authority);
$uriString = UriString::buildUri(
$this->scheme,
$authority,
$path,
Encoder::encodeQueryOrFragment($this->query),
Encoder::encodeQueryOrFragment($this->fragment)
);
return Uri::new(null === $baseUri ? $uriString : UriString::resolve($uriString, match (true) {
$baseUri instanceof Rfc3986Uri => $baseUri->toString(),
$baseUri instanceof WhatWgUrl => $baseUri->toAsciiString(),
default => $baseUri,
}));
}
/**
* @throws SyntaxError
*/
private function buildAuthority(): ?string
{
if (null === $this->host) {
(null === $this->username && null === $this->password && null === $this->port)
|| throw new SyntaxError('The User Information and/or the Port component(s) are set without a Host component being present.');
return null;
}
$authority = $this->host;
if (null !== $this->username || null !== $this->password) {
$userInfo = Encoder::encodeUser($this->username);
if (null !== $this->password) {
$userInfo .= ':'.Encoder::encodePassword($this->password);
}
$authority = $userInfo.'@'.$authority;
}
if (null !== $this->port) {
return $authority.':'.$this->port;
}
return $authority;
}
/**
* @throws SyntaxError
*/
private function buildPath(?string $authority): ?string
{
if (null === $this->path || '' === $this->path) {
return $this->path;
}
$path = Encoder::encodePath($this->path);
if (null !== $authority) {
return str_starts_with($path, '/') ? $path : '/'.$path;
}
if (str_starts_with($path, '//')) {
return '/.'.$path;
}
$colonPos = strpos($path, ':');
if (false !== $colonPos && null === $this->scheme) {
$slashPos = strpos($path, '/');
(false !== $slashPos && $colonPos > $slashPos) || throw new SyntaxError('In absence of the scheme and authority components, the first path segment cannot contain a colon (":") character.');
}
return $path;
}
/**
* Filter a string.
*
* @throws SyntaxError if the submitted data cannot be converted to string
*/
private function filterString(BackedEnum|Stringable|string|null $str): ?string
{
$str = match (true) {
$str instanceof FragmentDirective => $str->toFragmentValue(),
$str instanceof UriComponentInterface => $str->value(),
$str instanceof BackedEnum => (string) $str->value,
null === $str => null,
default => (string) $str,
};
if (null === $str) {
return null;
}
$str = str_replace(' ', '%20', $str);
return UriString::containsRfc3987Chars($str)
? $str
: throw new SyntaxError('The component value `'.$str.'` contains invalid characters.');
}
}