@@ -203,8 +203,6 @@ final class UriString
203
203
*/
204
204
public static function build (array $ components ): string
205
205
{
206
- $ components = self ::validateComponents ($ components );
207
-
208
206
return self ::buildUri (
209
207
$ components ['scheme ' ] ?? null ,
210
208
self ::buildAuthority ($ components ),
@@ -221,7 +219,7 @@ public static function build(array $components): string
221
219
* but properly encoded.
222
220
*
223
221
* @link https://tools.ietf.org/html/rfc3986#section-5.3
224
- * @link https://tools.ietf.org/html/rfc3986#section-7.5
222
+ * @link https://tools.ietf.org/html/rfc3986#section-7.5§
225
223
*/
226
224
public static function buildUri (
227
225
?string $ scheme ,
@@ -230,6 +228,7 @@ public static function buildUri(
230
228
?string $ query ,
231
229
?string $ fragment ,
232
230
): string {
231
+ self ::validateComponents ($ scheme , $ authority , $ path );
233
232
$ uri = '' ;
234
233
if (null !== $ scheme ) {
235
234
$ uri .= $ scheme .': ' ;
@@ -342,8 +341,12 @@ public static function normalize(Stringable|string $uri): string
342
341
*
343
342
* @throws SyntaxError if the URI is not parsable
344
343
*/
345
- public static function normalizeAuthority (Stringable |string $ authority ): string
344
+ public static function normalizeAuthority (Stringable |string | null $ authority ): ? string
346
345
{
346
+ if (null === $ authority ) {
347
+ return null ;
348
+ }
349
+
347
350
$ components = UriString::parseAuthority ($ authority );
348
351
if (null !== $ components ['host ' ] &&
349
352
false === filter_var ($ components ['host ' ], FILTER_VALIDATE_IP , FILTER_FLAG_IPV4 ) &&
@@ -595,44 +598,33 @@ public static function parse(Stringable|string|int $uri): array
595
598
* @link https://tools.ietf.org/html/rfc3986#section-3
596
599
* @link https://tools.ietf.org/html/rfc3986#section-3.3
597
600
*
598
- * @param ComponentMap|InputComponentMap $components
599
- *
600
601
* @throws SyntaxError
601
- *
602
- * @return ComponentMap
603
602
*/
604
- private static function validateComponents (array $ components ): array
603
+ private static function validateComponents (? string $ scheme , ? string $ authority , ? string $ path ): void
605
604
{
606
- /** @var ComponentMap $components */
607
- $ components = [...self ::URI_COMPONENTS , ...$ components ];
608
- $ authority = UriString::buildAuthority ($ components );
609
- $ path = $ components ['path ' ];
610
-
611
605
if (null !== $ authority ) {
612
606
if (null !== $ path && '' !== $ path && '/ ' !== $ path [0 ]) {
613
607
throw new SyntaxError ('If an authority is present the path must be empty or start with a `/`. ' );
614
608
}
615
609
616
- return $ components ;
610
+ return ;
617
611
}
618
612
619
613
if (null === $ path || '' === $ path ) {
620
- return $ components ;
614
+ return ;
621
615
}
622
616
623
617
if (str_starts_with ($ path , '// ' )) {
624
618
throw new SyntaxError ('If there is no authority the path ` ' .$ path .'` cannot start with a `//`. ' );
625
619
}
626
620
627
- if (null !== $ components [ ' scheme ' ] || false === ($ pos = strpos ($ path , ': ' ))) {
628
- return $ components ;
621
+ if (null !== $ scheme || false === ($ pos = strpos ($ path , ': ' ))) {
622
+ return ;
629
623
}
630
624
631
625
if (!str_contains (substr ($ path , 0 , $ pos ), '/ ' )) {
632
626
throw new SyntaxError ('In absence of a scheme and an authority the first path segment cannot contain a colon (":") character. ' );
633
627
}
634
-
635
- return $ components ;
636
628
}
637
629
638
630
/**
@@ -731,23 +723,19 @@ private static function filterHost(Stringable|string|null $host): ?string
731
723
}
732
724
733
725
/**
734
- * Tells whether the scheme component is valid
726
+ * Tells whether the scheme component is valid.
735
727
*
736
- * @param Stringable|string|null $scheme
737
728
*
738
- * @return bool
739
729
*/
740
730
public static function isScheme (Stringable |string |null $ scheme ): bool
741
731
{
742
732
return null === $ scheme || 1 === preg_match ('/^[A-Za-z]([-A-Za-z\d+.]+)?$/ ' , (string ) $ scheme );
743
733
}
744
734
745
735
/**
746
- * Tells whether the host component is valid
736
+ * Tells whether the host component is valid.
747
737
*
748
- * @param Stringable|string|null $host
749
738
*
750
- * @return bool
751
739
*/
752
740
public static function isHost (Stringable |string |null $ host ): bool
753
741
{
0 commit comments