thinktomorrow /
url
| 1 | <?php |
||
| 2 | declare(strict_types=1); |
||
| 3 | |||
| 4 | namespace Thinktomorrow\Url; |
||
| 5 | |||
| 6 | use JetBrains\PhpStorm\ArrayShape; |
||
| 7 | use JetBrains\PhpStorm\Pure; |
||
| 8 | use Thinktomorrow\Url\Exceptions\InvalidUrl; |
||
| 9 | |||
| 10 | class ParsedUrl |
||
| 11 | { |
||
| 12 | private Root $root; |
||
| 13 | private ?string $path; |
||
| 14 | private ?string $query; |
||
| 15 | private ?string $hash; |
||
| 16 | |||
| 17 | public function __construct(Root $root, ?string $path = null, ?string $query = null, ?string $hash = null) |
||
| 18 | { |
||
| 19 | $this->root = $root; |
||
| 20 | $this->path = $path; |
||
| 21 | 19 | $this->query = $query; |
|
| 22 | $this->hash = $hash; |
||
| 23 | 19 | } |
|
| 24 | 19 | ||
| 25 | 19 | public static function fromString(string $url): self |
|
| 26 | 19 | { |
|
| 27 | 19 | return new static(...array_values(static::parse($url))); |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 28 | } |
||
| 29 | 20 | ||
| 30 | public function get(): string |
||
| 31 | 20 | { |
|
| 32 | $result = $this->root->get() . |
||
| 33 | ($this->hasPath() ? '/' . $this->path() : '') . |
||
| 34 | 16 | ($this->hasQuery() ? '?' . $this->query() : '') . |
|
| 35 | ($this->hasHash() ? '#' . $this->hash() : ''); |
||
| 36 | 16 | ||
| 37 | 16 | return str_replace('///', '//', $result); |
|
| 38 | 16 | } |
|
| 39 | 16 | ||
| 40 | #[ArrayShape([ |
||
| 41 | 16 | 'root' => Root::class, |
|
| 42 | 'path' => 'null|string', |
||
| 43 | 'query' => 'null|string', |
||
| 44 | 20 | 'hash' => 'null|string', |
|
| 45 | ])] |
||
| 46 | private static function parse(string $url): array |
||
| 47 | { |
||
| 48 | 20 | // Specific case where we accept double slashes and convert it to a relative url. |
|
| 49 | 1 | // This would otherwise not be able to be parsed. |
|
| 50 | if ($url == '//') { |
||
| 51 | $url = '/'; |
||
| 52 | 20 | } |
|
| 53 | |||
| 54 | 20 | $parsed = parse_url($url); |
|
| 55 | 1 | ||
| 56 | if (false === $parsed) { |
||
| 57 | throw new InvalidUrl('Failed to parse url. Invalid url ['.$url.'] passed as parameter.'); |
||
| 58 | 19 | } |
|
| 59 | |||
| 60 | $root = Root::fromString($url)->defaultScheme(); |
||
| 61 | 19 | ||
| 62 | return [ |
||
| 63 | 19 | 'root' => $root, |
|
| 64 | 19 | // Check if path could match host because this means something as foobar.com is passed and this is regarded as 'path' by the parse_url function |
|
| 65 | 19 | 'path' => (isset($parsed['path']) && $parsed['path'] && $parsed['path'] != $root->host()) ? trim($parsed['path'], '/') : null, |
|
| 66 | 'query' => $parsed['query'] ?? null, |
||
| 67 | 'hash' => $parsed['fragment'] ?? null, |
||
| 68 | ]; |
||
| 69 | 4 | } |
|
| 70 | |||
| 71 | 4 | #[Pure] |
|
| 72 | 4 | public function replaceRoot(Root $root): self |
|
| 73 | 4 | { |
|
| 74 | 4 | return new static( |
|
| 75 | 4 | $root, |
|
| 76 | $this->path, |
||
| 77 | $this->query, |
||
| 78 | $this->hash |
||
| 79 | 7 | ); |
|
| 80 | } |
||
| 81 | 7 | ||
| 82 | 7 | public function replaceScheme(string $scheme): self |
|
| 83 | 7 | { |
|
| 84 | 7 | return new static( |
|
| 85 | 7 | $this->root->replaceScheme($scheme), |
|
| 86 | $this->path, |
||
| 87 | $this->query, |
||
| 88 | $this->hash |
||
| 89 | 9 | ); |
|
| 90 | } |
||
| 91 | 9 | ||
| 92 | 9 | #[Pure] |
|
| 93 | 9 | public function replacePath(string $path): self |
|
| 94 | 9 | { |
|
| 95 | 9 | return new static( |
|
| 96 | $this->root, |
||
| 97 | trim($path, '/'), |
||
| 98 | $this->query, |
||
| 99 | 1 | $this->hash |
|
| 100 | ); |
||
| 101 | 1 | } |
|
| 102 | |||
| 103 | #[Pure] |
||
| 104 | 1 | public function scheme(): ?string |
|
| 105 | { |
||
| 106 | 1 | return $this->root->scheme(); |
|
| 107 | } |
||
| 108 | |||
| 109 | 1 | #[Pure] |
|
| 110 | public function host(): ?string |
||
| 111 | 1 | { |
|
| 112 | return $this->root->host(); |
||
| 113 | } |
||
| 114 | 12 | ||
| 115 | #[Pure] |
||
| 116 | 12 | public function port(): ?string |
|
| 117 | { |
||
| 118 | return $this->root->port(); |
||
| 119 | 4 | } |
|
| 120 | |||
| 121 | 4 | public function path(): ?string |
|
| 122 | { |
||
| 123 | return $this->path; |
||
| 124 | 2 | } |
|
| 125 | |||
| 126 | 2 | public function query(): ?string |
|
| 127 | { |
||
| 128 | return $this->query; |
||
| 129 | 1 | } |
|
| 130 | |||
| 131 | 1 | public function hash(): ?string |
|
| 132 | { |
||
| 133 | return $this->hash; |
||
| 134 | 2 | } |
|
| 135 | |||
| 136 | 2 | #[Pure] |
|
| 137 | public function hasScheme(): bool |
||
| 138 | { |
||
| 139 | 1 | return ! ! $this->root->scheme(); |
|
| 140 | } |
||
| 141 | 1 | ||
| 142 | #[Pure] |
||
| 143 | public function hasHost(): bool |
||
| 144 | 17 | { |
|
| 145 | return ! ! $this->root->host(); |
||
| 146 | 17 | } |
|
| 147 | |||
| 148 | #[Pure] |
||
| 149 | 17 | public function hasPort(): bool |
|
| 150 | { |
||
| 151 | 17 | return ! ! $this->root->port(); |
|
| 152 | } |
||
| 153 | |||
| 154 | 17 | public function hasPath(): bool |
|
| 155 | { |
||
| 156 | 17 | return ! ! $this->path; |
|
| 157 | } |
||
| 158 | |||
| 159 | public function hasQuery(): bool |
||
| 160 | { |
||
| 161 | return ! ! $this->query; |
||
| 162 | } |
||
| 163 | |||
| 164 | public function hasHash(): bool |
||
| 165 | { |
||
| 166 | return ! ! $this->hash; |
||
| 167 | } |
||
| 168 | } |
||
| 169 |