Skip to content

Commit 3c4ae0a

Browse files
committed
Improve UriString implementation
1 parent 748a171 commit 3c4ae0a

File tree

1 file changed

+14
-26
lines changed

1 file changed

+14
-26
lines changed

interfaces/UriString.php

+14-26
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,6 @@ final class UriString
203203
*/
204204
public static function build(array $components): string
205205
{
206-
$components = self::validateComponents($components);
207-
208206
return self::buildUri(
209207
$components['scheme'] ?? null,
210208
self::buildAuthority($components),
@@ -221,7 +219,7 @@ public static function build(array $components): string
221219
* but properly encoded.
222220
*
223221
* @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§
225223
*/
226224
public static function buildUri(
227225
?string $scheme,
@@ -230,6 +228,7 @@ public static function buildUri(
230228
?string $query,
231229
?string $fragment,
232230
): string {
231+
self::validateComponents($scheme, $authority, $path);
233232
$uri = '';
234233
if (null !== $scheme) {
235234
$uri .= $scheme.':';
@@ -342,8 +341,12 @@ public static function normalize(Stringable|string $uri): string
342341
*
343342
* @throws SyntaxError if the URI is not parsable
344343
*/
345-
public static function normalizeAuthority(Stringable|string $authority): string
344+
public static function normalizeAuthority(Stringable|string|null $authority): ?string
346345
{
346+
if (null === $authority) {
347+
return null;
348+
}
349+
347350
$components = UriString::parseAuthority($authority);
348351
if (null !== $components['host'] &&
349352
false === filter_var($components['host'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) &&
@@ -595,44 +598,33 @@ public static function parse(Stringable|string|int $uri): array
595598
* @link https://tools.ietf.org/html/rfc3986#section-3
596599
* @link https://tools.ietf.org/html/rfc3986#section-3.3
597600
*
598-
* @param ComponentMap|InputComponentMap $components
599-
*
600601
* @throws SyntaxError
601-
*
602-
* @return ComponentMap
603602
*/
604-
private static function validateComponents(array $components): array
603+
private static function validateComponents(?string $scheme, ?string $authority, ?string $path): void
605604
{
606-
/** @var ComponentMap $components */
607-
$components = [...self::URI_COMPONENTS, ...$components];
608-
$authority = UriString::buildAuthority($components);
609-
$path = $components['path'];
610-
611605
if (null !== $authority) {
612606
if (null !== $path && '' !== $path && '/' !== $path[0]) {
613607
throw new SyntaxError('If an authority is present the path must be empty or start with a `/`.');
614608
}
615609

616-
return $components;
610+
return;
617611
}
618612

619613
if (null === $path || '' === $path) {
620-
return $components;
614+
return;
621615
}
622616

623617
if (str_starts_with($path, '//')) {
624618
throw new SyntaxError('If there is no authority the path `'.$path.'` cannot start with a `//`.');
625619
}
626620

627-
if (null !== $components['scheme'] || false === ($pos = strpos($path, ':'))) {
628-
return $components;
621+
if (null !== $scheme || false === ($pos = strpos($path, ':'))) {
622+
return;
629623
}
630624

631625
if (!str_contains(substr($path, 0, $pos), '/')) {
632626
throw new SyntaxError('In absence of a scheme and an authority the first path segment cannot contain a colon (":") character.');
633627
}
634-
635-
return $components;
636628
}
637629

638630
/**
@@ -731,23 +723,19 @@ private static function filterHost(Stringable|string|null $host): ?string
731723
}
732724

733725
/**
734-
* Tells whether the scheme component is valid
726+
* Tells whether the scheme component is valid.
735727
*
736-
* @param Stringable|string|null $scheme
737728
*
738-
* @return bool
739729
*/
740730
public static function isScheme(Stringable|string|null $scheme): bool
741731
{
742732
return null === $scheme || 1 === preg_match('/^[A-Za-z]([-A-Za-z\d+.]+)?$/', (string) $scheme);
743733
}
744734

745735
/**
746-
* Tells whether the host component is valid
736+
* Tells whether the host component is valid.
747737
*
748-
* @param Stringable|string|null $host
749738
*
750-
* @return bool
751739
*/
752740
public static function isHost(Stringable|string|null $host): bool
753741
{

0 commit comments

Comments
 (0)