mirror of
https://github.com/wiosna-dev/common-library.git
synced 2026-03-12 01:31:45 +01:00
Add Uri::buildUrl() method
Builds url with given root url and parts of url
This commit is contained in:
@@ -27,6 +27,7 @@ composer require meritoo/common-library
|
||||
4. [Static methods](docs/Static-methods.md)
|
||||
1. [Arrays](docs/Static-methods/Arrays.md)
|
||||
2. [Regex](docs/Static-methods/Regex.md)
|
||||
3. [Uri](docs/Static-methods/Uri.md)
|
||||
5. [Value Objects](docs/Value-Objects.md)
|
||||
|
||||
# Development
|
||||
|
||||
@@ -51,6 +51,7 @@ class MimeTypesTest extends BaseTestCase
|
||||
5. [Static methods](Static-methods.md)
|
||||
1. [Arrays](Static-methods/Arrays.md)
|
||||
2. [Regex](Static-methods/Regex.md)
|
||||
3. [Uri](Static-methods/Uri.md)
|
||||
6. [Value Objects](Value-Objects.md)
|
||||
|
||||
[‹ Back to `Readme`](../README.md)
|
||||
|
||||
@@ -60,6 +60,7 @@ class UnknownSimpleTypeException extends UnknownTypeException
|
||||
5. [Static methods](Static-methods.md)
|
||||
1. [Arrays](Static-methods/Arrays.md)
|
||||
2. [Regex](Static-methods/Regex.md)
|
||||
3. [Uri](Static-methods/Uri.md)
|
||||
6. [Value Objects](Value-Objects.md)
|
||||
|
||||
[‹ Back to `Readme`](../README.md)
|
||||
|
||||
@@ -22,6 +22,7 @@ var_dump($firstElement); // string(5) "lorem"
|
||||
5. [**Static methods**](Static-methods.md)
|
||||
1. [Arrays](Static-methods/Arrays.md)
|
||||
2. [Regex](Static-methods/Regex.md)
|
||||
3. [Uri](Static-methods/Uri.md)
|
||||
6. [Value Objects](Value-Objects.md)
|
||||
|
||||
[‹ Back to `Readme`](../README.md)
|
||||
|
||||
@@ -101,6 +101,7 @@ File: `src/Utilities/Arrays.php`
|
||||
5. [Static methods](../Static-methods.md)
|
||||
1. [**Arrays**](Arrays.md)
|
||||
2. [Regex](Regex.md)
|
||||
3. [Uri](Uri.md)
|
||||
6. [Value Objects](../Value-Objects.md)
|
||||
|
||||
[‹ Back to `Readme`](../../README.md)
|
||||
|
||||
@@ -93,6 +93,7 @@ File: `src/Utilities/Regex.php`
|
||||
5. [Static methods](../Static-methods.md)
|
||||
1. [Arrays](../Static-methods/Arrays.md)
|
||||
2. [**Regex**](Regex.md)
|
||||
3. [Uri](Uri.md)
|
||||
6. [Value Objects](../Value-Objects.md)
|
||||
|
||||
[‹ Back to `Readme`](../../README.md)
|
||||
|
||||
47
docs/Static-methods/Uri.md
Normal file
47
docs/Static-methods/Uri.md
Normal file
@@ -0,0 +1,47 @@
|
||||
# Meritoo Common Library
|
||||
|
||||
Common and useful classes, methods, exceptions etc.
|
||||
|
||||
# Uri
|
||||
|
||||
> Useful methods related to uri
|
||||
|
||||
Class: `Meritoo\Common\Utilities\Uri`
|
||||
File: `src/Utilities/Uri.php`
|
||||
|
||||
### buildUrl(string, string ...): string
|
||||
|
||||
> Builds url with given root url and parts of url (concatenates them using "/")
|
||||
|
||||
##### Arguments
|
||||
|
||||
- `string $rootUrl` - Protocol and domain (or domain only)
|
||||
- `string ...$urlParts` - Parts of url that will be concatenated with the rool url by "/"
|
||||
|
||||
##### Examples
|
||||
|
||||
1)
|
||||
|
||||
- rootUrl: `"http://my.example"`
|
||||
- urlParts: `""` (an empty string)
|
||||
- result: `"http://my.example"`
|
||||
|
||||
2)
|
||||
|
||||
- rootUrl: `"http://my.example"`
|
||||
- urlParts: `"/test", "/123"`
|
||||
- result: `"http://my.example/test/123"`
|
||||
|
||||
# More
|
||||
|
||||
1. [Base test case (with common methods and data providers)](../Base-test-case.md)
|
||||
2. [Collection of elements](../Collection/Collection.md)
|
||||
3. [Templates](../Collection/Templates.md)
|
||||
4. [Exceptions](../Exceptions.md)
|
||||
5. [Static methods](../Static-methods.md)
|
||||
1. [Arrays](Arrays.md)
|
||||
2. [Regex](Regex.md)
|
||||
3. [**Uri**](Uri.md)
|
||||
6. [Value Objects](../Value-Objects.md)
|
||||
|
||||
[‹ Back to `Readme`](../../README.md)
|
||||
@@ -344,6 +344,7 @@ $asString = (string)$version; // "1.0.2"
|
||||
5. [Static methods](Static-methods.md)
|
||||
1. [Arrays](Static-methods/Arrays.md)
|
||||
2. [Regex](Static-methods/Regex.md)
|
||||
3. [Uri](Static-methods/Uri.md)
|
||||
6. [**Value Objects**](Value-Objects.md)
|
||||
|
||||
[‹ Back to `Readme`](../README.md)
|
||||
|
||||
@@ -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)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ use Meritoo\Common\Utilities\Uri;
|
||||
* @copyright Meritoo <http://www.meritoo.pl>
|
||||
*
|
||||
* @internal
|
||||
* @covers \Meritoo\Common\Utilities\Uri
|
||||
* @covers \Meritoo\Common\Utilities\Uri
|
||||
*/
|
||||
class UriTest extends BaseTestCase
|
||||
{
|
||||
@@ -255,6 +255,18 @@ class UriTest extends BaseTestCase
|
||||
self::assertEquals($expectedUrl, Uri::getSecuredUrl($url, $user, $password));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $expected
|
||||
* @param string $rootUrl
|
||||
* @param string ...$urlParts
|
||||
*
|
||||
* @dataProvider provideRootUrlAndUrlParts
|
||||
*/
|
||||
public function testBuildUrl(string $expected, string $rootUrl, string ...$urlParts): void
|
||||
{
|
||||
static::assertSame($expected, Uri::buildUrl($rootUrl, ...$urlParts));
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides url to replenish protocol
|
||||
*
|
||||
@@ -372,4 +384,56 @@ class UriTest extends BaseTestCase
|
||||
'http://john:pass123@lorem.com/contact',
|
||||
];
|
||||
}
|
||||
|
||||
public function provideRootUrlAndUrlParts(): ?Generator
|
||||
{
|
||||
yield[
|
||||
'',
|
||||
'',
|
||||
];
|
||||
|
||||
yield[
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
];
|
||||
|
||||
yield[
|
||||
'http://my.example',
|
||||
'http://my.example',
|
||||
'',
|
||||
];
|
||||
|
||||
yield[
|
||||
'http://my.example',
|
||||
'http://my.example',
|
||||
'',
|
||||
'',
|
||||
];
|
||||
|
||||
yield[
|
||||
'http://my.example//test/12/test/',
|
||||
'http://my.example',
|
||||
'',
|
||||
'test',
|
||||
'12/test',
|
||||
'',
|
||||
];
|
||||
|
||||
yield[
|
||||
'http://my.example//test/12/test',
|
||||
'http://my.example',
|
||||
'',
|
||||
'test/',
|
||||
'12/test/',
|
||||
];
|
||||
|
||||
yield[
|
||||
'http://my.example/test/12/test/',
|
||||
'http://my.example',
|
||||
'/test/',
|
||||
'/12/test',
|
||||
'',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user