From 81b2cf0ef790f2916bbb687b9ad364173ea6dca2 Mon Sep 17 00:00:00 2001 From: ignace nyamagana butera Date: Sat, 20 Jul 2024 15:47:38 +0200 Subject: [PATCH] Simplify Uri generation --- uri/Uri.php | 61 ++++++++++++----------------------------------------- 1 file changed, 13 insertions(+), 48 deletions(-) diff --git a/uri/Uri.php b/uri/Uri.php index 5be385f9..38073fc4 100644 --- a/uri/Uri.php +++ b/uri/Uri.php @@ -222,17 +222,17 @@ private function __construct( $this->scheme = $this->formatScheme($scheme); $this->user = Encoder::encodeUser($user); $this->pass = Encoder::encodePassword($pass); - $this->userInfo = $this->formatUserInfo($this->user, $this->pass); $this->host = $this->formatHost($host); $this->port = $this->formatPort($port); - $this->authority = $this->setAuthority(); $this->path = $this->formatPath($path); $this->query = Encoder::encodeQueryOrFragment($query); $this->fragment = Encoder::encodeQueryOrFragment($fragment); + $components = $this->toComponents(); + $this->userInfo = $this->formatUserInfo($this->user, $this->pass); + $this->authority = UriString::buildAuthority($components); + $this->uri = UriString::build($components); $this->assertValidState(); - - $this->uri = $this->getUriString(); } /** @@ -716,27 +716,6 @@ private static function fetchRequestUri(array $server): array return [$server['PHP_SELF'], $server['QUERY_STRING']]; } - /** - * Generate the URI authority part. - */ - private function setAuthority(): ?string - { - $authority = null; - if (null !== $this->host) { - $authority = $this->host; - } - - if (null !== $this->userInfo) { - $authority = $this->userInfo.'@'.$authority; - } - - if (null !== $this->port) { - return $authority.':'.$this->port; - } - - return $authority; - } - /** * Format the Path component. */ @@ -875,7 +854,7 @@ private function assertValidState(): void 'ws', 'wss' => $this->isNonEmptyHostUriWithoutFragment(), default => true, }) { - throw new SyntaxError('The uri `'.$this->getUriString().'` is invalid for the `'.$this->scheme.'` scheme.'); + throw new SyntaxError('The uri `'.$this->uri.'` is invalid for the `'.$this->scheme.'` scheme.'); } } @@ -928,16 +907,6 @@ private function isNonEmptyHostUriWithoutFragmentAndQuery(): bool return $this->isNonEmptyHostUri() && null === $this->fragment && null === $this->query; } - /** - * Generate the URI string representation from its components. - * - * @link https://tools.ietf.org/html/rfc3986#section-5.3 - */ - private function getUriString(): string - { - return UriString::build($this->toComponents()); - } - public function toString(): string { return $this->uri; @@ -1105,17 +1074,16 @@ public function withUserInfo( Stringable|string|null $user, #[SensitiveParameter] Stringable|string|null $password = null ): UriInterface { - $userInfo = ('' !== $user) ? $this->formatUserInfo( - Encoder::encodeUser($this->filterString($user)), - Encoder::encodePassword($this->filterString($password)) - ) : null; + $user = Encoder::encodeUser($this->filterString($user)); + $pass = Encoder::encodePassword($this->filterString($password)); + $userInfo = ('' !== $user) ? $this->formatUserInfo($user, $pass) : null; return match ($userInfo) { $this->userInfo => $this, default => new self( $this->scheme, - $user instanceof Stringable ? $user->__toString() : $user, - $password instanceof Stringable ? $password->__toString() : $password, + $user, + $pass, $this->host, $this->port, $this->path, @@ -1165,12 +1133,9 @@ public function withPort(int|null $port): UriInterface public function withPath(Stringable|string $path): UriInterface { - $path = $this->filterString($path); - if (null === $path) { - throw new SyntaxError('The path component cannot be null.'); - } - - $path = $this->formatPath($path); + $path = $this->formatPath( + $this->filterString($path) ?? throw new SyntaxError('The path component cannot be null.') + ); return match ($path) { $this->path => $this,