Skip to content

Commit

Permalink
Deprecating getComponents and adding BaseUri::isOpaque
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Jul 11, 2024
1 parent cf57ec6 commit a055d60
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 9 deletions.
12 changes: 12 additions & 0 deletions BaseUri.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
41 changes: 41 additions & 0 deletions BaseUriTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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:',
];
}
}
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
28 changes: 23 additions & 5 deletions Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]),
};
}

Expand All @@ -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));
}

/**
Expand All @@ -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));
}

Expand Down
20 changes: 17 additions & 3 deletions Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
};

Expand Down Expand Up @@ -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)),
};
Expand Down Expand Up @@ -978,7 +978,7 @@ public function jsonSerialize(): string
/**
* @return ComponentMap
*/
public function getComponents(): array
public function toComponents(): array
{
return [
'scheme' => $this->scheme,
Expand Down Expand Up @@ -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.
*
Expand Down
2 changes: 1 addition & 1 deletion UriTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a055d60

Please sign in to comment.