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,23 @@ 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+ return $ uriFactory ->createUri ($ this ->expand ($ variables , $ baseUrl )->toString ());
170+ }
171+
158172 /**
159173 * @throws TemplateCanNotBeExpanded if the variables are invalid or missing
160174 * @throws UriException if the resulting expansion cannot be converted to a UriInterface instance
@@ -178,14 +192,29 @@ public function expandToUriOrFail(iterable $variables = [], Rfc3986Uri|WhatWgUrl
178192
179193 /**
180194 * @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
195+ * @throws InvalidUrlException if the base URI cannot be converted to a Uri\Whatwg\Url instance
196+ * @throws InvalidUrlException if the resulting expansion cannot be converted to a Uri\Whatwg\Url instance
183197 */
184198 public function expandToUrlOrFail (iterable $ variables = [], Rfc3986Uri |WhatWgUrl |Stringable |string |null $ baseUrl = null ): WhatWgUrl
185199 {
186200 return new WhatWgUrl ($ this ->templateExpandedOrFail ($ variables ), $ this ->newWhatWgUrl ($ baseUrl ));
187201 }
188202
203+ /**
204+ * @throws TemplateCanNotBeExpanded if the variables are invalid
205+ * @throws UriException if the resulting expansion cannot be converted to a UriInterface instance
206+ */
207+ public function expandToPsr7UriOrFail (
208+ iterable $ variables = [],
209+ Rfc3986Uri |WhatWgUrl |Stringable |string |null $ baseUrl = null ,
210+ UriFactoryInterface $ uriFactory = new HttpFactory ()
211+ ): Psr7UriInterface {
212+ return $ uriFactory ->createUri ($ this ->expandOrFail ($ variables , $ baseUrl )->toString ());
213+ }
214+
215+ /**
216+ * @throws InvalidUrlException
217+ */
189218 private function newWhatWgUrl (Rfc3986Uri |WhatWgUrl |Stringable |string |null $ url = null ): ?WhatWgUrl
190219 {
191220 return match (true ) {
@@ -196,13 +225,16 @@ private function newWhatWgUrl(Rfc3986Uri|WhatWgUrl|Stringable|string|null $url =
196225 };
197226 }
198227
199- private function newRfc3986Uri (Rfc3986Uri |WhatWgUrl |Stringable |string |null $ url = null ): ?Rfc3986Uri
228+ /**
229+ * @throws InvalidUriException
230+ */
231+ private function newRfc3986Uri (Rfc3986Uri |WhatWgUrl |Stringable |string |null $ uri = null ): ?Rfc3986Uri
200232 {
201233 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 ),
234+ null === $ uri => null ,
235+ $ uri instanceof Rfc3986Uri => $ uri ,
236+ $ uri instanceof WhatWgUrl => new Rfc3986Uri ($ uri ->toAsciiString ()),
237+ default => new Rfc3986Uri ((string ) $ uri ),
206238 };
207239 }
208240
0 commit comments