@@ -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