2121use League \Uri \UriTemplate \TemplateCanNotBeExpanded ;
2222use League \Uri \UriTemplate \VariableBag ;
2323use Stringable ;
24+ use Uri \InvalidUriException ;
25+ use Uri \Rfc3986 \Uri as Rfc3986Uri ;
26+ use Uri \WhatWg \Url as WhatWgUrl ;
2427
2528use function array_fill_keys ;
2629use 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