Debian Codegraph @

lib/3rdParty/composer/illuminate/support/Uri.php

raw symbols SCIP
1 <?php
2
3 namespace Illuminate\Support;
4
5 use Closure;
6 use Illuminate\Contracts\Routing\UrlRoutable;
7 use Illuminate\Contracts\Support\Htmlable;
8 use Illuminate\Contracts\Support\Responsable;
9 use Illuminate\Http\RedirectResponse;
10 use Illuminate\Support\Traits\Conditionable;
11 use Illuminate\Support\Traits\Dumpable;
12 use Illuminate\Support\Traits\Macroable;
13 use Illuminate\Support\Traits\Tappable;
14 use JsonSerializable;
15 use League\Uri\Contracts\UriInterface;
16 use League\Uri\Uri as LeagueUri;
17 use SensitiveParameter;
18 use Stringable;
19
20 class Uri implements Htmlable, JsonSerializable, Responsable, Stringable
21 {
22 use Conditionable, Dumpable, Macroable, Tappable;
23
24 /**
25 * The URI instance.
26 */
27 protected UriInterface $uri;
28
29 /**
30 * The URL generator resolver.
31 */
32 protected static ?Closure $urlGeneratorResolver = null;
33
34 /**
35 * Create a new parsed URI instance.
36 */
37 public function __construct(UriInterface|Stringable|string $uri = '')
38 {
39 $this->uri = $uri instanceof UriInterface ? $uri : LeagueUri::new((string) $uri);
40 }
41
42 /**
43 * Create a new URI instance.
44 */
45 public static function of(UriInterface|Stringable|string $uri = ''): static
46 {
47 return new static($uri);
48 }
49
50 /**
51 * Get a URI instance of an absolute URL for the given path.
52 */
53 public static function to(string $path): static
54 {
55 return new static(call_user_func(static::$urlGeneratorResolver)->to($path));
56 }
57
58 /**
59 * Get a URI instance for a named route.
60 *
61 * @param \BackedEnum|string $name
62 * @param mixed $parameters
63 * @param bool $absolute
64 * @return static
65 *
66 * @throws \Symfony\Component\Routing\Exception\RouteNotFoundException|\InvalidArgumentException
67 */
68 public static function route($name, $parameters = [], $absolute = true): static
69 {
70 return new static(call_user_func(static::$urlGeneratorResolver)->route($name, $parameters, $absolute));
71 }
72
73 /**
74 * Create a signed route URI instance for a named route.
75 *
76 * @param \BackedEnum|string $name
77 * @param mixed $parameters
78 * @param \DateTimeInterface|\DateInterval|int|null $expiration
79 * @param bool $absolute
80 * @return static
81 *
82 * @throws \InvalidArgumentException
83 */
84 public static function signedRoute($name, $parameters = [], $expiration = null, $absolute = true): static
85 {
86 return new static(call_user_func(static::$urlGeneratorResolver)->signedRoute($name, $parameters, $expiration, $absolute));
87 }
88
89 /**
90 * Create a temporary signed route URI instance for a named route.
91 *
92 * @param \BackedEnum|string $name
93 * @param \DateTimeInterface|\DateInterval|int $expiration
94 * @param array $parameters
95 * @param bool $absolute
96 * @return static
97 */
98 public static function temporarySignedRoute($name, $expiration, $parameters = [], $absolute = true): static
99 {
100 return static::signedRoute($name, $parameters, $expiration, $absolute);
101 }
102
103 /**
104 * Get a URI instance for a controller action.
105 *
106 * @param string|array $action
107 * @param mixed $parameters
108 * @param bool $absolute
109 * @return static
110 *
111 * @throws \InvalidArgumentException
112 */
113 public static function action($action, $parameters = [], $absolute = true): static
114 {
115 return new static(call_user_func(static::$urlGeneratorResolver)->action($action, $parameters, $absolute));
116 }
117
118 /**
119 * Get the URI's authority.
120 */
121 public function authority(): ?string
122 {
123 return $this->uri->getAuthority();
124 }
125
126 /**
127 * Get the URI's scheme.
128 */
129 public function scheme(): ?string
130 {
131 return $this->uri->getScheme();
132 }
133
134 /**
135 * Get the user from the URI.
136 */
137 public function user(bool $withPassword = false): ?string
138 {
139 return $withPassword
140 ? $this->uri->getUserInfo()
141 : $this->uri->getUsername();
142 }
143
144 /**
145 * Get the password from the URI.
146 */
147 public function password(): ?string
148 {
149 return $this->uri->getPassword();
150 }
151
152 /**
153 * Get the URI's host.
154 */
155 public function host(): ?string
156 {
157 return $this->uri->getHost();
158 }
159
160 /**
161 * Get the URI's port.
162 */
163 public function port(): ?int
164 {
165 return $this->uri->getPort();
166 }
167
168 /**
169 * Get the URI's path.
170 *
171 * Empty or missing paths are returned as a single "/".
172 *
173 * @return non-empty-string
174 */
175 public function path(): string
176 {
177 $path = trim((string) $this->uri->getPath(), '/');
178
179 return $path === '' ? '/' : $path;
180 }
181
182 /**
183 * Get the URI's path segments.
184 *
185 * Empty or missing paths are returned as an empty collection.
186 */
187 public function pathSegments(): Collection
188 {
189 $path = $this->path();
190
191 return $path === '/' ? new Collection : new Collection(explode('/', $path));
192 }
193
194 /**
195 * Get the URI's query string.
196 */
197 public function query(): UriQueryString
198 {
199 return new UriQueryString($this);
200 }
201
202 /**
203 * Get the URI's fragment.
204 */
205 public function fragment(): ?string
206 {
207 return $this->uri->getFragment();
208 }
209
210 /**
211 * Specify the scheme of the URI.
212 */
213 public function withScheme(Stringable|string $scheme): static
214 {
215 return new static($this->uri->withScheme($scheme));
216 }
217
218 /**
219 * Specify the user and password for the URI.
220 */
221 public function withUser(Stringable|string|null $user, #[SensitiveParameter] Stringable|string|null $password = null): static
222 {
223 return new static($this->uri->withUserInfo($user, $password));
224 }
225
226 /**
227 * Specify the host of the URI.
228 */
229 public function withHost(Stringable|string $host): static
230 {
231 return new static($this->uri->withHost($host));
232 }
233
234 /**
235 * Specify the port of the URI.
236 */
237 public function withPort(?int $port): static
238 {
239 return new static($this->uri->withPort($port));
240 }
241
242 /**
243 * Specify the path of the URI.
244 */
245 public function withPath(Stringable|string $path): static
246 {
247 return new static($this->uri->withPath(Str::start((string) $path, '/')));
248 }
249
250 /**
251 * Merge new query parameters into the URI.
252 */
253 public function withQuery(array $query, bool $merge = true): static
254 {
255 foreach ($query as $key => $value) {
256 if ($value instanceof UrlRoutable) {
257 $query[$key] = $value->getRouteKey();
258 }
259 }
260
261 if ($merge) {
262 $mergedQuery = $this->query()->all();
263
264 foreach ($query as $key => $value) {
265 data_set($mergedQuery, $key, $value);
266 }
267
268 $newQuery = $mergedQuery;
269 } else {
270 $newQuery = [];
271
272 foreach ($query as $key => $value) {
273 data_set($newQuery, $key, $value);
274 }
275 }
276
277 return new static($this->uri->withQuery(Arr::query($newQuery) ?: null));
278 }
279
280 /**
281 * Merge new query parameters into the URI if they are not already in the query string.
282 */
283 public function withQueryIfMissing(array $query): static
284 {
285 $currentQuery = $this->query();
286
287 foreach ($query as $key => $value) {
288 if (! $currentQuery->missing($key)) {
289 Arr::forget($query, $key);
290 }
291 }
292
293 return $this->withQuery($query);
294 }
295
296 /**
297 * Push a value onto the end of a query string parameter that is a list.
298 */
299 public function pushOntoQuery(string $key, mixed $value): static
300 {
301 $currentValue = data_get($this->query()->all(), $key);
302
303 $values = Arr::wrap($value);
304
305 return $this->withQuery([$key => match (true) {
306 is_array($currentValue) && array_is_list($currentValue) => array_values(array_unique([...$currentValue, ...$values])),
307 is_array($currentValue) => [...$currentValue, ...$values],
308 ! is_null($currentValue) => [$currentValue, ...$values],
309 default => $values,
310 }]);
311 }
312
313 /**
314 * Remove the given query parameters from the URI.
315 */
316 public function withoutQuery(array|string $keys): static
317 {
318 return $this->replaceQuery(Arr::except($this->query()->all(), $keys));
319 }
320
321 /**
322 * Specify new query parameters for the URI.
323 */
324 public function replaceQuery(array $query): static
325 {
326 return $this->withQuery($query, merge: false);
327 }
328
329 /**
330 * Specify the fragment of the URI.
331 */
332 public function withFragment(string $fragment): static
333 {
334 return new static($this->uri->withFragment($fragment));
335 }
336
337 /**
338 * Create a redirect HTTP response for the given URI.
339 */
340 public function redirect(int $status = 302, array $headers = []): RedirectResponse
341 {
342 return new RedirectResponse($this->value(), $status, $headers);
343 }
344
345 /**
346 * Get the URI as a Stringable instance.
347 *
348 * @return \Illuminate\Support\Stringable
349 */
350 public function toStringable()
351 {
352 return Str::of($this->value());
353 }
354
355 /**
356 * Create an HTTP response that represents the URI object.
357 *
358 * @param \Illuminate\Http\Request $request
359 * @return \Symfony\Component\HttpFoundation\Response
360 */
361 public function toResponse($request)
362 {
363 return new RedirectResponse($this->value());
364 }
365
366 /**
367 * Get the URI as a string of HTML.
368 *
369 * @return string
370 */
371 public function toHtml()
372 {
373 return $this->value();
374 }
375
376 /**
377 * Get the decoded string representation of the URI.
378 */
379 public function decode(): string
380 {
381 if (empty($this->query()->toArray())) {
382 return $this->value();
383 }
384
385 return Str::replace(Str::after($this->value(), '?'), $this->query()->decode(), $this->value());
386 }
387
388 /**
389 * Get the string representation of the URI.
390 */
391 public function value(): string
392 {
393 return (string) $this;
394 }
395
396 /**
397 * Determine if the URI is currently an empty string.
398 */
399 public function isEmpty(): bool
400 {
401 return trim($this->value()) === '';
402 }
403
404 /**
405 * Dump the string representation of the URI.
406 *
407 * @param mixed ...$args
408 * @return $this
409 */
410 public function dump(...$args)
411 {
412 dump($this->value(), ...$args);
413
414 return $this;
415 }
416
417 /**
418 * Set the URL generator resolver.
419 */
420 public static function setUrlGeneratorResolver(Closure $urlGeneratorResolver): void
421 {
422 static::$urlGeneratorResolver = $urlGeneratorResolver;
423 }
424
425 /**
426 * Get the underlying URI instance.
427 */
428 public function getUri(): UriInterface
429 {
430 return $this->uri;
431 }
432
433 /**
434 * Convert the object into a value that is JSON serializable.
435 *
436 * @return string
437 */
438 public function jsonSerialize(): string
439 {
440 return $this->value();
441 }
442
443 /**
444 * Get the string representation of the URI.
445 */
446 public function __toString(): string
447 {
448 return $this->uri->toString();
449 }
450 }