2020use League \Uri \UriTemplate \Template ;
2121use League \Uri \UriTemplate \TemplateCanNotBeExpanded ;
2222use League \Uri \UriTemplate \VariableBag ;
23+ use Psr \Http \Message \UriFactoryInterface ;
24+ use Psr \Http \Message \UriInterface as Psr7UriInterface ;
2325use Stringable ;
2426use Uri \InvalidUriException ;
2527use Uri \Rfc3986 \Uri as Rfc3986Uri ;
@@ -150,11 +152,33 @@ public function expandToUri(iterable $variables = [], Rfc3986Uri|WhatWgUrl|Strin
150152 * @throws InvalidUrlException if the base URI cannot be converted to a Uri\Whatwg\Url instance
151153 * @throws InvalidUrlException if the resulting expansion cannot be converted to a Uri\Whatwg\Url instance
152154 */
153- public function expandToUrl (iterable $ variables = [], WhatWgUrl |Stringable |string |null $ baseUrl = null ): WhatWgUrl
155+ public function expandToUrl (iterable $ variables = [], Rfc3986Uri | WhatWgUrl |Stringable |string |null $ baseUrl = null ): WhatWgUrl
154156 {
155157 return new WhatWgUrl ($ this ->templateExpanded ($ variables ), $ this ->newWhatWgUrl ($ baseUrl ));
156158 }
157159
160+ /**
161+ * @throws TemplateCanNotBeExpanded if the variables are invalid
162+ * @throws UriException if the resulting expansion cannot be converted to a UriInterface instance
163+ */
164+ public function expandToPsr7Uri (
165+ iterable $ variables = [],
166+ Rfc3986Uri |WhatWgUrl |Stringable |string |null $ baseUrl = null ,
167+ UriFactoryInterface $ uriFactory = new HttpFactory ()
168+ ): Psr7UriInterface {
169+ $ uriString = $ this ->templateExpandedOrFail ($ variables );
170+
171+ return $ uriFactory ->createUri (
172+ null === $ baseUrl
173+ ? $ uriString
174+ : UriString::resolve ($ uriString , match (true ) {
175+ $ baseUrl instanceof Rfc3986Uri => $ baseUrl ->toRawString (),
176+ $ baseUrl instanceof WhatWgUrl => $ baseUrl ->toUnicodeString (),
177+ default => $ baseUrl ,
178+ })
179+ );
180+ }
181+
158182 /**
159183 * @throws TemplateCanNotBeExpanded if the variables are invalid or missing
160184 * @throws UriException if the resulting expansion cannot be converted to a UriInterface instance
@@ -178,14 +202,39 @@ public function expandToUriOrFail(iterable $variables = [], Rfc3986Uri|WhatWgUrl
178202
179203 /**
180204 * @throws TemplateCanNotBeExpanded if the variables are invalid
181- * @throws InvalidUriException if the base URI cannot be converted to a Uri\Whatwg\Url instance
182- * @throws InvalidUriException if the resulting expansion cannot be converted to a Uri\Whatwg\Url instance
205+ * @throws InvalidUrlException if the base URI cannot be converted to a Uri\Whatwg\Url instance
206+ * @throws InvalidUrlException if the resulting expansion cannot be converted to a Uri\Whatwg\Url instance
183207 */
184208 public function expandToUrlOrFail (iterable $ variables = [], Rfc3986Uri |WhatWgUrl |Stringable |string |null $ baseUrl = null ): WhatWgUrl
185209 {
186210 return new WhatWgUrl ($ this ->templateExpandedOrFail ($ variables ), $ this ->newWhatWgUrl ($ baseUrl ));
187211 }
188212
213+ /**
214+ * @throws TemplateCanNotBeExpanded if the variables are invalid
215+ * @throws UriException if the resulting expansion cannot be converted to a UriInterface instance
216+ */
217+ public function expandToPsr7UriOrFail (
218+ iterable $ variables = [],
219+ Rfc3986Uri |WhatWgUrl |Stringable |string |null $ baseUrl = null ,
220+ UriFactoryInterface $ uriFactory = new HttpFactory ()
221+ ): Psr7UriInterface {
222+ $ uriString = $ this ->templateExpandedOrFail ($ variables );
223+
224+ return $ uriFactory ->createUri (
225+ null === $ baseUrl
226+ ? $ uriString
227+ : UriString::resolve ($ uriString , match (true ) {
228+ $ baseUrl instanceof Rfc3986Uri => $ baseUrl ->toRawString (),
229+ $ baseUrl instanceof WhatWgUrl => $ baseUrl ->toUnicodeString (),
230+ default => $ baseUrl ,
231+ })
232+ );
233+ }
234+
235+ /**
236+ * @throws InvalidUrlException
237+ */
189238 private function newWhatWgUrl (Rfc3986Uri |WhatWgUrl |Stringable |string |null $ url = null ): ?WhatWgUrl
190239 {
191240 return match (true ) {
@@ -196,13 +245,16 @@ private function newWhatWgUrl(Rfc3986Uri|WhatWgUrl|Stringable|string|null $url =
196245 };
197246 }
198247
199- private function newRfc3986Uri (Rfc3986Uri |WhatWgUrl |Stringable |string |null $ url = null ): ?Rfc3986Uri
248+ /**
249+ * @throws InvalidUriException
250+ */
251+ private function newRfc3986Uri (Rfc3986Uri |WhatWgUrl |Stringable |string |null $ uri = null ): ?Rfc3986Uri
200252 {
201253 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 ),
254+ null === $ uri => null ,
255+ $ uri instanceof Rfc3986Uri => $ uri ,
256+ $ uri instanceof WhatWgUrl => new Rfc3986Uri ($ uri ->toAsciiString ()),
257+ default => new Rfc3986Uri ((string ) $ uri ),
206258 };
207259 }
208260
0 commit comments