Skip to content

Commit 5e43ea4

Browse files
committed
Improve UriTemplate interoperability with PSR-13
1 parent e53e391 commit 5e43ea4

File tree

4 files changed

+54
-6
lines changed

4 files changed

+54
-6
lines changed

docs/uri/7.0/uri-template.md

+24-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ URI Template
99
The `League\Uri\UriTemplate` class enables expanding a URI object based on a URI template and its
1010
submitted parameters following [RFC 6570 URI Template](http://tools.ietf.org/html/rfc6570).
1111

12-
1312
## Template expansion
1413

1514
The `UriTemplate::expand` public method expands a URI template to generate a valid URI conforming
@@ -187,3 +186,27 @@ $uriTemplate = new UriTemplate($template);
187186
echo $uriTemplate->expand($params), PHP_EOL;
188187
// https://example.com/hotels/%7B/Rest%20%26%20Relax
189188
~~~
189+
190+
## Interoperability
191+
192+
<p class="message-info">Available since <code>version 7.0</code></p>
193+
194+
To allow easier integration with other PHP packages and especially [PSR-13](https://www.php-fig.org/psr/psr-13/)
195+
the `UriTemplate` class implements the `Stringable` interface.
196+
197+
~~~php
198+
use League\Uri\UriTemplate;
199+
use Symfony\Component\WebLink\Link;
200+
201+
$uriTemplate = new UriTemplate('https://google.com/search{?q*}');
202+
203+
$link = (new Link())
204+
->withHref($uriTemplate)
205+
->withRel('next')
206+
->withAttribute('me', 'you');
207+
208+
// Once serialized will return
209+
// '<https://google.com/search{?q*}>; rel="next"; me="you"'
210+
~~~
211+
212+
The `Symfony\Component\WebLink\Link` package implements `PSR-13` interfaces.

uri/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ All Notable changes to `League\Uri` will be documented in this file
1616
- `Uri::getUser` returns the encoded user component of the URI an alias for `Uri::getUsername`
1717
- `Uri::fromMarkdownAnchor`
1818
- `Uri::fromHtmlAnchor`
19+
- `UriTemplate` implements the `Stringable` interface
1920

2021
### Fixed
2122

uri/UriTemplate.php

+28-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace League\Uri;
1515

16+
use Deprecated;
1617
use League\Uri\Contracts\UriException;
1718
use League\Uri\Contracts\UriInterface;
1819
use League\Uri\Exceptions\SyntaxError;
@@ -31,8 +32,10 @@
3132
* @package League\Uri
3233
* @author Ignace Nyamagana Butera <[email protected]>
3334
* @since 6.1.0
35+
*
36+
* @phpstan-import-type InputValue from VariableBag
3437
*/
35-
final class UriTemplate
38+
final class UriTemplate implements Stringable
3639
{
3740
private readonly Template $template;
3841
private readonly VariableBag $defaultVariables;
@@ -60,19 +63,27 @@ private function filterVariables(iterable $variables): VariableBag
6063
));
6164
}
6265

63-
public function getTemplate(): string
66+
/**
67+
* Returns the string representation of the UriTemplate.
68+
*/
69+
public function __toString(): string
6470
{
6571
return $this->template->value;
6672
}
6773

6874
/**
75+
* Returns the distinct variables placeholders used in the template.
76+
*
6977
* @return array<string>
7078
*/
7179
public function getVariableNames(): array
7280
{
7381
return $this->template->variableNames;
7482
}
7583

84+
/**
85+
* @return array<string, InputValue>
86+
*/
7687
public function getDefaultVariables(): array
7788
{
7889
return iterator_to_array($this->defaultVariables);
@@ -120,4 +131,19 @@ public function expandOrFail(iterable $variables = []): UriInterface
120131
$this->filterVariables($variables)->replace($this->defaultVariables)
121132
));
122133
}
134+
135+
/**
136+
* DEPRECATION WARNING! This method will be removed in the next major point release.
137+
*
138+
* @deprecated Since version 7.6.0
139+
* @codeCoverageIgnore
140+
* @see UriTemplate::toString()
141+
*
142+
* Create a new instance from the environment.
143+
*/
144+
#[Deprecated(message:'use League\Uri\UriTemplate::__toString() instead', since:'league/uri:7.6.0')]
145+
public function getTemplate(): string
146+
{
147+
return $this->__toString();
148+
}
123149
}

uri/UriTemplateTest.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ public function testGetTemplate(): void
3333
'foo[]' => ['fizz', 'buzz'],
3434
];
3535

36-
$uriTemplate = new UriTemplate($template, $variables);
37-
38-
self::assertSame($template, $uriTemplate->getTemplate());
36+
self::assertSame($template, (string) new UriTemplate($template, $variables));
3937
}
4038

4139
public function testGetDefaultVariables(): void

0 commit comments

Comments
 (0)