| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace GuzzleHttp\Psr7; |
| 6 |
|
| 7 |
use GuzzleHttp\Psr7\Exception\MalformedUriException; |
| 8 |
use Psr\Http\Message\UriInterface; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
class Uri implements UriInterface, \JsonSerializable |
| 18 |
{ |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
private const HTTP_DEFAULT_HOST = 'localhost'; |
| 26 |
|
| 27 |
private const DEFAULT_PORTS = [ |
| 28 |
'http' => 80, |
| 29 |
'https' => 443, |
| 30 |
'ftp' => 21, |
| 31 |
'gopher' => 70, |
| 32 |
'nntp' => 119, |
| 33 |
'news' => 119, |
| 34 |
'telnet' => 23, |
| 35 |
'tn3270' => 23, |
| 36 |
'imap' => 143, |
| 37 |
'pop' => 110, |
| 38 |
'ldap' => 389, |
| 39 |
]; |
| 40 |
|
| 41 |
private const QUERY_SEPARATORS_REPLACEMENT = ['=' => '%3D', '&' => '%26', '+' => '%2B']; |
| 42 |
|
| 43 |
|
| 44 |
private $scheme = ''; |
| 45 |
|
| 46 |
|
| 47 |
private $userInfo = ''; |
| 48 |
|
| 49 |
|
| 50 |
private $host = ''; |
| 51 |
|
| 52 |
|
| 53 |
private $port; |
| 54 |
|
| 55 |
|
| 56 |
private $path = ''; |
| 57 |
|
| 58 |
|
| 59 |
private $query = ''; |
| 60 |
|
| 61 |
|
| 62 |
private $fragment = ''; |
| 63 |
|
| 64 |
public function __construct(string $uri = '') |
| 65 |
{ |
| 66 |
if ($uri !== '') { |
| 67 |
$parts = self::parse($uri); |
| 68 |
if ($parts === false) { |
| 69 |
throw new MalformedUriException("Unable to parse URI: $uri"); |
| 70 |
} |
| 71 |
try { |
| 72 |
$this->applyParts($parts); |
| 73 |
} catch (MalformedUriException $e) { |
| 74 |
throw $e; |
| 75 |
} catch (\InvalidArgumentException $e) { |
| 76 |
throw new MalformedUriException($e->getMessage(), 0, $e); |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
private static function parse(string $url) |
| 97 |
{ |
| 98 |
if (self::isPathNoSchemeReference($url)) { |
| 99 |
return self::parsePathNoSchemeReference($url); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
$prefix = ''; |
| 104 |
if (preg_match('%^([0-9A-Za-z+.-]+://\[[0-9:.a-fA-F]+\])(.*?)$%', $url, $matches)) { |
| 105 |
|
| 106 |
$prefix = $matches[1]; |
| 107 |
$url = $matches[2]; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
$encodedUrl = preg_replace_callback( |
| 112 |
'%[^:/@?&=#]+%usD', |
| 113 |
static function ($matches) { |
| 114 |
return urlencode($matches[0]); |
| 115 |
}, |
| 116 |
$url |
| 117 |
); |
| 118 |
|
| 119 |
if ($encodedUrl === null) { |
| 120 |
return false; |
| 121 |
} |
| 122 |
|
| 123 |
$result = parse_url($prefix.$encodedUrl); |
| 124 |
|
| 125 |
if ($result === false) { |
| 126 |
return false; |
| 127 |
} |
| 128 |
|
| 129 |
return array_map('urldecode', $result); |
| 130 |
} |
| 131 |
|
| 132 |
private static function isPathNoSchemeReference(string $url): bool |
| 133 |
{ |
| 134 |
if ($url === '' || $url[0] === '/' || $url[0] === '?' || $url[0] === '#') { |
| 135 |
return false; |
| 136 |
} |
| 137 |
|
| 138 |
$firstSegment = substr($url, 0, strcspn($url, '/?#')); |
| 139 |
|
| 140 |
return strpos($firstSegment, ':') === false; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
private static function parsePathNoSchemeReference(string $url): array |
| 147 |
{ |
| 148 |
$parts = []; |
| 149 |
|
| 150 |
if (false !== ($fragmentPosition = strpos($url, '#'))) { |
| 151 |
$parts['fragment'] = substr($url, $fragmentPosition + 1); |
| 152 |
$url = substr($url, 0, $fragmentPosition); |
| 153 |
} |
| 154 |
|
| 155 |
if (false !== ($queryPosition = strpos($url, '?'))) { |
| 156 |
$parts['query'] = substr($url, $queryPosition + 1); |
| 157 |
$url = substr($url, 0, $queryPosition); |
| 158 |
} |
| 159 |
|
| 160 |
$parts['path'] = $url; |
| 161 |
|
| 162 |
return $parts; |
| 163 |
} |
| 164 |
|
| 165 |
public function __toString(): string |
| 166 |
{ |
| 167 |
return self::composeComponents( |
| 168 |
$this->scheme, |
| 169 |
$this->getAuthority(), |
| 170 |
$this->path, |
| 171 |
$this->query, |
| 172 |
$this->fragment |
| 173 |
); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
public static function composeComponents(?string $scheme, ?string $authority, string $path, ?string $query, ?string $fragment): string |
| 195 |
{ |
| 196 |
$uri = ''; |
| 197 |
|
| 198 |
|
| 199 |
if ($scheme != '') { |
| 200 |
$uri .= $scheme.':'; |
| 201 |
} |
| 202 |
|
| 203 |
if ($authority != '' || $scheme === 'file') { |
| 204 |
$uri .= '//'.$authority; |
| 205 |
} |
| 206 |
|
| 207 |
if ($authority != '' && $path != '' && $path[0] != '/') { |
| 208 |
$path = '/'.$path; |
| 209 |
} |
| 210 |
|
| 211 |
$uri .= $path; |
| 212 |
|
| 213 |
if ($query != '') { |
| 214 |
$uri .= '?'.$query; |
| 215 |
} |
| 216 |
|
| 217 |
if ($fragment != '') { |
| 218 |
$uri .= '#'.$fragment; |
| 219 |
} |
| 220 |
|
| 221 |
return $uri; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
public static function isDefaultPort(UriInterface $uri): bool |
| 231 |
{ |
| 232 |
return $uri->getPort() === null |
| 233 |
|| (isset(self::DEFAULT_PORTS[$uri->getScheme()]) && $uri->getPort() === self::DEFAULT_PORTS[$uri->getScheme()]); |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
public static function isAbsolute(UriInterface $uri): bool |
| 252 |
{ |
| 253 |
return $uri->getScheme() !== ''; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
public static function isNetworkPathReference(UriInterface $uri): bool |
| 264 |
{ |
| 265 |
return $uri->getScheme() === '' && $uri->getAuthority() !== ''; |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
public static function isAbsolutePathReference(UriInterface $uri): bool |
| 276 |
{ |
| 277 |
return $uri->getScheme() === '' |
| 278 |
&& $uri->getAuthority() === '' |
| 279 |
&& isset($uri->getPath()[0]) |
| 280 |
&& $uri->getPath()[0] === '/'; |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
public static function isRelativePathReference(UriInterface $uri): bool |
| 291 |
{ |
| 292 |
return $uri->getScheme() === '' |
| 293 |
&& $uri->getAuthority() === '' |
| 294 |
&& (!isset($uri->getPath()[0]) || $uri->getPath()[0] !== '/'); |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
public static function isSameDocumentReference(UriInterface $uri, ?UriInterface $base = null): bool |
| 310 |
{ |
| 311 |
if ($base !== null) { |
| 312 |
$uri = UriResolver::resolve($base, $uri); |
| 313 |
|
| 314 |
return ($uri->getScheme() === $base->getScheme()) |
| 315 |
&& ($uri->getAuthority() === $base->getAuthority()) |
| 316 |
&& ($uri->getPath() === $base->getPath()) |
| 317 |
&& ($uri->getQuery() === $base->getQuery()); |
| 318 |
} |
| 319 |
|
| 320 |
return $uri->getScheme() === '' && $uri->getAuthority() === '' && $uri->getPath() === '' && $uri->getQuery() === ''; |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
public static function withoutQueryValue(UriInterface $uri, string $key): UriInterface |
| 333 |
{ |
| 334 |
$result = self::getFilteredQueryString($uri, [$key]); |
| 335 |
|
| 336 |
return $uri->withQuery(implode('&', $result)); |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
public static function withQueryValue(UriInterface $uri, string $key, ?string $value): UriInterface |
| 353 |
{ |
| 354 |
$result = self::getFilteredQueryString($uri, [$key]); |
| 355 |
|
| 356 |
$result[] = self::generateQueryString($key, $value); |
| 357 |
|
| 358 |
return $uri->withQuery(implode('&', $result)); |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
public static function withQueryValues(UriInterface $uri, array $keyValueArray): UriInterface |
| 370 |
{ |
| 371 |
$result = self::getFilteredQueryString($uri, array_keys($keyValueArray)); |
| 372 |
|
| 373 |
foreach ($keyValueArray as $key => $value) { |
| 374 |
$result[] = self::generateQueryString((string) $key, $value !== null ? (string) $value : null); |
| 375 |
} |
| 376 |
|
| 377 |
return $uri->withQuery(implode('&', $result)); |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
public static function fromParts(array $parts): UriInterface |
| 388 |
{ |
| 389 |
$uri = new self(); |
| 390 |
try { |
| 391 |
$uri->applyParts($parts); |
| 392 |
$uri->validateState(); |
| 393 |
} catch (MalformedUriException $e) { |
| 394 |
throw $e; |
| 395 |
} catch (\InvalidArgumentException $e) { |
| 396 |
throw new MalformedUriException($e->getMessage(), 0, $e); |
| 397 |
} |
| 398 |
|
| 399 |
return $uri; |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
public static function assertValidHost(string $host): void |
| 408 |
{ |
| 409 |
if ($host === '') { |
| 410 |
return; |
| 411 |
} |
| 412 |
|
| 413 |
if (preg_match('/[\x00-\x20\x7F]/', $host)) { |
| 414 |
throw new \InvalidArgumentException(sprintf('Invalid host: "%s"', $host)); |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
public function getScheme(): string |
| 419 |
{ |
| 420 |
return $this->scheme; |
| 421 |
} |
| 422 |
|
| 423 |
public function getAuthority(): string |
| 424 |
{ |
| 425 |
$authority = $this->host; |
| 426 |
if ($this->userInfo !== '') { |
| 427 |
$authority = $this->userInfo.'@'.$authority; |
| 428 |
} |
| 429 |
|
| 430 |
if ($this->port !== null) { |
| 431 |
$authority .= ':'.$this->port; |
| 432 |
} |
| 433 |
|
| 434 |
return $authority; |
| 435 |
} |
| 436 |
|
| 437 |
public function getUserInfo(): string |
| 438 |
{ |
| 439 |
return $this->userInfo; |
| 440 |
} |
| 441 |
|
| 442 |
public function getHost(): string |
| 443 |
{ |
| 444 |
return $this->host; |
| 445 |
} |
| 446 |
|
| 447 |
public function getPort(): ?int |
| 448 |
{ |
| 449 |
return $this->port; |
| 450 |
} |
| 451 |
|
| 452 |
public function getPath(): string |
| 453 |
{ |
| 454 |
return $this->path; |
| 455 |
} |
| 456 |
|
| 457 |
public function getQuery(): string |
| 458 |
{ |
| 459 |
return $this->query; |
| 460 |
} |
| 461 |
|
| 462 |
public function getFragment(): string |
| 463 |
{ |
| 464 |
return $this->fragment; |
| 465 |
} |
| 466 |
|
| 467 |
public function withScheme($scheme): UriInterface |
| 468 |
{ |
| 469 |
$scheme = $this->filterScheme($scheme); |
| 470 |
|
| 471 |
if ($this->scheme === $scheme) { |
| 472 |
return $this; |
| 473 |
} |
| 474 |
|
| 475 |
$new = clone $this; |
| 476 |
$new->scheme = $scheme; |
| 477 |
$new->removeDefaultPort(); |
| 478 |
$new->validateState(); |
| 479 |
|
| 480 |
return $new; |
| 481 |
} |
| 482 |
|
| 483 |
public function withUserInfo($user, $password = null): UriInterface |
| 484 |
{ |
| 485 |
$info = $this->filterUserInfoComponent($user); |
| 486 |
if ($password !== null) { |
| 487 |
$info .= ':'.$this->filterUserInfoComponent($password); |
| 488 |
} |
| 489 |
|
| 490 |
if ($this->userInfo === $info) { |
| 491 |
return $this; |
| 492 |
} |
| 493 |
|
| 494 |
$new = clone $this; |
| 495 |
$new->userInfo = $info; |
| 496 |
$new->validateState(); |
| 497 |
|
| 498 |
return $new; |
| 499 |
} |
| 500 |
|
| 501 |
public function withHost($host): UriInterface |
| 502 |
{ |
| 503 |
$host = $this->filterHost($host); |
| 504 |
|
| 505 |
if ($this->host === $host) { |
| 506 |
return $this; |
| 507 |
} |
| 508 |
|
| 509 |
$new = clone $this; |
| 510 |
$new->host = $host; |
| 511 |
$new->validateState(); |
| 512 |
|
| 513 |
return $new; |
| 514 |
} |
| 515 |
|
| 516 |
public function withPort($port): UriInterface |
| 517 |
{ |
| 518 |
if ($port !== null && !\is_int($port)) { |
| 519 |
\trigger_deprecation( |
| 520 |
'guzzlehttp/psr7', |
| 521 |
'2.11', |
| 522 |
'Passing %s to UriInterface::withPort() is deprecated; guzzlehttp/psr7 3.0 requires int|null.', |
| 523 |
\get_debug_type($port) |
| 524 |
); |
| 525 |
} |
| 526 |
|
| 527 |
$port = $this->filterPort($port); |
| 528 |
|
| 529 |
if ($this->port === $port) { |
| 530 |
return $this; |
| 531 |
} |
| 532 |
|
| 533 |
$new = clone $this; |
| 534 |
$new->port = $port; |
| 535 |
$new->removeDefaultPort(); |
| 536 |
$new->validateState(); |
| 537 |
|
| 538 |
return $new; |
| 539 |
} |
| 540 |
|
| 541 |
public function withPath($path): UriInterface |
| 542 |
{ |
| 543 |
$path = $this->filterPath($path); |
| 544 |
|
| 545 |
if ($this->path === $path) { |
| 546 |
return $this; |
| 547 |
} |
| 548 |
|
| 549 |
$new = clone $this; |
| 550 |
$new->path = $path; |
| 551 |
$new->validateState(); |
| 552 |
|
| 553 |
return $new; |
| 554 |
} |
| 555 |
|
| 556 |
public function withQuery($query): UriInterface |
| 557 |
{ |
| 558 |
$query = $this->filterQueryAndFragment($query); |
| 559 |
|
| 560 |
if ($this->query === $query) { |
| 561 |
return $this; |
| 562 |
} |
| 563 |
|
| 564 |
$new = clone $this; |
| 565 |
$new->query = $query; |
| 566 |
|
| 567 |
return $new; |
| 568 |
} |
| 569 |
|
| 570 |
public function withFragment($fragment): UriInterface |
| 571 |
{ |
| 572 |
$fragment = $this->filterQueryAndFragment($fragment); |
| 573 |
|
| 574 |
if ($this->fragment === $fragment) { |
| 575 |
return $this; |
| 576 |
} |
| 577 |
|
| 578 |
$new = clone $this; |
| 579 |
$new->fragment = $fragment; |
| 580 |
|
| 581 |
return $new; |
| 582 |
} |
| 583 |
|
| 584 |
public function jsonSerialize(): string |
| 585 |
{ |
| 586 |
return $this->__toString(); |
| 587 |
} |
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
private function applyParts(array $parts): void |
| 595 |
{ |
| 596 |
$this->scheme = isset($parts['scheme']) |
| 597 |
? $this->filterScheme($parts['scheme']) |
| 598 |
: ''; |
| 599 |
$this->userInfo = isset($parts['user']) |
| 600 |
? $this->filterUserInfoComponent($parts['user']) |
| 601 |
: ''; |
| 602 |
$this->host = isset($parts['host']) |
| 603 |
? $this->filterHost($parts['host']) |
| 604 |
: ''; |
| 605 |
$this->port = isset($parts['port']) |
| 606 |
? $this->filterPort($parts['port']) |
| 607 |
: null; |
| 608 |
$this->path = isset($parts['path']) |
| 609 |
? $this->filterPath($parts['path']) |
| 610 |
: ''; |
| 611 |
$this->query = isset($parts['query']) |
| 612 |
? $this->filterQueryAndFragment($parts['query']) |
| 613 |
: ''; |
| 614 |
$this->fragment = isset($parts['fragment']) |
| 615 |
? $this->filterQueryAndFragment($parts['fragment']) |
| 616 |
: ''; |
| 617 |
if (isset($parts['pass'])) { |
| 618 |
$this->userInfo .= ':'.$this->filterUserInfoComponent($parts['pass']); |
| 619 |
} |
| 620 |
|
| 621 |
$this->removeDefaultPort(); |
| 622 |
} |
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
private function filterScheme($scheme): string |
| 630 |
{ |
| 631 |
if (!is_string($scheme)) { |
| 632 |
throw new \InvalidArgumentException('Scheme must be a string'); |
| 633 |
} |
| 634 |
|
| 635 |
$scheme = \strtr($scheme, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'); |
| 636 |
|
| 637 |
if ($scheme !== '' && !preg_match('/^[a-z][a-z0-9.+-]*$/D', $scheme)) { |
| 638 |
\trigger_deprecation( |
| 639 |
'guzzlehttp/psr7', |
| 640 |
'2.11', |
| 641 |
'Passing "%s" as a URI scheme is deprecated; guzzlehttp/psr7 3.0 requires URI schemes to match RFC 3986 syntax and begin with a letter.', |
| 642 |
$scheme |
| 643 |
); |
| 644 |
} |
| 645 |
|
| 646 |
return $scheme; |
| 647 |
} |
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
private function filterUserInfoComponent($component): string |
| 655 |
{ |
| 656 |
if (!is_string($component)) { |
| 657 |
throw new \InvalidArgumentException('User info must be a string'); |
| 658 |
} |
| 659 |
|
| 660 |
return preg_replace_callback( |
| 661 |
'/(?:[^%'.Rfc3986::CHAR_UNRESERVED.Rfc3986::CHAR_SUB_DELIMS.']+|%(?![A-Fa-f0-9]{2}))/', |
| 662 |
[$this, 'rawurlencodeMatchZero'], |
| 663 |
$component |
| 664 |
); |
| 665 |
} |
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
private function filterHost($host): string |
| 673 |
{ |
| 674 |
if (!is_string($host)) { |
| 675 |
throw new \InvalidArgumentException('Host must be a string'); |
| 676 |
} |
| 677 |
|
| 678 |
$host = \strtr($host, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'); |
| 679 |
self::assertValidHost($host); |
| 680 |
|
| 681 |
return $host; |
| 682 |
} |
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
private function filterPort($port): ?int |
| 690 |
{ |
| 691 |
if ($port === null) { |
| 692 |
return null; |
| 693 |
} |
| 694 |
|
| 695 |
$port = (int) $port; |
| 696 |
if (0 > $port || 0xFFFF < $port) { |
| 697 |
throw new \InvalidArgumentException( |
| 698 |
sprintf('Invalid port: %d. Must be between 0 and 65535', $port) |
| 699 |
); |
| 700 |
} |
| 701 |
|
| 702 |
return $port; |
| 703 |
} |
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
private static function getFilteredQueryString(UriInterface $uri, array $keys): array |
| 711 |
{ |
| 712 |
$current = $uri->getQuery(); |
| 713 |
|
| 714 |
if ($current === '') { |
| 715 |
return []; |
| 716 |
} |
| 717 |
|
| 718 |
$decodedKeys = array_map(function ($k): string { |
| 719 |
return rawurldecode((string) $k); |
| 720 |
}, $keys); |
| 721 |
|
| 722 |
return array_filter(explode('&', $current), function ($part) use ($decodedKeys) { |
| 723 |
return !in_array(rawurldecode(explode('=', $part)[0]), $decodedKeys, true); |
| 724 |
}); |
| 725 |
} |
| 726 |
|
| 727 |
private static function generateQueryString(string $key, ?string $value): string |
| 728 |
{ |
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
$queryString = strtr($key, self::QUERY_SEPARATORS_REPLACEMENT); |
| 734 |
|
| 735 |
if ($value !== null) { |
| 736 |
$queryString .= '='.strtr($value, self::QUERY_SEPARATORS_REPLACEMENT); |
| 737 |
} |
| 738 |
|
| 739 |
return $queryString; |
| 740 |
} |
| 741 |
|
| 742 |
private function removeDefaultPort(): void |
| 743 |
{ |
| 744 |
if ($this->port !== null && self::isDefaultPort($this)) { |
| 745 |
$this->port = null; |
| 746 |
} |
| 747 |
} |
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
private function filterPath($path): string |
| 757 |
{ |
| 758 |
if (!is_string($path)) { |
| 759 |
throw new \InvalidArgumentException('Path must be a string'); |
| 760 |
} |
| 761 |
|
| 762 |
return preg_replace_callback( |
| 763 |
'/(?:[^'.Rfc3986::CHAR_UNRESERVED.Rfc3986::CHAR_SUB_DELIMS.'%:@\/]++|%(?![A-Fa-f0-9]{2}))/', |
| 764 |
[$this, 'rawurlencodeMatchZero'], |
| 765 |
$path |
| 766 |
); |
| 767 |
} |
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
private function filterQueryAndFragment($str): string |
| 777 |
{ |
| 778 |
if (!is_string($str)) { |
| 779 |
throw new \InvalidArgumentException('Query and fragment must be a string'); |
| 780 |
} |
| 781 |
|
| 782 |
return preg_replace_callback( |
| 783 |
'/(?:[^'.Rfc3986::CHAR_UNRESERVED.Rfc3986::CHAR_SUB_DELIMS.'%:@\/\?]++|%(?![A-Fa-f0-9]{2}))/', |
| 784 |
[$this, 'rawurlencodeMatchZero'], |
| 785 |
$str |
| 786 |
); |
| 787 |
} |
| 788 |
|
| 789 |
private function rawurlencodeMatchZero(array $match): string |
| 790 |
{ |
| 791 |
return rawurlencode($match[0]); |
| 792 |
} |
| 793 |
|
| 794 |
private function validateState(): void |
| 795 |
{ |
| 796 |
if ($this->host === '' && ($this->scheme === 'http' || $this->scheme === 'https')) { |
| 797 |
$this->host = self::HTTP_DEFAULT_HOST; |
| 798 |
} |
| 799 |
|
| 800 |
if ($this->getAuthority() === '') { |
| 801 |
if (0 === strpos($this->path, '//')) { |
| 802 |
throw new MalformedUriException('The path of a URI without an authority must not start with two slashes "//"'); |
| 803 |
} |
| 804 |
if ($this->scheme === '' && false !== strpos(explode('/', $this->path, 2)[0], ':')) { |
| 805 |
throw new MalformedUriException('A relative URI must not have a path beginning with a segment containing a colon'); |
| 806 |
} |
| 807 |
} |
| 808 |
} |
| 809 |
} |