Add Uri::buildUrl() method

Builds url with given root url and parts of url
This commit is contained in:
Meritoo
2019-08-28 16:01:27 +02:00
parent 891411e231
commit f7a8da0550
10 changed files with 140 additions and 2 deletions

View File

@@ -9,7 +9,7 @@
namespace Meritoo\Common\Utilities;
/**
* Useful uri methods (only static functions)
* Useful methods related to uri
*
* @author Meritoo <github@meritoo.pl>
* @copyright Meritoo <http://www.meritoo.pl>
@@ -351,4 +351,24 @@ class Uri
return sprintf('%s://%s', $protocol, $url);
}
public static function buildUrl(string $rootUrl, string ...$urlParts): string
{
$rootUrl = Regex::clearEndingSlash($rootUrl);
if (empty($urlParts) || Arrays::containsEmptyStringsOnly($urlParts)) {
return $rootUrl;
}
array_walk($urlParts, static function (&$part) {
$part = Regex::clearBeginningSlash($part);
$part = Regex::clearEndingSlash($part);
});
return sprintf(
'%s/%s',
$rootUrl,
implode('/', $urlParts)
);
}
}