diff --git a/BaseUri.php b/BaseUri.php index 973536f3..acae263f 100644 --- a/BaseUri.php +++ b/BaseUri.php @@ -157,15 +157,14 @@ private static function reducer(array $carry, string $segment): array private function resolvePathAndQuery(Psr7UriInterface|UriInterface $uri): array { $targetPath = $uri->getPath(); - $targetQuery = $uri->getQuery(); $null = $uri instanceof Psr7UriInterface ? '' : null; - $baseNull = $this->value instanceof Psr7UriInterface ? '' : null; if (str_starts_with($targetPath, '/')) { - return [$targetPath, $targetQuery]; + return [$targetPath, $uri->getQuery()]; } if ('' === $targetPath) { + $targetQuery = $uri->getQuery(); if ($null === $targetQuery) { $targetQuery = $this->value->getQuery(); } @@ -173,7 +172,7 @@ private function resolvePathAndQuery(Psr7UriInterface|UriInterface $uri): array $targetPath = $this->value->getPath(); //@codeCoverageIgnoreStart //because some PSR-7 Uri implementations allow this RFC3986 forbidden construction - if ($baseNull !== $this->value->getAuthority() && !str_starts_with($targetPath, '/')) { + if (null !== $this->value->getAuthority() && !str_starts_with($targetPath, '/')) { $targetPath = '/'.$targetPath; } //@codeCoverageIgnoreEnd @@ -182,7 +181,7 @@ private function resolvePathAndQuery(Psr7UriInterface|UriInterface $uri): array } $basePath = $this->value->getPath(); - if ($baseNull !== $this->value->getAuthority() && '' === $basePath) { + if (null !== $this->value->getAuthority() && '' === $basePath) { $targetPath = '/'.$targetPath; } @@ -194,14 +193,14 @@ private function resolvePathAndQuery(Psr7UriInterface|UriInterface $uri): array } } - return [$targetPath, $targetQuery]; + return [$targetPath, $uri->getQuery()]; } /** - * Relativizes a URI according to a base URI. + * Relativize a URI according to a base URI. * * This method MUST retain the state of the submitted URI instance, and return - * an URI instance of the same type that contains the applied modifications. + * a URI instance of the same type that contains the applied modifications. * * This method MUST be transparent when dealing with error and exceptions. * It MUST not alter of silence them apart from validating its own parameters. @@ -217,19 +216,13 @@ public function relativize(Stringable|string $uri): Psr7UriInterface|UriInterfac $uri = $uri->withScheme($null)->withPort(null)->withUserInfo($null)->withHost($null); $targetPath = $uri->getPath(); $basePath = $this->value->getPath(); - if ($targetPath !== $basePath) { - return $uri->withPath(self::relativizePath($targetPath, $basePath)); - } - - if (self::componentEquals('query', $uri)) { - return $uri->withPath('')->withQuery($null); - } - if ($null === $uri->getQuery()) { - return $uri->withPath(self::formatPathWithEmptyBaseQuery($targetPath)); - } - - return $uri->withPath(''); + return 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(''), + }; } /** diff --git a/CHANGELOG.md b/CHANGELOG.md index 49d007b4..b6894f26 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ All Notable changes to `League\Uri` will be documented in this file - `League\Uri\Http::fromTemplate` - `League\Uri\UriTemplate::expandOrFail` - `League\Uri\UriTemplate\Template::expandOrFail` +- `League\Uri\UriTemplate\TemplateCanNotBeExpanded` - `League\Uri\UriString::parseAuthority` - `League\Uri\UriString::buildAuthority` - `League\Uri\BaseUri` @@ -53,7 +54,7 @@ All Notable changes to `League\Uri` will be documented in this file - Support for `__set_state` - `League\Uri\UriTemplate\VariableBag::all` -- Support for `PSR-7` v1 +- `League\Uri\Exceptions\TemplateCanNotBeExpanded` use `League\Uri\UriTemplate\TemplateCanNotBeExpanded` instead ## [6.8.0](https://github.com/thephpleague/uri/compare/6.7.2...6.8.0) - 2022-09-13 diff --git a/UriTemplate.php b/UriTemplate.php index 53684df3..037e4daa 100644 --- a/UriTemplate.php +++ b/UriTemplate.php @@ -16,8 +16,8 @@ use League\Uri\Contracts\UriException; use League\Uri\Contracts\UriInterface; use League\Uri\Exceptions\SyntaxError; -use League\Uri\Exceptions\TemplateCanNotBeExpanded; use League\Uri\UriTemplate\Template; +use League\Uri\UriTemplate\TemplateCanNotBeExpanded; use League\Uri\UriTemplate\VariableBag; use Stringable; use function array_fill_keys; @@ -37,8 +37,8 @@ final class UriTemplate public readonly VariableBag $defaultVariables; /** - * @throws SyntaxError if the template syntax is invalid - * @throws TemplateCanNotBeExpanded if the template or the variables are invalid + * @throws SyntaxError if the template syntax is invalid + * @throws \League\Uri\UriTemplate\TemplateCanNotBeExpanded if the template or the variables are invalid */ public function __construct(Template|Stringable|string $template, iterable $defaultVariables = []) { diff --git a/UriTemplate/ExpressionTest.php b/UriTemplate/ExpressionTest.php index 20f81942..e99c6796 100644 --- a/UriTemplate/ExpressionTest.php +++ b/UriTemplate/ExpressionTest.php @@ -14,7 +14,6 @@ namespace League\Uri\UriTemplate; use League\Uri\Exceptions\SyntaxError; -use League\Uri\Exceptions\TemplateCanNotBeExpanded; use PHPUnit\Framework\TestCase; /** diff --git a/UriTemplate/Operator.php b/UriTemplate/Operator.php index ca9f23cf..23ce95f5 100644 --- a/UriTemplate/Operator.php +++ b/UriTemplate/Operator.php @@ -14,7 +14,6 @@ namespace League\Uri\UriTemplate; use League\Uri\Exceptions\SyntaxError; -use League\Uri\Exceptions\TemplateCanNotBeExpanded; use Stringable; use function implode; use function is_array; diff --git a/UriTemplate/Template.php b/UriTemplate/Template.php index 098d9062..98e09d39 100644 --- a/UriTemplate/Template.php +++ b/UriTemplate/Template.php @@ -14,7 +14,6 @@ namespace League\Uri\UriTemplate; use League\Uri\Exceptions\SyntaxError; -use League\Uri\Exceptions\TemplateCanNotBeExpanded; use Stringable; use function array_fill_keys; use function array_filter; diff --git a/Exceptions/TemplateCanNotBeExpanded.php b/UriTemplate/TemplateCanNotBeExpanded.php similarity index 97% rename from Exceptions/TemplateCanNotBeExpanded.php rename to UriTemplate/TemplateCanNotBeExpanded.php index 4554ee80..c742e398 100644 --- a/Exceptions/TemplateCanNotBeExpanded.php +++ b/UriTemplate/TemplateCanNotBeExpanded.php @@ -11,7 +11,7 @@ declare(strict_types=1); -namespace League\Uri\Exceptions; +namespace League\Uri\UriTemplate; use InvalidArgumentException; use League\Uri\Contracts\UriException; diff --git a/UriTemplate/VariableBag.php b/UriTemplate/VariableBag.php index c0ac5c37..85f0f8f7 100644 --- a/UriTemplate/VariableBag.php +++ b/UriTemplate/VariableBag.php @@ -17,7 +17,6 @@ use Closure; use Countable; use IteratorAggregate; -use League\Uri\Exceptions\TemplateCanNotBeExpanded; use Stringable; use Traversable; use function array_filter; diff --git a/UriTemplate/VariableBagTest.php b/UriTemplate/VariableBagTest.php index 019c3153..87efd026 100644 --- a/UriTemplate/VariableBagTest.php +++ b/UriTemplate/VariableBagTest.php @@ -14,7 +14,6 @@ namespace League\Uri\UriTemplate; use ArrayIterator; -use League\Uri\Exceptions\TemplateCanNotBeExpanded; use PHPUnit\Framework\TestCase; use stdClass; use TypeError; diff --git a/UriTemplateTest.php b/UriTemplateTest.php index 350910b5..00ab2f42 100644 --- a/UriTemplateTest.php +++ b/UriTemplateTest.php @@ -14,7 +14,7 @@ namespace League\Uri; use League\Uri\Exceptions\SyntaxError; -use League\Uri\Exceptions\TemplateCanNotBeExpanded; +use League\Uri\UriTemplate\TemplateCanNotBeExpanded; use PHPUnit\Framework\TestCase; /** @@ -356,7 +356,7 @@ public function testExpansionWithMultipleSameExpression(): void public function testExpandOrFailIfVariablesAreMissing(): void { - $this->expectException(TemplateCanNotBeExpanded::class); + $this->expectException(UriTemplate\TemplateCanNotBeExpanded::class); (new UriTemplate('{var}'))->expandOrFail([]); }