-
Notifications
You must be signed in to change notification settings - Fork 11.6k
[11.x] Introduce Support\Url and Support\UrlQueryParameters classes for easier URL handling
#53370
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
2e62a2d
Add Url support classes
stevebauman f7b5c4b
Add tests
stevebauman c119343
Replace standalone parse_url calls with Url::parse
stevebauman 9cf9a38
CS fixes
stevebauman 5b9a7b4
Last CS fixes
stevebauman 83a0c8b
Add types
stevebauman 1fcd294
Fix tests
stevebauman 6924791
Implement stringable
stevebauman ad9c0d3
Add tests
stevebauman 0f0f4c6
Use http_build_query
stevebauman ec14194
CS fixes
stevebauman 70f8df5
Don't urldecode twice
stevebauman 2c8e5ea
Update test case to ensure no double encoding
stevebauman 94fd44f
Use `Arr::query`
stevebauman e90959a
Refactors
stevebauman 7ac981e
Fix test
stevebauman 92f2500
Fix style
stevebauman 892ff79
Update src/Illuminate/Support/UrlQueryParameters.php
stevebauman 9f22201
Update src/Illuminate/Support/Url.php
stevebauman 036ddae
Throw exception when parse_url returns false
stevebauman d57f4d9
Merge branch 'url-support' of https://github.com/stevebauman/framewor…
stevebauman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| <?php | ||
|
|
||
| namespace Illuminate\Support; | ||
|
|
||
| use Illuminate\Contracts\Support\Arrayable; | ||
| use RuntimeException; | ||
| use Stringable; | ||
|
|
||
| class Url implements Arrayable, Stringable | ||
| { | ||
| /** | ||
| * Constructor. | ||
| */ | ||
| public function __construct( | ||
| public ?string $scheme = null, | ||
| public ?string $host = null, | ||
| public ?int $port = null, | ||
| public ?string $user = null, | ||
| #[\SensitiveParameter] | ||
| public ?string $pass = null, | ||
| public ?string $path = null, | ||
| public ?UrlQueryParameters $query = null, | ||
| public ?string $fragment = null, | ||
| ) { | ||
| $this->query ??= new UrlQueryParameters; | ||
| } | ||
|
|
||
| /** | ||
| * Parse a URL string into a URL object. | ||
| * | ||
| * @param string $url | ||
| * @return static | ||
| */ | ||
| public static function parse(string $url): static | ||
| { | ||
| $components = parse_url($url); | ||
|
|
||
| if ($components === false) { | ||
| throw new RuntimeException("Invalid URL [$url]."); | ||
| } | ||
|
|
||
| if (isset($components['query'])) { | ||
| $components['query'] = static::query($components['query']); | ||
| } | ||
|
|
||
| return new static(...$components); | ||
| } | ||
|
|
||
| /** | ||
| * Parse URL query parameters. | ||
| * | ||
| * @return \Illuminate\Support\UrlQueryParameters | ||
| */ | ||
| protected static function query(?string $query): UrlQueryParameters | ||
| { | ||
| return UrlQueryParameters::parse($query); | ||
| } | ||
|
|
||
| /** | ||
| * Convert the URL object to an array. | ||
| * | ||
| * @return array<string, string|null> | ||
| */ | ||
| public function toArray(): array | ||
| { | ||
| return [ | ||
| 'scheme' => $this->scheme, | ||
| 'host' => $this->host, | ||
| 'port' => $this->port, | ||
| 'user' => $this->user, | ||
| 'pass' => $this->pass, | ||
| 'path' => $this->path, | ||
| 'query' => (string) $this->query, | ||
| 'fragment' => $this->fragment, | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * Convert the URL object to a string. | ||
| * | ||
| * @return string | ||
| */ | ||
| public function __toString(): string | ||
| { | ||
| $url = ''; | ||
|
|
||
| if ($this->scheme) { | ||
| $url .= $this->scheme.':'; | ||
| } | ||
|
|
||
| $url .= '//'; | ||
|
|
||
| if ($this->user) { | ||
| $url .= $this->user.($this->pass ? ':'.$this->pass : '').'@'; | ||
| } | ||
|
|
||
| if ($this->host) { | ||
| $url .= $this->host; | ||
| } | ||
|
|
||
| if ($this->port) { | ||
| $url .= ':'.$this->port; | ||
| } | ||
|
|
||
| if ($this->path) { | ||
| $url .= $this->path; | ||
| } | ||
|
|
||
| if ($this->query) { | ||
| $url .= '?'.$this->query; | ||
| } | ||
|
|
||
| if ($this->fragment) { | ||
| $url .= '#'.$this->fragment; | ||
| } | ||
|
|
||
| return $url; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,190 @@ | ||
| <?php | ||
|
|
||
| namespace Illuminate\Support; | ||
|
|
||
| use ArrayAccess; | ||
| use Illuminate\Contracts\Support\Arrayable; | ||
| use Stringable; | ||
|
|
||
| class UrlQueryParameters implements Arrayable, ArrayAccess, Stringable | ||
| { | ||
| /** | ||
| * Constructor. | ||
| * | ||
| * @param array<string, string> $parameters | ||
| * @return void | ||
| */ | ||
| public function __construct( | ||
| protected array $parameters = [] | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * Parse a query string. | ||
| * | ||
| * @param string|null $query | ||
| * @return static | ||
| */ | ||
| public static function parse(?string $query): static | ||
| { | ||
| if (is_null($query)) { | ||
| return new static; | ||
| } | ||
|
|
||
| parse_str( | ||
| Str::of($query) | ||
| ->chopStart('?') | ||
| ->toString(), | ||
| $parameters | ||
| ); | ||
|
|
||
| return new static($parameters); | ||
| } | ||
|
|
||
| /** | ||
| * Get a parameter value. | ||
| * | ||
| * @param string $key | ||
| * @param mixed $default | ||
| * @return mixed | ||
| */ | ||
| public function get(string $key, mixed $default = null): mixed | ||
| { | ||
| return $this->parameters[$key] ?? $default; | ||
| } | ||
|
|
||
| /** | ||
| * Set a parameter value. | ||
| * | ||
| * @param string $key | ||
| * @param string $value | ||
| * @return $this | ||
| */ | ||
| public function set(string $key, string $value): static | ||
| { | ||
| $this->parameters[$key] = $value; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Remove a parameter. | ||
| * | ||
| * @param string $key | ||
| * @return $this | ||
| */ | ||
| public function forget(string $key): static | ||
| { | ||
| unset($this->parameters[$key]); | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Clear all parameters. | ||
| * | ||
| * @return $this | ||
| */ | ||
| public function clear(): static | ||
| { | ||
| $this->parameters = []; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Check if a parameter exists. | ||
| * | ||
| * @param string $key | ||
| * @return bool | ||
| */ | ||
| public function has(string $key): bool | ||
| { | ||
| return isset($this->parameters[$key]); | ||
| } | ||
|
|
||
| /** | ||
| * Get all parameters. | ||
| * | ||
| * @return array<string, mixed> | ||
| */ | ||
| public function all(): array | ||
| { | ||
| return $this->parameters; | ||
| } | ||
|
|
||
| /** | ||
| * Determine if the parameters are empty. | ||
| * | ||
| * @return bool | ||
| */ | ||
| public function isEmpty(): bool | ||
| { | ||
| return empty($this->parameters); | ||
| } | ||
|
|
||
| /** | ||
| * Get the instance as an array. | ||
| * | ||
| * @return array<string, string> | ||
| */ | ||
| public function toArray(): array | ||
| { | ||
| return $this->all(); | ||
| } | ||
|
|
||
| /** | ||
| * Convert the query parameters to a string. | ||
| * | ||
| * @return string | ||
| */ | ||
| public function __toString(): string | ||
| { | ||
| return Arr::query($this->parameters); | ||
| } | ||
|
|
||
| /** | ||
| * Determine if a parameter exists. | ||
| * | ||
| * @param mixed $offset | ||
| * @return bool | ||
| */ | ||
| public function offsetExists(mixed $offset): bool | ||
| { | ||
| return isset($this->parameters[$offset]); | ||
| } | ||
|
|
||
| /** | ||
| * Get a parameter value. | ||
| * | ||
| * @param mixed $offset | ||
| * @return mixed | ||
| */ | ||
| public function offsetGet(mixed $offset): mixed | ||
| { | ||
| return $this->parameters[$offset]; | ||
| } | ||
|
|
||
| /** | ||
| * Set a parameter value. | ||
| * | ||
| * @param mixed $offset | ||
| * @param mixed $value | ||
| * @return void | ||
| */ | ||
| public function offsetSet(mixed $offset, mixed $value): void | ||
| { | ||
| $this->parameters[$offset] = $value; | ||
| } | ||
|
|
||
| /** | ||
| * Remove a parameter. | ||
| * | ||
| * @param mixed $offset | ||
| * @return void | ||
| */ | ||
| public function offsetUnset(mixed $offset): void | ||
| { | ||
| unset($this->parameters[$offset]); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.