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 2, 2023
1 parent 370b44f commit 4f963cf
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 22 deletions.
37 changes: 20 additions & 17 deletions BaseUri.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,25 @@ final class BaseUri implements Stringable
*/
private const DOT_SEGMENTS = ['.' => 1, '..' => 1];

public readonly ?string $origin;
public readonly ?UriInterface $origin;

private function __construct(
public readonly UriInterface $value
public readonly Psr7UriInterface|UriInterface $value
) {
$this->origin = UriInfo::getOrigin($this->value);
$origin = UriInfo::getOrigin($this->value);
if (null !== $origin) {
$this->origin = Uri::new($origin);
}
}

public static function new(Stringable|string $baseUri): self
{
return new self(Uri::new($baseUri));
return new self(self::filterUri($baseUri));
}

public function __toString(): string
{
return $this->value->toString();
return $this->value->__toString();
}

/**
Expand All @@ -72,14 +75,14 @@ private static function filterUri(Stringable|string $uri): Psr7UriInterface|UriI
* This method MUST be transparent when dealing with error and exceptions.
* It MUST not alter or silence them apart from validating its own parameters.
*/
public function resolve(Stringable|string $uri): Psr7UriInterface|UriInterface
public function resolve(Stringable|string $uri): self
{
$uri = self::filterUri($uri);
$null = $uri instanceof Psr7UriInterface ? '' : null;

if ($null !== $uri->getScheme()) {
return $uri
->withPath(self::removeDotSegments($uri->getPath()));
return new self($uri
->withPath(self::removeDotSegments($uri->getPath())));
}

if ($null !== $uri->getAuthority()) {
Expand All @@ -88,9 +91,9 @@ public function resolve(Stringable|string $uri): Psr7UriInterface|UriInterface
$scheme = '';
}

return $uri
return new self($uri
->withScheme($scheme)
->withPath(self::removeDotSegments($uri->getPath()));
->withPath(self::removeDotSegments($uri->getPath())));
}

$user = $null;
Expand All @@ -102,13 +105,13 @@ public function resolve(Stringable|string $uri): Psr7UriInterface|UriInterface

[$path, $query] = $this->resolvePathAndQuery($uri);

return $uri
return new self($uri
->withPath($this->removeDotSegments($path))
->withQuery($query)
->withHost($this->value->getHost())
->withPort($this->value->getPort())
->withUserInfo((string) $user, $pass)
->withScheme($this->value->getScheme())
->withScheme($this->value->getScheme()))
;
}

Expand Down Expand Up @@ -213,24 +216,24 @@ private function resolvePathAndQuery(Psr7UriInterface|UriInterface $uri): array
* This method MUST be transparent when dealing with error and exceptions.
* It MUST not alter of silence them apart from validating its own parameters.
*/
public function relativize(Stringable|string $uri): Psr7UriInterface|UriInterface
public function relativize(Stringable|string $uri): self
{
$uri = self::formatHost(self::filterUri($uri));
if (!$this->isRelativizable($uri)) {
return $uri;
return new self($uri);
}

$null = $uri instanceof Psr7UriInterface ? '' : null;
$uri = $uri->withScheme($null)->withPort(null)->withUserInfo($null)->withHost($null);
$targetPath = $uri->getPath();
$basePath = $this->value->getPath();

return match (true) {
return new self(match (true) {
$targetPath !== $basePath => $uri->withPath(self::relativizePath($targetPath, $basePath)),
self::componentEquals('query', $uri) => $uri->withPath('')->withQuery($null),
$null === $uri->getQuery() => $uri->withPath(self::formatPathWithEmptyBaseQuery($targetPath)),
default => $uri->withPath(''),
};
});
}

/**
Expand Down Expand Up @@ -365,6 +368,6 @@ public function isCrossOrigin(Stringable|string $uri): bool
{
return null === $this->origin
|| null === ($uriString = UriInfo::getOrigin($uri))
|| $uriString !== $this->origin;
|| $uriString !== $this->origin->toString();
}
}
4 changes: 2 additions & 2 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);
$uri = BaseUri::new($uri->withFragment(null)->withQuery(null)->withPath(''))->resolve($uri)->value;

return $uri;
}
Expand All @@ -461,7 +461,7 @@ public static function fromBaseUri(Stringable|String $uri, Stringable|String|nul
}

/** @var UriInterface $uri */
$uri = $baseUri->resolve($uri);
$uri = $baseUri->resolve($uri)->value;

return $uri;
}
Expand Down
2 changes: 1 addition & 1 deletion UriInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private static function normalize(Psr7UriInterface|UriInterface $uri): Psr7UriIn

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

$query = $uri->getQuery();
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);
return BaseUri::new($baseUri)->resolve($uri)->value;
}

/**
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);
return BaseUri::new($baseUri)->relativize($uri)->value;
}
}

0 comments on commit 4f963cf

Please sign in to comment.