Skip to content

Commit

Permalink
Update BaseUri public API
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Jul 3, 2023
1 parent a477245 commit 6901d80
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
37 changes: 21 additions & 16 deletions BaseUri.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string,int>
Expand All @@ -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);
Expand All @@ -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;
}

Expand All @@ -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
Expand All @@ -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();
Expand Down Expand Up @@ -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];
Expand All @@ -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.
*/
Expand Down
8 changes: 4 additions & 4 deletions Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions UriResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

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

0 comments on commit 6901d80

Please sign in to comment.