diff --git a/BaseUri.php b/BaseUri.php index 502a0d13..017bf777 100644 --- a/BaseUri.php +++ b/BaseUri.php @@ -30,8 +30,7 @@ final class BaseUri implements Stringable, JsonSerializable { - private const WHATWG_SPECIAL_SCHEMES = ['ftp', 'http', 'https', 'ws', 'wss']; - private const REGEXP_ENCODED_CHARS = ',%(2[D|E]|3\d|4[1-9|A-F]|5[\d|AF]|6[1-9|A-F]|7[\d|E]),i'; + private const WHATWG_SPECIAL_SCHEMES = ['ftp' => 1, 'http' => 1, 'https' => 1, 'ws' => 1, 'wss' => 1]; /** * @var array @@ -42,7 +41,7 @@ final class BaseUri implements Stringable, JsonSerializable private readonly ?string $nullValue; private function __construct( - public readonly Psr7UriInterface|UriInterface $value + private readonly Psr7UriInterface|UriInterface $value ) { $this->nullValue = $this->value instanceof Psr7UriInterface ? '' : null; $this->origin = $this->computeOrigin($this->value, $this->nullValue); @@ -56,7 +55,7 @@ private function computeOrigin(Psr7UriInterface|UriInterface $uri, ?string $null $scheme = $uri->getScheme(); } - if (!in_array($scheme, self::WHATWG_SPECIAL_SCHEMES, true)) { + if (!isset(self::WHATWG_SPECIAL_SCHEMES[$scheme])) { return null; } @@ -67,9 +66,9 @@ private function computeOrigin(Psr7UriInterface|UriInterface $uri, ?string $null ->withUserInfo($nullValue); } - public static function new(Stringable|string $baseUri): self + public static function new(Stringable|string $uri): self { - return new self(self::filterUri($baseUri)); + return new self(self::filterUri($uri)); } public function jsonSerialize(): mixed @@ -82,6 +81,20 @@ public function __toString(): string return $this->value->__toString(); } + public function uri(): Psr7UriInterface|UriInterface + { + return $this->value; + } + + public function origin(): ?self + { + if (null === $this->origin) { + return null; + } + + return new self($this->origin); + } + public function isAbsolute(): bool { return $this->nullValue !== $this->value->getScheme(); @@ -131,8 +144,9 @@ private function normalize(Psr7UriInterface|UriInterface $uri): string $pairs = null === $query ? [] : explode('&', $query); sort($pairs); + static $regexpEncodedChars = ',%(2[D|E]|3\d|4[1-9|A-F]|5[\d|AF]|6[1-9|A-F]|7[\d|E]),i'; $value = preg_replace_callback( - self::REGEXP_ENCODED_CHARS, + $regexpEncodedChars, static fn (array $matches): string => rawurldecode($matches[0]), [$path, implode('&', $pairs)] ) ?? ['', $null]; @@ -150,15 +164,6 @@ private function normalize(Psr7UriInterface|UriInterface $uri): string ->__toString(); } - public function origin(): ?self - { - if (null === $this->origin) { - return null; - } - - return new self($this->origin); - } - /** * Tells whether two URI do not share the same origin. */ diff --git a/Uri.php b/Uri.php index cc7b4fd6..17c8a05a 100644 --- a/Uri.php +++ b/Uri.php @@ -447,7 +447,7 @@ public static function fromBaseUri(Stringable|String $uri, Stringable|String|nul } /** @var UriInterface $uri */ - $uri = BaseUri::new($uri->withFragment(null)->withQuery(null)->withPath(''))->resolve($uri)->value; + $uri = BaseUri::new($uri->withFragment(null)->withQuery(null)->withPath(''))->resolve($uri)->uri(); return $uri; } @@ -456,12 +456,12 @@ public static function fromBaseUri(Stringable|String $uri, Stringable|String|nul $baseUri = BaseUri::new($baseUri); } - if (null === $baseUri->value->getScheme()) { - throw new SyntaxError('the base URI `'.$baseUri->value.'` must be absolute.'); + if (null === $baseUri->uri()->getScheme()) { + throw new SyntaxError('the base URI `'.$baseUri.'` must be absolute.'); } /** @var UriInterface $uri */ - $uri = $baseUri->resolve($uri)->value; + $uri = $baseUri->resolve($uri)->uri(); return $uri; } diff --git a/UriResolver.php b/UriResolver.php index f7162a22..9a889c5b 100644 --- a/UriResolver.php +++ b/UriResolver.php @@ -35,7 +35,7 @@ final class UriResolver */ public static function resolve(Stringable|string $uri, Stringable|string $baseUri): Psr7UriInterface|UriInterface { - return BaseUri::new($baseUri)->resolve($uri)->value; + return BaseUri::new($baseUri)->resolve($uri)->uri(); } /** @@ -49,6 +49,6 @@ public static function resolve(Stringable|string $uri, Stringable|string $baseUr */ public static function relativize(Stringable|string $uri, Stringable|string $baseUri): Psr7UriInterface|UriInterface { - return BaseUri::new($baseUri)->relativize($uri)->value; + return BaseUri::new($baseUri)->relativize($uri)->uri(); } }