-
Notifications
You must be signed in to change notification settings - Fork 162
/
Uri.php
347 lines (299 loc) · 8.33 KB
/
Uri.php
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
<?php
namespace Illuminate\Support;
use Closure;
use Illuminate\Contracts\Routing\UrlRoutable;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Contracts\Support\Responsable;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Traits\Conditionable;
use Illuminate\Support\Traits\Tappable;
use League\Uri\Contracts\UriInterface;
use League\Uri\Uri as LeagueUri;
use SensitiveParameter;
use Stringable;
class Uri implements Htmlable, Responsable
{
use Conditionable, Tappable;
/**
* The URI instance.
*/
protected UriInterface $uri;
/**
* The URL generator resolver.
*/
protected static ?Closure $urlGeneratorResolver = null;
/**
* Create a new parsed URI instance.
*/
public function __construct(UriInterface|Stringable|string $uri = '')
{
$this->uri = $uri instanceof UriInterface ? $uri : LeagueUri::new((string) $uri);
}
/**
* Create a new URI instance.
*/
public static function of(UriInterface|Stringable|string $uri = ''): static
{
return new static($uri);
}
/**
* Get a URI instance of an absolute URL for the given path.
*/
public static function to(string $path): static
{
return new static(call_user_func(static::$urlGeneratorResolver)->to($path));
}
/**
* Get a URI instance for a named route.
*
* @param \BackedEnum|string $name
* @param mixed $parameters
* @param bool $absolute
* @return static
*
* @throws \Symfony\Component\Routing\Exception\RouteNotFoundException|\InvalidArgumentException
*/
public static function route($name, $parameters = [], $absolute = true): static
{
return new static(call_user_func(static::$urlGeneratorResolver)->route($name, $parameters, $absolute));
}
/**
* Get the URI's scheme.
*/
public function scheme(): ?string
{
return $this->uri->getScheme();
}
/**
* Get the user from the URI.
*/
public function user(bool $withPassword = false): ?string
{
return $withPassword
? $this->uri->getUserInfo()
: $this->uri->getUsername();
}
/**
* Get the password from the URI.
*/
public function password(): ?string
{
return $this->uri->getPassword();
}
/**
* Get the URI's host.
*/
public function host(): ?string
{
return $this->uri->getHost();
}
/**
* Get the URI's port.
*/
public function port(): ?int
{
return $this->uri->getPort();
}
/**
* Get the URI's path.
*
* Empty or missing paths are returned as a single "/".
*/
public function path(): ?string
{
$path = trim((string) $this->uri->getPath(), '/');
return $path === '' ? '/' : $path;
}
/**
* Get the URI's query string.
*/
public function query(): UriQueryString
{
return new UriQueryString($this);
}
/**
* Get the URI's fragment.
*/
public function fragment(): ?string
{
return $this->uri->getFragment();
}
/**
* Specify the scheme of the URI.
*/
public function withScheme(Stringable|string $scheme): static
{
return new static($this->uri->withScheme($scheme));
}
/**
* Specify the user and password for the URI.
*/
public function withUser(Stringable|string|null $user, #[SensitiveParameter] Stringable|string|null $password = null): static
{
return new static($this->uri->withUserInfo($user, $password));
}
/**
* Specify the host of the URI.
*/
public function withHost(Stringable|string $host): static
{
return new static($this->uri->withHost($host));
}
/**
* Specify the port of the URI.
*/
public function withPort(int|null $port): static
{
return new static($this->uri->withPort($port));
}
/**
* Specify the path of the URI.
*/
public function withPath(Stringable|string $path): static
{
return new static($this->uri->withPath(Str::start((string) $path, '/')));
}
/**
* Merge new query parameters into the URI.
*/
public function withQuery(array $query, bool $merge = true): static
{
foreach ($query as $key => $value) {
if ($value instanceof UrlRoutable) {
$query[$key] = $value->getRouteKey();
}
}
if ($merge) {
$mergedQuery = $this->query()->all();
foreach ($query as $key => $value) {
data_set($mergedQuery, $key, $value);
}
$newQuery = $mergedQuery;
} else {
$newQuery = [];
foreach ($query as $key => $value) {
data_set($newQuery, $key, $value);
}
}
return new static($this->uri->withQuery(Arr::query($newQuery)));
}
/**
* Merge new query parameters into the URI if they are not already in the query string.
*/
public function withQueryIfMissing(array $query): static
{
$currentQuery = $this->query();
foreach ($query as $key => $value) {
if (! $currentQuery->missing($key)) {
Arr::forget($query, $key);
}
}
return $this->withQuery($query);
}
/**
* Push a value onto the end of a query string parameter that is a list.
*/
public function pushOntoQuery(string $key, mixed $value): static
{
$currentValue = data_get($this->query()->all(), $key);
$values = Arr::wrap($value);
return $this->withQuery([$key => match (true) {
is_array($currentValue) && array_is_list($currentValue) => array_values(array_unique([...$currentValue, ...$values])),
is_array($currentValue) => [...$currentValue, ...$values],
! is_null($currentValue) => [$currentValue, ...$values],
default => $values,
}]);
}
/**
* Remove the given query parameters from the URI.
*/
public function withoutQuery(array $keys): static
{
return $this->replaceQuery(Arr::except($this->query()->all(), $keys));
}
/**
* Specify new query parameters for the URI.
*/
public function replaceQuery(array $query): static
{
return $this->withQuery($query, merge: false);
}
/**
* Specify the fragment of the URI.
*/
public function withFragment(string $fragment): static
{
return new static($this->uri->withFragment($fragment));
}
/**
* Create a redirect HTTP response for the given URI.
*/
public function redirect(int $status = 302, array $headers = []): RedirectResponse
{
return new RedirectResponse($this->value(), $status, $headers);
}
/**
* Create an HTTP response that represents the object.
*
* @param \Illuminate\Http\Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function toResponse($request)
{
return new RedirectResponse($this->value());
}
/**
* Get content as a string of HTML.
*
* @return string
*/
public function toHtml()
{
return $this->value();
}
/**
* Get the decoded string representation of the URI.
*/
public function decode(): string
{
if (empty($this->query()->toArray())) {
return $this->value();
}
return Str::replace(Str::after($this->value(), '?'), $this->query()->decode(), $this->value());
}
/**
* Get the string representation of the URI.
*/
public function value(): string
{
return (string) $this;
}
/**
* Determine if the URI is currently an empty string.
*/
public function isEmpty(): bool
{
return trim($this->value()) === '';
}
/**
* Set the URL generator resolver.
*/
public static function setUrlGeneratorResolver(Closure $urlGeneratorResolver): void
{
static::$urlGeneratorResolver = $urlGeneratorResolver;
}
/**
* Get the underlying URI instance.
*/
public function getUri(): UriInterface
{
return $this->uri;
}
/**
* Get the string representation of the URI.
*/
public function __toString(): string
{
return $this->uri->toString();
}
}