Skip to content

Commit

Permalink
Update and 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 6901d80 commit bec4c67
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
33 changes: 23 additions & 10 deletions BaseUri.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,12 @@ public function isCrossOrigin(Stringable|string $uri): bool
/**
* Input URI normalization to allow Stringable and string URI.
*/
private static function filterUri(Stringable|string $uri): Psr7UriInterface|UriInterface
private static function filterUri(Stringable|string $uri, Psr7UriInterface|UriInterface|null $href = null): Psr7UriInterface|UriInterface
{
return match (true) {
$uri instanceof self => $uri->value,
$uri instanceof Psr7UriInterface, $uri instanceof UriInterface => $uri,
default => Uri::new($uri),
default => $href instanceof Psr7UriInterface ? self::createPsr7Uri($uri, $href) : Uri::new($uri),
};
}

Expand All @@ -197,7 +197,7 @@ private static function filterUri(Stringable|string $uri): Psr7UriInterface|UriI
*/
public function resolve(Stringable|string $uri): self
{
$uri = self::filterUri($uri);
$uri = self::filterUri($uri, $this->value);
$null = $uri instanceof Psr7UriInterface ? '' : null;

if ($null !== $uri->getScheme()) {
Expand All @@ -206,13 +206,8 @@ public function resolve(Stringable|string $uri): self
}

if ($null !== $uri->getAuthority()) {
$scheme = $this->value->getScheme();
if (null === $scheme || '' === $null) {
$scheme = '';
}

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

Expand Down Expand Up @@ -338,7 +333,7 @@ private function resolvePathAndQuery(Psr7UriInterface|UriInterface $uri): array
*/
public function relativize(Stringable|string $uri): self
{
$uri = self::formatHost(self::filterUri($uri));
$uri = self::formatHost(self::filterUri($uri, $this->value));
if (!$this->isRelativizable($uri)) {
return new self($uri);
}
Expand Down Expand Up @@ -478,4 +473,22 @@ private static function formatPathWithEmptyBaseQuery(string $path): string

return '' === $basename ? './' : $basename;
}

private static function createPsr7Uri(Stringable|string $uri, Psr7UriInterface $href): Psr7UriInterface
{
if ($href instanceof Http) {
return Http::new($uri);
}

$components = UriString::parse($uri);
return $href
->withFragment($components['fragment'] ?? '')
->withQuery($components['query'] ?? '')
->withPath($components['path'])
->withScheme($components['scheme'] ?? '')
->withHost($components['host'] ?? '')
->withUserInfo($components['user'] ?? '', $components['pass'])
->withPort($components['port'])
;
}
}
23 changes: 23 additions & 0 deletions BaseUriTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace League\Uri;

use GuzzleHttp\Psr7\Utils;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\UriInterface as Psr7UriInterface;

Expand Down Expand Up @@ -376,4 +377,26 @@ public static function getCrossOriginExamples(): array
'cross origin using a blob' => ['blob:http://mozilla.org:443/', 'https://mozilla.org/123', true],
];
}

/**
* @dataProvider resolveProvider
*/
public function testResolveWithPsr7Implementation(string $baseUri, string $uri, string $expected): void
{
$resolvedUri = BaseUri::new(Utils::uriFor($baseUri))->resolve($uri);

self::assertInstanceOf(\GuzzleHttp\Psr7\Uri::class, $resolvedUri->uri());
self::assertSame($expected, (string) $resolvedUri);
}

/**
* @dataProvider relativizeProvider
*/
public function testRelativizeWithPsr7Implementation(string $uri, string $resolved, string $expected): void
{
$relativizedUri = BaseUri::new(Utils::uriFor($uri))->relativize($resolved);

self::assertInstanceOf(\GuzzleHttp\Psr7\Uri::class, $relativizedUri->uri());
self::assertSame($expected, (string) $relativizedUri);
}
}

0 comments on commit bec4c67

Please sign in to comment.