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 e53e391 commit 5e43ea4
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 deletions.
25 changes: 24 additions & 1 deletion docs/uri/7.0/uri-template.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ URI Template
The `League\Uri\UriTemplate` class enables expanding a URI object based on a URI template and its
submitted parameters following [RFC 6570 URI Template](http://tools.ietf.org/html/rfc6570).


## Template expansion

The `UriTemplate::expand` public method expands a URI template to generate a valid URI conforming
Expand Down Expand Up @@ -187,3 +186,27 @@ $uriTemplate = new UriTemplate($template);
echo $uriTemplate->expand($params), PHP_EOL;
// https://example.com/hotels/%7B/Rest%20%26%20Relax
~~~

## Interoperability

<p class="message-info">Available since <code>version 7.0</code></p>

To allow easier integration with other PHP packages and especially [PSR-13](https://www.php-fig.org/psr/psr-13/)
the `UriTemplate` class implements the `Stringable` interface.

~~~php
use League\Uri\UriTemplate;
use Symfony\Component\WebLink\Link;

$uriTemplate = new UriTemplate('https://google.com/search{?q*}');

$link = (new Link())
->withHref($uriTemplate)
->withRel('next')
->withAttribute('me', 'you');

// Once serialized will return
// '<https://google.com/search{?q*}>; rel="next"; me="you"'
~~~

The `Symfony\Component\WebLink\Link` package implements `PSR-13` interfaces.
1 change: 1 addition & 0 deletions uri/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 uri/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 uri/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 5e43ea4

Please sign in to comment.