Skip to content

Commit f061d07

Browse files
committed
UriTemplate can now returns PHP Uri objects
1 parent dd34569 commit f061d07

File tree

3 files changed

+94
-7
lines changed

3 files changed

+94
-7
lines changed

docs/uri/7.0/uri-template.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,16 @@ $template = 'https://example.com/hotels/{hotel}/bookings/{booking}';
2323
$params = ['booking' => '42', 'hotel' => 'Rest & Relax'];
2424

2525
$uriTemplate = new UriTemplate($template);
26-
$uri = $uriTemplate->expand($params); // instance of League\Uri\Uri
26+
$uri = $uriTemplate->expand($params); // instance of League\Uri\Uri
27+
$rfc3986Uri = $uriTemplate->expandToUri($params); // instance of Uri\Rfc3986\Uri
28+
$whatWgUrl = $uriTemplate->expandToUrl($params); // instance of Uri\Whatwg\Url
29+
2730
echo $uri; //display https://example.com/hotels/Rest%20%26%20Relax/bookings/42"
2831
~~~
2932

33+
<p class="message-notice"><code>expandToUri()</code> and <code>expandToUrl()</code> are available since
34+
version <code>7.6.0</code></p>
35+
3036
## Template variables
3137

3238
<p class="message-notice">For maximum interoperability you should make sure your variables are
@@ -72,6 +78,30 @@ echo $uriTemplate->expand($params), PHP_EOL;
7278
//displays https://api.twitter.com/2.0/search/j/john/?q=a&q=b&limit=10
7379
~~~
7480

81+
The `expandToUri()` and the `expandToUrl()` methods will act exactly like the `expand()` method
82+
but will instead return a `Uri\Rfc3986\Uri` and a `Uri\Whatwg\Url` object respectively.
83+
84+
<p class="message-warning">a WHATWG URL must always be absolute if the URI template is not you
85+
should provide to <code>expandToUrl()</code> a base URL otherwise an <code>Uri\WhatWg\InvalidUrlException</code>
86+
exception will be thrown.</p>
87+
88+
~~~php
89+
$template = '/{version}/search/{term:1}/{term}/{?q*,limit}';
90+
//the template represents a non-absolute URL
91+
92+
$params = [
93+
'term' => 'john',
94+
'q' => ['a', 'b'],
95+
'limit' => '10',
96+
'version' => '2.0'
97+
];
98+
99+
$uriTemplate = new UriTemplate($template, ['version' => '1.1']);
100+
101+
echo $uriTemplate->expandToUrl($params, 'https://api.twitter.com')->toAsciiString(); //works
102+
echo $uriTemplate->expandToUrl($params); //will throw
103+
~~~
104+
75105
### Updating the default variables
76106

77107
At any given time you may update your default variables but since the `UriTemplate`

uri/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ All Notable changes to `League\Uri` will be documented in this file
3333
- `Uri::withUsername` returns a new `Uri` instance with the updated username component.
3434
- `Uri::withPassword` returns a new `Uri` instance with the updated password component.
3535
- `UriTemplate` implements the `Stringable` interface
36+
- `UriTemplate::expandToUri` and `UriTemplate::expandToUrl`
37+
- `UriTemplate::expandToUriOrFail` and `UriTemplate::expandToUrlOrFail`
3638
- Dependency to `psr/http-factory` package which is required for the package.
3739
- Support for `Uri\Rfc3986\Uri` and `Uri\Whatwg\Url`
3840

uri/UriTemplate.php

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
use League\Uri\UriTemplate\TemplateCanNotBeExpanded;
2222
use League\Uri\UriTemplate\VariableBag;
2323
use Stringable;
24+
use Uri\InvalidUriException;
25+
use Uri\Rfc3986\Uri as Rfc3986Uri;
26+
use Uri\WhatWg\Url as WhatWgUrl;
2427

2528
use function array_fill_keys;
2629
use function array_key_exists;
@@ -110,15 +113,46 @@ public function withDefaultVariables(iterable $defaultVariables): self
110113
return new self($this->template, $defaultVariables);
111114
}
112115

116+
private function templateExpanded(iterable $variables = []): string
117+
{
118+
return $this->template->expand($this->filterVariables($variables)->replace($this->defaultVariables));
119+
}
120+
121+
private function templateExpandedOrFail(iterable $variables = []): string
122+
{
123+
return $this->template->expandOrFail($this->filterVariables($variables)->replace($this->defaultVariables));
124+
}
125+
113126
/**
114127
* @throws TemplateCanNotBeExpanded if the variables are invalid
115128
* @throws UriException if the resulting expansion cannot be converted to a UriInterface instance
116129
*/
117130
public function expand(iterable $variables = []): UriInterface
118131
{
119-
return Uri::new($this->template->expand(
120-
$this->filterVariables($variables)->replace($this->defaultVariables)
121-
));
132+
return Uri::new($this->templateExpanded($variables));
133+
}
134+
135+
/**
136+
* @throws TemplateCanNotBeExpanded if the variables are invalid
137+
* @throws InvalidUriException if the resulting expansion cannot be converted to a Uri\Rfc3986\Uri instance
138+
*/
139+
public function expandToUri(iterable $variables = []): Rfc3986Uri
140+
{
141+
return new Rfc3986Uri($this->template->expand($variables));
142+
}
143+
144+
/**
145+
* @throws TemplateCanNotBeExpanded if the variables are invalid
146+
* @throws InvalidUriException if the base URI cannot be converted to a Uri\Whatwg\Url instance
147+
* @throws InvalidUriException if the resulting expansion cannot be converted to a Uri\Whatwg\Url instance
148+
*/
149+
public function expandToUrl(iterable $variables = [], WhatWgUrl|Stringable|string|null $baseUri = null): WhatWgUrl
150+
{
151+
return new WhatWgUrl($this->templateExpanded($variables), match (true) {
152+
null === $baseUri => null,
153+
$baseUri instanceof WhatWgUrl => $baseUri,
154+
default => new WhatWgUrl((string) $baseUri),
155+
});
122156
}
123157

124158
/**
@@ -127,9 +161,30 @@ public function expand(iterable $variables = []): UriInterface
127161
*/
128162
public function expandOrFail(iterable $variables = []): UriInterface
129163
{
130-
return Uri::new($this->template->expandOrFail(
131-
$this->filterVariables($variables)->replace($this->defaultVariables)
132-
));
164+
return Uri::new($this->templateExpandedOrFail($variables));
165+
}
166+
167+
/**
168+
* @throws TemplateCanNotBeExpanded if the variables are invalid
169+
* @throws InvalidUriException if the resulting expansion cannot be converted to a Uri\Rfc3986\Uri instance
170+
*/
171+
public function expandToUriOrFail(iterable $variables = []): Rfc3986Uri
172+
{
173+
return new Rfc3986Uri($this->template->expandOrFail($variables));
174+
}
175+
176+
/**
177+
* @throws TemplateCanNotBeExpanded if the variables are invalid
178+
* @throws InvalidUriException if the base URI cannot be converted to a Uri\Whatwg\Url instance
179+
* @throws InvalidUriException if the resulting expansion cannot be converted to a Uri\Whatwg\Url instance
180+
*/
181+
public function expandToUrlOrFail(iterable $variables = [], WhatWgUrl|Stringable|string|null $baseUri = null): WhatWgUrl
182+
{
183+
return new WhatWgUrl($this->templateExpandedOrFail($variables), match (true) {
184+
null === $baseUri => null,
185+
$baseUri instanceof WhatWgUrl => $baseUri,
186+
default => new WhatWgUrl((string) $baseUri),
187+
});
133188
}
134189

135190
/**

0 commit comments

Comments
 (0)