Skip to content

Commit

Permalink
Improve BaseUri public API
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Jul 3, 2023
1 parent 4059b3b commit a477245
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
34 changes: 22 additions & 12 deletions BaseUri.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace League\Uri;

use JsonSerializable;
use League\Uri\Contracts\UriInterface;
use Psr\Http\Message\UriInterface as Psr7UriInterface;
use Stringable;
Expand All @@ -27,7 +28,7 @@
use function strpos;
use function substr;

final class BaseUri implements Stringable
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';
Expand All @@ -37,7 +38,7 @@ final class BaseUri implements Stringable
*/
private const DOT_SEGMENTS = ['.' => 1, '..' => 1];

public readonly Psr7UriInterface|UriInterface|null $origin;
private readonly Psr7UriInterface|UriInterface|null $origin;
private readonly ?string $nullValue;

private function __construct(
Expand Down Expand Up @@ -71,6 +72,11 @@ public static function new(Stringable|string $baseUri): self
return new self(self::filterUri($baseUri));
}

public function jsonSerialize(): mixed
{
return $this->value->__toString();
}

public function __toString(): string
{
return $this->value->__toString();
Expand Down Expand Up @@ -106,19 +112,19 @@ public function isRelativePath(): bool
*/
public function isSameDocument(Stringable|string $uri): bool
{
return self::normalize(self::filterUri($uri)) === self::normalize($this->value);
return $this->normalize(self::filterUri($uri)) === $this->normalize($this->value);
}

/**
* Normalizes a URI for comparison.
*/
private static function normalize(Psr7UriInterface|UriInterface $uri): string
private function normalize(Psr7UriInterface|UriInterface $uri): string
{
$null = $uri instanceof Psr7UriInterface ? '' : null;

$path = $uri->getPath();
if ('/' === ($path[0] ?? '') || '' !== $uri->getScheme().$uri->getAuthority()) {
$path = BaseUri::new($uri->withPath('')->withQuery($null))->resolve($uri)->value->getPath();
$path = $this->removeDotSegments($path);
}

$query = $uri->getQuery();
Expand All @@ -129,12 +135,9 @@ private static function normalize(Psr7UriInterface|UriInterface $uri): string
self::REGEXP_ENCODED_CHARS,
static fn (array $matches): string => rawurldecode($matches[0]),
[$path, implode('&', $pairs)]
);

if (null !== $value) {
[$path, $query] = $value + ['', $null];
}
) ?? ['', $null];

[$path, $query] = $value + ['', $null];
if ($null !== $uri->getAuthority() && '' === $path) {
$path = '/';
}
Expand All @@ -147,10 +150,17 @@ private static 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.
*
* @see UriInfo::getOrigin()
*/
public function isCrossOrigin(Stringable|string $uri): bool
{
Expand Down
2 changes: 1 addition & 1 deletion BaseUriTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ public static function sameValueAsProvider(): array
*/
public function testGetOrigin(Psr7UriInterface|Uri $uri, ?string $expectedOrigin): void
{
self::assertSame($expectedOrigin, BaseUri::new($uri)->origin?->__toString());
self::assertSame($expectedOrigin, BaseUri::new($uri)->origin()?->__toString());
}

public static function getOriginProvider(): array
Expand Down
2 changes: 1 addition & 1 deletion UriInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static function isSameDocument(Stringable|string $uri, Stringable|string
*/
public static function getOrigin(Stringable|string $uri): ?string
{
return BaseUri::new($uri)->origin?->__toString();
return BaseUri::new($uri)->origin()?->__toString();
}

/**
Expand Down

0 comments on commit a477245

Please sign in to comment.