Skip to content

Commit 531c8b7

Browse files
committed
Update UriTemplate API to keep te method consistent
1 parent 2d045d9 commit 531c8b7

File tree

4 files changed

+41
-16
lines changed

4 files changed

+41
-16
lines changed

components/Modifier.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@
5858
use function rtrim;
5959
use function str_ends_with;
6060
use function str_starts_with;
61-
6261
use function strtolower;
6362
use function trim;
63+
6464
use const FILTER_FLAG_IPV4;
6565
use const FILTER_VALIDATE_IP;
6666

docs/uri/7.0/uri-template.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ echo $uriTemplate->expandToUrl($params, 'https://api.twitter.com')->toAsciiStrin
102102
echo $uriTemplate->expandToUrl($params); //will throw
103103
~~~
104104

105+
<p class="message-info">The same feature of adding a base URI is also supported for the
106+
other two methods <code>expand()</code> and <code>expandToUri</code> but only if the
107+
baseURI variable is not <code>null</code>.</p>
108+
105109
### Updating the default variables
106110

107111
At any given time you may update your default variables but since the `UriTemplate`
@@ -124,7 +128,7 @@ $newUriTemplate = $uriTemplate->withDefaultVariables(['version' => '1.1']);
124128
$newUriTemplate->getDefaultVariables(); //returns new VariableBag(['version' => '1.1'])
125129
~~~
126130

127-
<p class="message-warning">Following RFC6570 requirements means not support for
131+
<p class="message-warning">Following RFC6570 requirements means not support for
128132
nested array like the one used with <code>http_build_query</code></p>
129133

130134
~~~php

uri/Uri.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@
4040
use TypeError;
4141
use Uri\Rfc3986\Uri as Rfc3986Uri;
4242
use Uri\WhatWg\Url as WhatWgUrl;
43-
4443
use ValueError;
44+
4545
use function array_filter;
4646
use function array_key_last;
4747
use function array_map;
@@ -1201,8 +1201,12 @@ public function toString(): string
12011201
public function toDisplayString(): string
12021202
{
12031203
$components = $this->toComponents();
1204+
$port = null;
1205+
if (isset($components['port'])) {
1206+
$port = (int) $components['port'];
1207+
unset($components['port']);
1208+
}
12041209

1205-
unset($components['port']);
12061210
if (null !== $components['host']) {
12071211
$components['host'] = IdnaConverter::toUnicode($components['host'])->domain();
12081212
}
@@ -1218,12 +1222,12 @@ public function toDisplayString(): string
12181222
$components['fragment'] = Encoder::decodeFragment($components['fragment']);
12191223

12201224
return UriString::build([
1221-
...array_map(fn (string|null $value) => match (true) {
1225+
...array_map(fn (?string $value) => match (true) {
12221226
null === $value,
12231227
!str_contains($value, '%20') => $value,
12241228
default => str_replace('%20', ' ', $value),
12251229
}, $components),
1226-
...['port' => $this->port],
1230+
...['port' => $port],
12271231
]);
12281232
}
12291233

uri/UriTemplate.php

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,18 +128,21 @@ private function templateExpandedOrFail(iterable $variables = []): string
128128
* @throws TemplateCanNotBeExpanded if the variables are invalid
129129
* @throws UriException if the resulting expansion cannot be converted to a UriInterface instance
130130
*/
131-
public function expand(iterable $variables = []): UriInterface
131+
public function expand(iterable $variables = [], Rfc3986Uri|WhatWgUrl|Stringable|string|null $baseUri = null): UriInterface
132132
{
133-
return Uri::new($this->templateExpanded($variables));
133+
$expanded = $this->templateExpanded($variables);
134+
135+
return null === $baseUri ? Uri::new($expanded) : Uri::fromBaseUri($expanded, $baseUri);
134136
}
135137

136138
/**
137139
* @throws TemplateCanNotBeExpanded if the variables are invalid
140+
* @throws InvalidUriException if the base URI cannot be converted to a Uri\Rfc3986\Uri instance
138141
* @throws InvalidUriException if the resulting expansion cannot be converted to a Uri\Rfc3986\Uri instance
139142
*/
140-
public function expandToUri(iterable $variables = []): Rfc3986Uri
143+
public function expandToUri(iterable $variables = [], Rfc3986Uri|WhatWgUrl|Stringable|string|null $baseUri = null): Rfc3986Uri
141144
{
142-
return new Rfc3986Uri($this->templateExpanded($variables));
145+
return new Rfc3986Uri($this->templateExpanded($variables), $this->newRfc3986Uri($baseUri));
143146
}
144147

145148
/**
@@ -156,39 +159,53 @@ public function expandToUrl(iterable $variables = [], WhatWgUrl|Stringable|strin
156159
* @throws TemplateCanNotBeExpanded if the variables are invalid or missing
157160
* @throws UriException if the resulting expansion cannot be converted to a UriInterface instance
158161
*/
159-
public function expandOrFail(iterable $variables = []): UriInterface
162+
public function expandOrFail(iterable $variables = [], Rfc3986Uri|WhatWgUrl|Stringable|string|null $baseUri = null): UriInterface
160163
{
161-
return Uri::new($this->templateExpandedOrFail($variables));
164+
$expanded = $this->templateExpandedOrFail($variables);
165+
166+
return null === $baseUri ? Uri::new($expanded) : Uri::fromBaseUri($expanded, $baseUri);
162167
}
163168

164169
/**
165170
* @throws TemplateCanNotBeExpanded if the variables are invalid
171+
* @throws InvalidUriException if the base URI cannot be converted to a Uri\Rfc3986\Uri instance
166172
* @throws InvalidUriException if the resulting expansion cannot be converted to a Uri\Rfc3986\Uri instance
167173
*/
168-
public function expandToUriOrFail(iterable $variables = []): Rfc3986Uri
174+
public function expandToUriOrFail(iterable $variables = [], Rfc3986Uri|WhatWgUrl|Stringable|string|null $baseUri = null): Rfc3986Uri
169175
{
170-
return new Rfc3986Uri($this->templateExpandedOrFail($variables));
176+
return new Rfc3986Uri($this->templateExpandedOrFail($variables), $this->newRfc3986Uri($baseUri));
171177
}
172178

173179
/**
174180
* @throws TemplateCanNotBeExpanded if the variables are invalid
175181
* @throws InvalidUriException if the base URI cannot be converted to a Uri\Whatwg\Url instance
176182
* @throws InvalidUriException if the resulting expansion cannot be converted to a Uri\Whatwg\Url instance
177183
*/
178-
public function expandToUrlOrFail(iterable $variables = [], WhatWgUrl|Stringable|string|null $baseUrl = null): WhatWgUrl
184+
public function expandToUrlOrFail(iterable $variables = [], Rfc3986Uri|WhatWgUrl|Stringable|string|null $baseUrl = null): WhatWgUrl
179185
{
180186
return new WhatWgUrl($this->templateExpandedOrFail($variables), $this->newWhatWgUrl($baseUrl));
181187
}
182188

183-
private function newWhatWgUrl(WhatWgUrl|Stringable|string|null $url = null): ?WhatWgUrl
189+
private function newWhatWgUrl(Rfc3986Uri|WhatWgUrl|Stringable|string|null $url = null): ?WhatWgUrl
184190
{
185191
return match (true) {
186192
null === $url => null,
187193
$url instanceof WhatWgUrl => $url,
194+
$url instanceof Rfc3986Uri => new WhatWgUrl($url->toRawString()),
188195
default => new WhatWgUrl((string) $url),
189196
};
190197
}
191198

199+
private function newRfc3986Uri(Rfc3986Uri|WhatWgUrl|Stringable|string|null $url = null): ?Rfc3986Uri
200+
{
201+
return match (true) {
202+
null === $url => null,
203+
$url instanceof Rfc3986Uri => $url,
204+
$url instanceof WhatWgUrl => new Rfc3986Uri($url->toAsciiString()),
205+
default => new Rfc3986Uri((string) $url),
206+
};
207+
}
208+
192209
/**
193210
* DEPRECATION WARNING! This method will be removed in the next major point release.
194211
*

0 commit comments

Comments
 (0)