Skip to content

Commit

Permalink
Improve UriTemplate interoperability with PSR-13
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Jan 25, 2025
1 parent ec1d0d3 commit 2973737
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ All Notable changes to `League\Uri` will be documented in this file
- `Uri::getUser` returns the encoded user component of the URI an alias for `Uri::getUsername`
- `Uri::fromMarkdownAnchor`
- `Uri::fromHtmlAnchor`
- `UriTemplate` implements the `Stringable` interface

### Fixed

Expand Down
30 changes: 28 additions & 2 deletions UriTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace League\Uri;

use Deprecated;
use League\Uri\Contracts\UriException;
use League\Uri\Contracts\UriInterface;
use League\Uri\Exceptions\SyntaxError;
Expand All @@ -31,8 +32,10 @@
* @package League\Uri
* @author Ignace Nyamagana Butera <[email protected]>
* @since 6.1.0
*
* @phpstan-import-type InputValue from VariableBag
*/
final class UriTemplate
final class UriTemplate implements Stringable
{
private readonly Template $template;
private readonly VariableBag $defaultVariables;
Expand Down Expand Up @@ -60,19 +63,27 @@ private function filterVariables(iterable $variables): VariableBag
));
}

public function getTemplate(): string
/**
* Returns the string representation of the UriTemplate.
*/
public function __toString(): string
{
return $this->template->value;
}

/**
* Returns the distinct variables placeholders used in the template.
*
* @return array<string>
*/
public function getVariableNames(): array
{
return $this->template->variableNames;
}

/**
* @return array<string, InputValue>
*/
public function getDefaultVariables(): array
{
return iterator_to_array($this->defaultVariables);
Expand Down Expand Up @@ -120,4 +131,19 @@ public function expandOrFail(iterable $variables = []): UriInterface
$this->filterVariables($variables)->replace($this->defaultVariables)
));
}

/**
* DEPRECATION WARNING! This method will be removed in the next major point release.
*
* @deprecated Since version 7.6.0
* @codeCoverageIgnore
* @see UriTemplate::toString()
*
* Create a new instance from the environment.
*/
#[Deprecated(message:'use League\Uri\UriTemplate::__toString() instead', since:'league/uri:7.6.0')]
public function getTemplate(): string
{
return $this->__toString();
}
}
4 changes: 1 addition & 3 deletions UriTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ public function testGetTemplate(): void
'foo[]' => ['fizz', 'buzz'],
];

$uriTemplate = new UriTemplate($template, $variables);

self::assertSame($template, $uriTemplate->getTemplate());
self::assertSame($template, (string) new UriTemplate($template, $variables));
}

public function testGetDefaultVariables(): void
Expand Down

0 comments on commit 2973737

Please sign in to comment.