Skip to content

Commit bef86d8

Browse files
committed
enhance: UrlHelper join method handle endpoint URL string
1 parent 161aadb commit bef86d8

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

src/Helpers/URLHelper.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ public static function matches(string $pattern, string $value): bool
2727
*/
2828
public static function join(string $baseUrl, string $endpoint): string
2929
{
30+
if (static::isValidUrl($endpoint)) {
31+
return $endpoint;
32+
}
33+
3034
if ($endpoint !== '/') {
3135
$endpoint = ltrim($endpoint, '/ ');
3236
}
@@ -35,11 +39,13 @@ public static function join(string $baseUrl, string $endpoint): string
3539

3640
$baseEndpoint = rtrim($baseUrl, '/ ');
3741

38-
$endpointIsNotUrl = empty(filter_var($endpoint, FILTER_VALIDATE_URL));
39-
$glue = $endpointIsNotUrl ? '/' : null;
40-
41-
$baseEndpoint = $requiresTrailingSlash ? $baseEndpoint . $glue : $baseEndpoint;
42+
$baseEndpoint = $requiresTrailingSlash ? $baseEndpoint . '/' : $baseEndpoint;
4243

4344
return $baseEndpoint . $endpoint;
4445
}
46+
47+
public static function isValidUrl(string $url): bool
48+
{
49+
return ! empty(filter_var($url, FILTER_VALIDATE_URL));
50+
}
4551
}

tests/Unit/URLHelperTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@
1212
['https://google.com//', '//search', 'https://google.com/search'],
1313
['', 'https://google.com/search', 'https://google.com/search'],
1414
['', 'google.com/search', '/google.com/search'],
15+
['https://google.com', 'https://api.google.com/search', 'https://api.google.com/search'],
1516
]);

0 commit comments

Comments
 (0)