| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Nyholm\Psr7; |
| 6 |
|
| 7 |
use Psr\Http\Message\UriInterface; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
class Uri implements UriInterface |
| 21 |
{ |
| 22 |
private const SCHEMES = ['http' => 80, 'https' => 443]; |
| 23 |
|
| 24 |
private const CHAR_UNRESERVED = 'a-zA-Z0-9_\-\.~'; |
| 25 |
|
| 26 |
private const CHAR_SUB_DELIMS = '!\$&\'\(\)\*\+,;='; |
| 27 |
|
| 28 |
private const CHAR_GEN_DELIMS = ':\/\?#\[\]@'; |
| 29 |
|
| 30 |
|
| 31 |
private $scheme = ''; |
| 32 |
|
| 33 |
|
| 34 |
private $userInfo = ''; |
| 35 |
|
| 36 |
|
| 37 |
private $host = ''; |
| 38 |
|
| 39 |
|
| 40 |
private $port; |
| 41 |
|
| 42 |
|
| 43 |
private $path = ''; |
| 44 |
|
| 45 |
|
| 46 |
private $query = ''; |
| 47 |
|
| 48 |
|
| 49 |
private $fragment = ''; |
| 50 |
|
| 51 |
public function __construct(string $uri = '') |
| 52 |
{ |
| 53 |
if ('' !== $uri) { |
| 54 |
if (false === $parts = \parse_url($uri)) { |
| 55 |
throw new \InvalidArgumentException(\sprintf('Unable to parse URI: "%s"', $uri)); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
$this->scheme = isset($parts['scheme']) ? \strtr($parts['scheme'], 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') : ''; |
| 60 |
$this->userInfo = $parts['user'] ?? ''; |
| 61 |
$this->host = isset($parts['host']) ? \strtr($parts['host'], 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') : ''; |
| 62 |
$this->port = isset($parts['port']) ? $this->filterPort($parts['port']) : null; |
| 63 |
$this->path = isset($parts['path']) ? $this->filterPath($parts['path']) : ''; |
| 64 |
$this->query = isset($parts['query']) ? $this->filterQueryAndFragment($parts['query']) : ''; |
| 65 |
$this->fragment = isset($parts['fragment']) ? $this->filterQueryAndFragment($parts['fragment']) : ''; |
| 66 |
if (isset($parts['pass'])) { |
| 67 |
$this->userInfo .= ':' . $parts['pass']; |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
public function __toString(): string |
| 73 |
{ |
| 74 |
return self::createUriString($this->scheme, $this->getAuthority(), $this->path, $this->query, $this->fragment); |
| 75 |
} |
| 76 |
|
| 77 |
public function getScheme(): string |
| 78 |
{ |
| 79 |
return $this->scheme; |
| 80 |
} |
| 81 |
|
| 82 |
public function getAuthority(): string |
| 83 |
{ |
| 84 |
if ('' === $this->host) { |
| 85 |
return ''; |
| 86 |
} |
| 87 |
|
| 88 |
$authority = $this->host; |
| 89 |
if ('' !== $this->userInfo) { |
| 90 |
$authority = $this->userInfo . '@' . $authority; |
| 91 |
} |
| 92 |
|
| 93 |
if (null !== $this->port) { |
| 94 |
$authority .= ':' . $this->port; |
| 95 |
} |
| 96 |
|
| 97 |
return $authority; |
| 98 |
} |
| 99 |
|
| 100 |
public function getUserInfo(): string |
| 101 |
{ |
| 102 |
return $this->userInfo; |
| 103 |
} |
| 104 |
|
| 105 |
public function getHost(): string |
| 106 |
{ |
| 107 |
return $this->host; |
| 108 |
} |
| 109 |
|
| 110 |
public function getPort(): ?int |
| 111 |
{ |
| 112 |
return $this->port; |
| 113 |
} |
| 114 |
|
| 115 |
public function getPath(): string |
| 116 |
{ |
| 117 |
$path = $this->path; |
| 118 |
|
| 119 |
if ('' !== $path && '/' !== $path[0]) { |
| 120 |
if ('' !== $this->host) { |
| 121 |
|
| 122 |
$path = '/' . $path; |
| 123 |
} |
| 124 |
} elseif (isset($path[1]) && '/' === $path[1]) { |
| 125 |
|
| 126 |
|
| 127 |
$path = '/' . \ltrim($path, '/'); |
| 128 |
} |
| 129 |
|
| 130 |
return $path; |
| 131 |
} |
| 132 |
|
| 133 |
public function getQuery(): string |
| 134 |
{ |
| 135 |
return $this->query; |
| 136 |
} |
| 137 |
|
| 138 |
public function getFragment(): string |
| 139 |
{ |
| 140 |
return $this->fragment; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public function withScheme($scheme): UriInterface |
| 147 |
{ |
| 148 |
if (!\is_string($scheme)) { |
| 149 |
throw new \InvalidArgumentException('Scheme must be a string'); |
| 150 |
} |
| 151 |
|
| 152 |
if ($this->scheme === $scheme = \strtr($scheme, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')) { |
| 153 |
return $this; |
| 154 |
} |
| 155 |
|
| 156 |
$new = clone $this; |
| 157 |
$new->scheme = $scheme; |
| 158 |
$new->port = $new->filterPort($new->port); |
| 159 |
|
| 160 |
return $new; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
public function withUserInfo($user, $password = null): UriInterface |
| 167 |
{ |
| 168 |
if (!\is_string($user)) { |
| 169 |
throw new \InvalidArgumentException('User must be a string'); |
| 170 |
} |
| 171 |
|
| 172 |
$info = \preg_replace_callback('/[' . self::CHAR_GEN_DELIMS . self::CHAR_SUB_DELIMS . ']++/', [__CLASS__, 'rawurlencodeMatchZero'], $user); |
| 173 |
if (null !== $password && '' !== $password) { |
| 174 |
if (!\is_string($password)) { |
| 175 |
throw new \InvalidArgumentException('Password must be a string'); |
| 176 |
} |
| 177 |
|
| 178 |
$info .= ':' . \preg_replace_callback('/[' . self::CHAR_GEN_DELIMS . self::CHAR_SUB_DELIMS . ']++/', [__CLASS__, 'rawurlencodeMatchZero'], $password); |
| 179 |
} |
| 180 |
|
| 181 |
if ($this->userInfo === $info) { |
| 182 |
return $this; |
| 183 |
} |
| 184 |
|
| 185 |
$new = clone $this; |
| 186 |
$new->userInfo = $info; |
| 187 |
|
| 188 |
return $new; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
public function withHost($host): UriInterface |
| 195 |
{ |
| 196 |
if (!\is_string($host)) { |
| 197 |
throw new \InvalidArgumentException('Host must be a string'); |
| 198 |
} |
| 199 |
|
| 200 |
if ($this->host === $host = \strtr($host, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')) { |
| 201 |
return $this; |
| 202 |
} |
| 203 |
|
| 204 |
$new = clone $this; |
| 205 |
$new->host = $host; |
| 206 |
|
| 207 |
return $new; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
public function withPort($port): UriInterface |
| 214 |
{ |
| 215 |
if ($this->port === $port = $this->filterPort($port)) { |
| 216 |
return $this; |
| 217 |
} |
| 218 |
|
| 219 |
$new = clone $this; |
| 220 |
$new->port = $port; |
| 221 |
|
| 222 |
return $new; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
public function withPath($path): UriInterface |
| 229 |
{ |
| 230 |
if ($this->path === $path = $this->filterPath($path)) { |
| 231 |
return $this; |
| 232 |
} |
| 233 |
|
| 234 |
$new = clone $this; |
| 235 |
$new->path = $path; |
| 236 |
|
| 237 |
return $new; |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
public function withQuery($query): UriInterface |
| 244 |
{ |
| 245 |
if ($this->query === $query = $this->filterQueryAndFragment($query)) { |
| 246 |
return $this; |
| 247 |
} |
| 248 |
|
| 249 |
$new = clone $this; |
| 250 |
$new->query = $query; |
| 251 |
|
| 252 |
return $new; |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
public function withFragment($fragment): UriInterface |
| 259 |
{ |
| 260 |
if ($this->fragment === $fragment = $this->filterQueryAndFragment($fragment)) { |
| 261 |
return $this; |
| 262 |
} |
| 263 |
|
| 264 |
$new = clone $this; |
| 265 |
$new->fragment = $fragment; |
| 266 |
|
| 267 |
return $new; |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
private static function createUriString(string $scheme, string $authority, string $path, string $query, string $fragment): string |
| 274 |
{ |
| 275 |
$uri = ''; |
| 276 |
if ('' !== $scheme) { |
| 277 |
$uri .= $scheme . ':'; |
| 278 |
} |
| 279 |
|
| 280 |
if ('' !== $authority) { |
| 281 |
$uri .= '//' . $authority; |
| 282 |
} |
| 283 |
|
| 284 |
if ('' !== $path) { |
| 285 |
if ('/' !== $path[0]) { |
| 286 |
if ('' !== $authority) { |
| 287 |
|
| 288 |
$path = '/' . $path; |
| 289 |
} |
| 290 |
} elseif (isset($path[1]) && '/' === $path[1]) { |
| 291 |
if ('' === $authority) { |
| 292 |
|
| 293 |
|
| 294 |
$path = '/' . \ltrim($path, '/'); |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
$uri .= $path; |
| 299 |
} |
| 300 |
|
| 301 |
if ('' !== $query) { |
| 302 |
$uri .= '?' . $query; |
| 303 |
} |
| 304 |
|
| 305 |
if ('' !== $fragment) { |
| 306 |
$uri .= '#' . $fragment; |
| 307 |
} |
| 308 |
|
| 309 |
return $uri; |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
private static function isNonStandardPort(string $scheme, int $port): bool |
| 316 |
{ |
| 317 |
return !isset(self::SCHEMES[$scheme]) || $port !== self::SCHEMES[$scheme]; |
| 318 |
} |
| 319 |
|
| 320 |
private function filterPort($port): ?int |
| 321 |
{ |
| 322 |
if (null === $port) { |
| 323 |
return null; |
| 324 |
} |
| 325 |
|
| 326 |
$port = (int) $port; |
| 327 |
if (0 > $port || 0xFFFF < $port) { |
| 328 |
throw new \InvalidArgumentException(\sprintf('Invalid port: %d. Must be between 0 and 65535', $port)); |
| 329 |
} |
| 330 |
|
| 331 |
return self::isNonStandardPort($this->scheme, $port) ? $port : null; |
| 332 |
} |
| 333 |
|
| 334 |
private function filterPath($path): string |
| 335 |
{ |
| 336 |
if (!\is_string($path)) { |
| 337 |
throw new \InvalidArgumentException('Path must be a string'); |
| 338 |
} |
| 339 |
|
| 340 |
return \preg_replace_callback('/(?:[^' . self::CHAR_UNRESERVED . self::CHAR_SUB_DELIMS . '%:@\/]++|%(?![A-Fa-f0-9]{2}))/', [__CLASS__, 'rawurlencodeMatchZero'], $path); |
| 341 |
} |
| 342 |
|
| 343 |
private function filterQueryAndFragment($str): string |
| 344 |
{ |
| 345 |
if (!\is_string($str)) { |
| 346 |
throw new \InvalidArgumentException('Query and fragment must be a string'); |
| 347 |
} |
| 348 |
|
| 349 |
return \preg_replace_callback('/(?:[^' . self::CHAR_UNRESERVED . self::CHAR_SUB_DELIMS . '%:@\/\?]++|%(?![A-Fa-f0-9]{2}))/', [__CLASS__, 'rawurlencodeMatchZero'], $str); |
| 350 |
} |
| 351 |
|
| 352 |
private static function rawurlencodeMatchZero(array $match): string |
| 353 |
{ |
| 354 |
return \rawurlencode($match[0]); |
| 355 |
} |
| 356 |
} |