|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of the Nette Framework (https://nette.org) |
| 5 | + * Copyright (c) 2004 David Grudl (https://davidgrudl.com) |
| 6 | + */ |
| 7 | + |
| 8 | +declare(strict_types=1); |
| 9 | + |
| 10 | +namespace Nette\Http; |
| 11 | + |
| 12 | +use Nette; |
| 13 | + |
| 14 | + |
| 15 | +/** |
| 16 | + * Immutable representation of a URL. |
| 17 | + * |
| 18 | + * <pre> |
| 19 | + * scheme user password host port path query fragment |
| 20 | + * | | | | | | | | |
| 21 | + * /--\ /--\ /------\ /-------\ /--\/------------\ /--------\ /------\ |
| 22 | + * http://john:[email protected]:8042/en/manual.php?name=param#fragment <-- absoluteUrl |
| 23 | + * \______\__________________________/ |
| 24 | + * | | |
| 25 | + * hostUrl authority |
| 26 | + * </pre> |
| 27 | + * |
| 28 | + * @property-read string $scheme |
| 29 | + * @property-read string $user |
| 30 | + * @property-read string $password |
| 31 | + * @property-read string $host |
| 32 | + * @property-read int $port |
| 33 | + * @property-read string $path |
| 34 | + * @property-read string $query |
| 35 | + * @property-read string $fragment |
| 36 | + * @property-read string $absoluteUrl |
| 37 | + * @property-read string $authority |
| 38 | + * @property-read string $hostUrl |
| 39 | + * @property-read array $queryParameters |
| 40 | + */ |
| 41 | +class UrlImmutable implements \JsonSerializable |
| 42 | +{ |
| 43 | + use Nette\SmartObject; |
| 44 | + |
| 45 | + /** @var string */ |
| 46 | + private $scheme = ''; |
| 47 | + |
| 48 | + /** @var string */ |
| 49 | + private $user = ''; |
| 50 | + |
| 51 | + /** @var string */ |
| 52 | + private $password = ''; |
| 53 | + |
| 54 | + /** @var string */ |
| 55 | + private $host = ''; |
| 56 | + |
| 57 | + /** @var int|null */ |
| 58 | + private $port; |
| 59 | + |
| 60 | + /** @var string */ |
| 61 | + private $path = ''; |
| 62 | + |
| 63 | + /** @var array */ |
| 64 | + private $query = []; |
| 65 | + |
| 66 | + /** @var string */ |
| 67 | + private $fragment = ''; |
| 68 | + |
| 69 | + |
| 70 | + /** |
| 71 | + * @param string|self|Url $url |
| 72 | + * @throws Nette\InvalidArgumentException if URL is malformed |
| 73 | + */ |
| 74 | + public function __construct($url) |
| 75 | + { |
| 76 | + if ($url instanceof Url || $url instanceof self || is_string($url)) { |
| 77 | + $url = is_string($url) ? new Url($url) : $url; |
| 78 | + [$this->scheme, $this->user, $this->password, $this->host, $this->port, $this->path, $this->query, $this->fragment] = $url->export(); |
| 79 | + } else { |
| 80 | + throw new Nette\InvalidArgumentException; |
| 81 | + } |
| 82 | + |
| 83 | + if ($this->host && substr($this->path, 0, 1) !== '/') { |
| 84 | + $this->path = '/' . $this->path; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | + public function getScheme(): string |
| 90 | + { |
| 91 | + return $this->scheme; |
| 92 | + } |
| 93 | + |
| 94 | + |
| 95 | + public function getUser(): string |
| 96 | + { |
| 97 | + return $this->user; |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | + public function getPassword(): string |
| 102 | + { |
| 103 | + return $this->password; |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | + public function getHost(): string |
| 108 | + { |
| 109 | + return $this->host; |
| 110 | + } |
| 111 | + |
| 112 | + |
| 113 | + public function getDomain(int $level = 2): string |
| 114 | + { |
| 115 | + $parts = ip2long($this->host) ? [$this->host] : explode('.', $this->host); |
| 116 | + $parts = $level >= 0 ? array_slice($parts, -$level) : array_slice($parts, 0, $level); |
| 117 | + return implode('.', $parts); |
| 118 | + } |
| 119 | + |
| 120 | + |
| 121 | + public function getPort(): ?int |
| 122 | + { |
| 123 | + return $this->port ?: (Url::$defaultPorts[$this->scheme] ?? null); |
| 124 | + } |
| 125 | + |
| 126 | + |
| 127 | + public function getPath(): string |
| 128 | + { |
| 129 | + return $this->path; |
| 130 | + } |
| 131 | + |
| 132 | + |
| 133 | + public function getQuery(): string |
| 134 | + { |
| 135 | + return http_build_query($this->query, '', '&', PHP_QUERY_RFC3986); |
| 136 | + } |
| 137 | + |
| 138 | + |
| 139 | + public function getQueryParameters(): array |
| 140 | + { |
| 141 | + return $this->query; |
| 142 | + } |
| 143 | + |
| 144 | + |
| 145 | + /** |
| 146 | + * @return array|string|null |
| 147 | + */ |
| 148 | + public function getQueryParameter(string $name) |
| 149 | + { |
| 150 | + return $this->query[$name] ?? null; |
| 151 | + } |
| 152 | + |
| 153 | + |
| 154 | + public function getFragment(): string |
| 155 | + { |
| 156 | + return $this->fragment; |
| 157 | + } |
| 158 | + |
| 159 | + |
| 160 | + /** |
| 161 | + * Returns the entire URI including query string and fragment. |
| 162 | + */ |
| 163 | + public function getAbsoluteUrl(): string |
| 164 | + { |
| 165 | + return $this->getHostUrl() . $this->path |
| 166 | + . (($tmp = $this->getQuery()) ? '?' . $tmp : '') |
| 167 | + . ($this->fragment === '' ? '' : '#' . $this->fragment); |
| 168 | + } |
| 169 | + |
| 170 | + |
| 171 | + /** |
| 172 | + * Returns the [user[:pass]@]host[:port] part of URI. |
| 173 | + */ |
| 174 | + public function getAuthority(): string |
| 175 | + { |
| 176 | + return $this->host === '' |
| 177 | + ? '' |
| 178 | + : ($this->user !== '' |
| 179 | + ? rawurlencode($this->user) . ($this->password === '' ? '' : ':' . rawurlencode($this->password)) . '@' |
| 180 | + : '') |
| 181 | + . $this->host |
| 182 | + . ($this->port && (!isset(Url::$defaultPorts[$this->scheme]) || $this->port !== Url::$defaultPorts[$this->scheme]) |
| 183 | + ? ':' . $this->port |
| 184 | + : ''); |
| 185 | + } |
| 186 | + |
| 187 | + |
| 188 | + /** |
| 189 | + * Returns the scheme and authority part of URI. |
| 190 | + */ |
| 191 | + public function getHostUrl(): string |
| 192 | + { |
| 193 | + return ($this->scheme ? $this->scheme . ':' : '') |
| 194 | + . (($authority = $this->getAuthority()) ? '//' . $authority : ''); |
| 195 | + } |
| 196 | + |
| 197 | + |
| 198 | + public function __toString(): string |
| 199 | + { |
| 200 | + return $this->getAbsoluteUrl(); |
| 201 | + } |
| 202 | + |
| 203 | + |
| 204 | + /** |
| 205 | + * @param string|Url|self $url |
| 206 | + */ |
| 207 | + public function isEqual($url): bool |
| 208 | + { |
| 209 | + return (new Url($this))->isEqual($url); |
| 210 | + } |
| 211 | + |
| 212 | + |
| 213 | + public function jsonSerialize(): string |
| 214 | + { |
| 215 | + return $this->getAbsoluteUrl(); |
| 216 | + } |
| 217 | + |
| 218 | + |
| 219 | + /** @internal */ |
| 220 | + final public function export(): array |
| 221 | + { |
| 222 | + return [$this->scheme, $this->user, $this->password, $this->host, $this->port, $this->path, $this->query, $this->fragment]; |
| 223 | + } |
| 224 | +} |
0 commit comments