@@ -50,7 +50,7 @@ class UrlImmutable implements \JsonSerializable
5050 private string $ path = '' ;
5151 private array $ query = [];
5252 private string $ fragment = '' ;
53- private string $ authority = '' ;
53+ private ? string $ authority = null ;
5454
5555
5656 /**
@@ -60,15 +60,14 @@ public function __construct(string|self|Url $url)
6060 {
6161 $ url = is_string ($ url ) ? new Url ($ url ) : $ url ;
6262 [$ this ->scheme , $ this ->user , $ this ->password , $ this ->host , $ this ->port , $ this ->path , $ this ->query , $ this ->fragment ] = $ url ->export ();
63- $ this ->build ();
6463 }
6564
6665
6766 public function withScheme (string $ scheme ): static
6867 {
6968 $ dolly = clone $ this ;
7069 $ dolly ->scheme = $ scheme ;
71- $ dolly ->build () ;
70+ $ dolly ->authority = null ;
7271 return $ dolly ;
7372 }
7473
@@ -83,7 +82,7 @@ public function withUser(string $user): static
8382 {
8483 $ dolly = clone $ this ;
8584 $ dolly ->user = $ user ;
86- $ dolly ->build () ;
85+ $ dolly ->authority = null ;
8786 return $ dolly ;
8887 }
8988
@@ -98,7 +97,7 @@ public function withPassword(string $password): static
9897 {
9998 $ dolly = clone $ this ;
10099 $ dolly ->password = $ password ;
101- $ dolly ->build () ;
100+ $ dolly ->authority = null ;
102101 return $ dolly ;
103102 }
104103
@@ -113,7 +112,7 @@ public function withoutUserInfo(): static
113112 {
114113 $ dolly = clone $ this ;
115114 $ dolly ->user = $ dolly ->password = '' ;
116- $ dolly ->build () ;
115+ $ dolly ->authority = null ;
117116 return $ dolly ;
118117 }
119118
@@ -122,8 +121,8 @@ public function withHost(string $host): static
122121 {
123122 $ dolly = clone $ this ;
124123 $ dolly ->host = $ host ;
125- $ dolly ->build () ;
126- return $ dolly ;
124+ $ dolly ->authority = null ;
125+ return $ dolly-> setPath ( $ dolly -> path ) ;
127126 }
128127
129128
@@ -149,7 +148,7 @@ public function withPort(int $port): static
149148 {
150149 $ dolly = clone $ this ;
151150 $ dolly ->port = $ port ;
152- $ dolly ->build () ;
151+ $ dolly ->authority = null ;
153152 return $ dolly ;
154153 }
155154
@@ -168,10 +167,14 @@ public function getDefaultPort(): ?int
168167
169168 public function withPath (string $ path ): static
170169 {
171- $ dolly = clone $ this ;
172- $ dolly ->path = $ path ;
173- $ dolly ->build ();
174- return $ dolly ;
170+ return (clone $ this )->setPath ($ path );
171+ }
172+
173+
174+ private function setPath (string $ path ): static
175+ {
176+ $ this ->path = $ this ->host && !str_starts_with ($ path , '/ ' ) ? '/ ' . $ path : $ path ;
177+ return $ this ;
175178 }
176179
177180
@@ -185,7 +188,6 @@ public function withQuery(string|array $query): static
185188 {
186189 $ dolly = clone $ this ;
187190 $ dolly ->query = is_array ($ query ) ? $ query : Url::parseQuery ($ query );
188- $ dolly ->build ();
189191 return $ dolly ;
190192 }
191193
@@ -220,7 +222,6 @@ public function withFragment(string $fragment): static
220222 {
221223 $ dolly = clone $ this ;
222224 $ dolly ->fragment = $ fragment ;
223- $ dolly ->build ();
224225 return $ dolly ;
225226 }
226227
@@ -247,7 +248,15 @@ public function getAbsoluteUrl(): string
247248 */
248249 public function getAuthority (): string
249250 {
250- return $ this ->authority ;
251+ return $ this ->authority ??= $ this ->host === ''
252+ ? ''
253+ : ($ this ->user !== ''
254+ ? rawurlencode ($ this ->user ) . ($ this ->password === '' ? '' : ': ' . rawurlencode ($ this ->password )) . '@ '
255+ : '' )
256+ . $ this ->host
257+ . ($ this ->port && $ this ->port !== $ this ->getDefaultPort ()
258+ ? ': ' . $ this ->port
259+ : '' );
251260 }
252261
253262
@@ -256,8 +265,8 @@ public function getAuthority(): string
256265 */
257266 public function getHostUrl (): string
258267 {
259- return ($ this ->scheme ? $ this ->scheme . ': ' : ' ' )
260- . ($ this ->authority !== '' ? '// ' . $ this ->authority : '' );
268+ return ($ this ->scheme === '' ? '' : $ this ->scheme . ': ' )
269+ . ($ this ->host === '' ? '' : ' // ' . $ this ->getAuthority () );
261270 }
262271
263272
@@ -284,22 +293,4 @@ final public function export(): array
284293 {
285294 return [$ this ->scheme , $ this ->user , $ this ->password , $ this ->host , $ this ->port , $ this ->path , $ this ->query , $ this ->fragment ];
286295 }
287-
288-
289- protected function build (): void
290- {
291- if ($ this ->host && !str_starts_with ($ this ->path , '/ ' )) {
292- $ this ->path = '/ ' . $ this ->path ;
293- }
294-
295- $ this ->authority = $ this ->host === ''
296- ? ''
297- : ($ this ->user !== ''
298- ? rawurlencode ($ this ->user ) . ($ this ->password === '' ? '' : ': ' . rawurlencode ($ this ->password )) . '@ '
299- : '' )
300- . $ this ->host
301- . ($ this ->port && $ this ->port !== $ this ->getDefaultPort ()
302- ? ': ' . $ this ->port
303- : '' );
304- }
305296}
0 commit comments