@@ -73,45 +73,6 @@ public function __construct(string $uri, ?string $baseUri = null)
7373 $ this ->rawComponents = self ::addUserInfo ($ components );
7474 }
7575
76- /**
77- * @return array{__uri: string}
78- */
79- public function __serialize (): array
80- {
81- return ['__uri ' => $ this ->toRawString ()];
82- }
83-
84- /**
85- * @param array{__uri: string} $data
86- *
87- * @throws Exception|InvalidUriException
88- */
89- public function __unserialize (array $ data ): void
90- {
91- $ uri = new self ($ data ['__uri ' ] ?? throw new Exception ('The `__uri` property is missing from the serialized object. ' ));
92-
93- $ this ->rawComponents = $ uri ->rawComponents ;
94- $ this ->rawUri = $ uri ->rawUri ;
95- $ this ->isNormalized = false ;
96- }
97-
98- /**
99- * @return array{scheme: ?string, user: ?string, password: ?string, host: ?string, port: ?int, path: ?string, query: ?string, fragment: ?string}
100- */
101- public function __debugInfo (): array
102- {
103- return [
104- 'scheme ' => $ this ->rawComponents ['scheme ' ],
105- 'user ' => $ this ->rawComponents ['user ' ],
106- 'password ' => $ this ->rawComponents ['pass ' ],
107- 'host ' => $ this ->rawComponents ['host ' ],
108- 'port ' => $ this ->rawComponents ['port ' ],
109- 'path ' => $ this ->rawComponents ['path ' ],
110- 'query ' => $ this ->rawComponents ['query ' ],
111- 'fragment ' => $ this ->rawComponents ['fragment ' ],
112- ];
113- }
114-
11576 /**
11677 * @throws InvalidUriException
11778 */
@@ -160,17 +121,6 @@ public static function parse(string $uri, ?string $baseUri = null): ?Uri
160121 }
161122 }
162123
163- private function setNormalizedComponents (): void
164- {
165- if (!$ this ->isNormalized ) {
166- $ this ->normalizedComponents = [
167- ...self ::addUserInfo (UriString::parseNormalized ($ this ->rawUri )),
168- ...['host ' => Encoder::normalizeHost ($ this ->rawComponents ['host ' ])],
169- ];
170- $ this ->isNormalized = true ;
171- }
172- }
173-
174124 /**
175125 * @param self::TYPE_RAW|self::TYPE_NORMALIZED $type
176126 */
@@ -188,6 +138,17 @@ private function getComponent(string $type, string $name): ?string
188138 return (string ) $ value ;
189139 }
190140
141+ private function setNormalizedComponents (): void
142+ {
143+ if (!$ this ->isNormalized ) {
144+ $ this ->normalizedComponents = [
145+ ...self ::addUserInfo (UriString::parseNormalized ($ this ->rawUri )),
146+ ...['host ' => Encoder::normalizeHost ($ this ->rawComponents ['host ' ])],
147+ ];
148+ $ this ->isNormalized = true ;
149+ }
150+ }
151+
191152 /**
192153 * @param InputComponentMap $components
193154 *
@@ -217,12 +178,12 @@ public function getRawScheme(): ?string
217178 /**
218179 * @throws InvalidUriException
219180 */
220- public function withScheme (?string $ encodedScheme ): self
181+ public function withScheme (?string $ scheme ): self
221182 {
222183 return match (true ) {
223- $ encodedScheme === $ this ->getRawScheme () => $ this ,
224- UriString::isScheme ($ encodedScheme ) => $ this ->withComponent (['scheme ' => $ encodedScheme ]),
225- default => throw new InvalidUriException ('The scheme string component ` ' .$ encodedScheme .'` is an invalid scheme. ' ),
184+ $ scheme === $ this ->getRawScheme () => $ this ,
185+ UriString::isScheme ($ scheme ) => $ this ->withComponent (['scheme ' => $ scheme ]),
186+ default => throw new InvalidUriException ('The scheme string component ` ' .$ scheme .'` is an invalid scheme. ' ),
226187 };
227188 }
228189
@@ -239,17 +200,17 @@ public function getRawUserInfo(): ?string
239200 /**
240201 * @throws InvalidUriException
241202 */
242- public function withUserInfo (#[SensitiveParameter] ?string $ encodedUserInfo ): self
203+ public function withUserInfo (#[SensitiveParameter] ?string $ userInfo ): self
243204 {
244- if ($ encodedUserInfo === $ this ->getRawUserInfo ()) {
205+ if ($ userInfo === $ this ->getRawUserInfo ()) {
245206 return $ this ;
246207 }
247208
248- if (null === $ encodedUserInfo ) {
209+ if (null === $ userInfo ) {
249210 return $ this ->withComponent (['user ' => null , 'pass ' => null ]);
250211 }
251212
252- [$ user , $ password ] = explode (': ' , $ encodedUserInfo , 2 ) + [1 => null ];
213+ [$ user , $ password ] = explode (': ' , $ userInfo , 2 ) + [1 => null ];
253214 if (!Encoder::isUserEncoded ($ user ) || !Encoder::isPasswordEncoded ($ password )) {
254215 throw new InvalidUriException ('The encoded userInfo string component contains invalid characters. ' );
255216 }
@@ -290,12 +251,12 @@ public function getHost(): ?string
290251 /**
291252 * @throws InvalidUriException
292253 */
293- public function withHost (?string $ encodedHost ): self
254+ public function withHost (?string $ host ): self
294255 {
295256 return match (true ) {
296- $ encodedHost === $ this ->getRawHost () => $ this ,
297- UriString::isHost ($ encodedHost ) => $ this ->withComponent (['host ' => $ encodedHost ]),
298- default => throw new InvalidUriException ('The host component value ` ' .$ encodedHost .'` is not a valid host. ' ),
257+ $ host === $ this ->getRawHost () => $ this ,
258+ UriString::isHost ($ host ) => $ this ->withComponent (['host ' => $ host ]),
259+ default => throw new InvalidUriException ('The host component value ` ' .$ host .'` is not a valid host. ' ),
299260 };
300261 }
301262
@@ -329,12 +290,12 @@ public function getPath(): ?string
329290 /**
330291 * @throws InvalidUriException
331292 */
332- public function withPath (?string $ encodedPath ): self
293+ public function withPath (?string $ path ): self
333294 {
334295 return match (true ) {
335- $ encodedPath === $ this ->getRawPath () => $ this ,
336- Encoder::isPathEncoded ($ encodedPath ) => $ this ->withComponent (['path ' => $ encodedPath ]),
337- default => throw new InvalidUriException ('The encoded path component ` ' .$ encodedPath .'` contains invalid characters. ' ),
296+ $ path === $ this ->getRawPath () => $ this ,
297+ Encoder::isPathEncoded ($ path ) => $ this ->withComponent (['path ' => $ path ]),
298+ default => throw new InvalidUriException ('The encoded path component ` ' .$ path .'` contains invalid characters. ' ),
338299 };
339300 }
340301
@@ -351,12 +312,12 @@ public function getQuery(): ?string
351312 /**
352313 * @throws InvalidUriException
353314 */
354- public function withQuery (?string $ encodedQuery ): self
315+ public function withQuery (?string $ query ): self
355316 {
356317 return match (true ) {
357- $ encodedQuery === $ this ->getRawQuery () => $ this ,
358- Encoder::isQueryEncoded ($ encodedQuery ) => $ this ->withComponent (['query ' => $ encodedQuery ]),
359- default => throw new InvalidUriException ('The encoded query string component ` ' .$ encodedQuery .'` contains invalid characters. ' ),
318+ $ query === $ this ->getRawQuery () => $ this ,
319+ Encoder::isQueryEncoded ($ query ) => $ this ->withComponent (['query ' => $ query ]),
320+ default => throw new InvalidUriException ('The encoded query string component ` ' .$ query .'` contains invalid characters. ' ),
360321 };
361322 }
362323
@@ -373,12 +334,12 @@ public function getFragment(): ?string
373334 /**
374335 * @throws InvalidUriException
375336 */
376- public function withFragment (?string $ encodedFragment ): self
337+ public function withFragment (?string $ fragment ): self
377338 {
378339 return match (true ) {
379- $ encodedFragment === $ this ->getRawFragment () => $ this ,
380- Encoder::isFragmentEncoded ($ encodedFragment ) => $ this ->withComponent (['fragment ' => $ encodedFragment ]),
381- default => throw new InvalidUriException ('The encoded fragment string component ` ' .$ encodedFragment .'` contains invalid characters. ' ),
340+ $ fragment === $ this ->getRawFragment () => $ this ,
341+ Encoder::isFragmentEncoded ($ fragment ) => $ this ->withComponent (['fragment ' => $ fragment ]),
342+ default => throw new InvalidUriException ('The encoded fragment string component ` ' .$ fragment .'` contains invalid characters. ' ),
382343 };
383344 }
384345
@@ -414,5 +375,44 @@ public function resolve(string $uri): self
414375 {
415376 return new self ($ uri , $ this ->toRawString ());
416377 }
378+
379+ /**
380+ * @return array{__uri: string}
381+ */
382+ public function __serialize (): array
383+ {
384+ return ['__uri ' => $ this ->toRawString ()];
385+ }
386+
387+ /**
388+ * @param array{__uri: string} $data
389+ *
390+ * @throws Exception|InvalidUriException
391+ */
392+ public function __unserialize (array $ data ): void
393+ {
394+ $ uri = new self ($ data ['__uri ' ] ?? throw new Exception ('The `__uri` property is missing from the serialized object. ' ));
395+
396+ $ this ->rawComponents = $ uri ->rawComponents ;
397+ $ this ->rawUri = $ uri ->rawUri ;
398+ $ this ->isNormalized = false ;
399+ }
400+
401+ /**
402+ * @return array{scheme: ?string, user: ?string, password: ?string, host: ?string, port: ?int, path: ?string, query: ?string, fragment: ?string}
403+ */
404+ public function __debugInfo (): array
405+ {
406+ return [
407+ 'scheme ' => $ this ->rawComponents ['scheme ' ],
408+ 'user ' => $ this ->rawComponents ['user ' ],
409+ 'password ' => $ this ->rawComponents ['pass ' ],
410+ 'host ' => $ this ->rawComponents ['host ' ],
411+ 'port ' => $ this ->rawComponents ['port ' ],
412+ 'path ' => $ this ->rawComponents ['path ' ],
413+ 'query ' => $ this ->rawComponents ['query ' ],
414+ 'fragment ' => $ this ->rawComponents ['fragment ' ],
415+ ];
416+ }
417417 }
418418}
0 commit comments