From a055d600fe575d38678b0de48d405850ff7737f3 Mon Sep 17 00:00:00 2001 From: ignace nyamagana butera Date: Thu, 11 Jul 2024 21:06:27 +0200 Subject: [PATCH] Deprecating getComponents and adding BaseUri::isOpaque --- BaseUri.php | 12 ++++++++++++ BaseUriTest.php | 41 +++++++++++++++++++++++++++++++++++++++++ CHANGELOG.md | 1 + Http.php | 28 +++++++++++++++++++++++----- Uri.php | 20 +++++++++++++++++--- UriTest.php | 2 +- 6 files changed, 95 insertions(+), 9 deletions(-) diff --git a/BaseUri.php b/BaseUri.php index 2b44ecaa..5de77a93 100644 --- a/BaseUri.php +++ b/BaseUri.php @@ -185,6 +185,18 @@ public function isLocalFile(): bool }; } + /** + * Tells whether the URI is opaque or not. + * + * A URI is opaque if and only if it is absolute + * and does not has an authority path. + */ + public function isOpaque(): bool + { + return $this->nullValue === $this->uri->getAuthority() + && $this->isAbsolute(); + } + /** * Tells whether two URI do not share the same origin. */ diff --git a/BaseUriTest.php b/BaseUriTest.php index be04edc8..f5b9ebb3 100644 --- a/BaseUriTest.php +++ b/BaseUriTest.php @@ -16,6 +16,7 @@ use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use Psr\Http\Message\UriInterface as Psr7UriInterface; @@ -583,4 +584,44 @@ public static function rfc8089UriProvider(): iterable ], ]; } + + #[DataProvider('opaqueUriProvider')] + #[Test] + public function it_tells_if_an_uri_is_opaque(bool $expected, string $uri): void + { + self::assertSame($expected, BaseUri::from($uri)->isOpaque()); + } + + public static function opaqueUriProvider(): iterable + { + yield 'empty URI' => [ + 'expected' => false, + 'uri' => '', + ]; + + yield 'relative URI' => [ + 'expected' => false, + 'uri' => 'path?query#fragment', + ]; + + yield 'URI with authority' => [ + 'expected' => false, + 'uri' => '//authority/path?query#fragment', + ]; + + yield 'absolute HTTP URI' => [ + 'expected' => false, + 'uri' => 'https://authority/path?query#fragment', + ]; + + yield 'absolute mail URI' => [ + 'expected' => true, + 'uri' => 'mail:foo@example.com', + ]; + + yield 'data URI' => [ + 'expected' => true, + 'uri' => 'data:', + ]; + } } diff --git a/CHANGELOG.md b/CHANGELOG.md index 72ad2904..f9ae9ebd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ All Notable changes to `League\Uri` will be documented in this file - `Uri::getUsername` returns the encoded user component of the URI. - `Uri::getPassword` returns the encoded password component of the URI. +- `BaseUri::isOpaque` tells whether a URI is opaque. ### Fixed diff --git a/Http.php b/Http.php index 58e5dafc..d2fce116 100644 --- a/Http.php +++ b/Http.php @@ -67,7 +67,7 @@ private function normalizePsr7Uri(UriInterface $uri): UriInterface return match ($components) { [] => $uri, - default => Uri::fromComponents([...$uri->getComponents(), ...$components]), /* @phpstan-ignore-line */ + default => Uri::fromComponents([...$uri->toComponents(), ...$components]), }; } @@ -76,10 +76,7 @@ private function normalizePsr7Uri(UriInterface $uri): UriInterface */ public static function new(Stringable|string $uri = ''): self { - return match (true) { - $uri instanceof UriInterface => new self($uri), - default => new self(Uri::new($uri)), - }; + return self::fromComponents(UriString::parse($uri)); } /** @@ -90,6 +87,27 @@ public static function new(Stringable|string $uri = ''): self */ public static function fromComponents(array $components): self { + $components += [ + 'scheme' => null, 'user' => null, 'pass' => null, 'host' => null, + 'port' => null, 'path' => '', 'query' => null, 'fragment' => null, + ]; + + if ($components['user'] === '') { + $components['user'] = null; + } + + if ($components['pass'] === '') { + $components['pass'] = null; + } + + if ($components['query'] === '') { + $components['query'] = null; + } + + if ($components['fragment'] === '') { + $components['fragment'] = null; + } + return new self(Uri::fromComponents($components)); } diff --git a/Uri.php b/Uri.php index b406ff9f..2d5a6bae 100644 --- a/Uri.php +++ b/Uri.php @@ -384,7 +384,7 @@ private function formatPort(?int $port = null): ?int public static function new(#[SensitiveParameter] Stringable|string $uri = ''): self { $components = match (true) { - $uri instanceof UriInterface => $uri->getComponents(), + $uri instanceof UriInterface => $uri->toComponents(), default => UriString::parse($uri), }; @@ -430,7 +430,7 @@ public static function fromBaseUri( public static function fromTemplate(UriTemplate|Stringable|string $template, iterable $variables = []): self { return match (true) { - $template instanceof UriTemplate => self::fromComponents($template->expand($variables)->getComponents()), + $template instanceof UriTemplate => self::fromComponents($template->expand($variables)->toComponents()), $template instanceof UriTemplate\Template => self::new($template->expand($variables)), default => self::new(UriTemplate\Template::new($template)->expand($variables)), }; @@ -978,7 +978,7 @@ public function jsonSerialize(): string /** * @return ComponentMap */ - public function getComponents(): array + public function toComponents(): array { return [ 'scheme' => $this->scheme, @@ -1241,6 +1241,20 @@ public function withFragment(Stringable|string|null $fragment): UriInterface }; } + /** + * DEPRECATION WARNING! This method will be removed in the next major point release. + * + * @deprecated Since version 7.5.0 + * @codeCoverageIgnore + * @see Uri::toComponents() + * + * @return ComponentMap + */ + public function getComponents(): array + { + return $this->toComponents(); + } + /** * DEPRECATION WARNING! This method will be removed in the next major point release. * diff --git a/UriTest.php b/UriTest.php index e5f762f5..460d027a 100644 --- a/UriTest.php +++ b/UriTest.php @@ -55,7 +55,7 @@ public function testAutomaticUrlNormalization(): void $uri = Uri::new($raw); self::assertSame($normalized, $uri->toString()); - self::assertSame($components, $uri->getComponents()); + self::assertSame($components, $uri->toComponents()); } public function testAutomaticUrlNormalizationBis(): void