From f7a8da055062c990c15d398987854a434a341227 Mon Sep 17 00:00:00 2001 From: Meritoo Date: Wed, 28 Aug 2019 16:01:27 +0200 Subject: [PATCH] Add Uri::buildUrl() method Builds url with given root url and parts of url --- README.md | 1 + docs/Base-test-case.md | 1 + docs/Exceptions.md | 1 + docs/Static-methods.md | 1 + docs/Static-methods/Arrays.md | 1 + docs/Static-methods/Regex.md | 1 + docs/Static-methods/Uri.md | 47 +++++++++++++++++++++++++ docs/Value-Objects.md | 1 + src/Utilities/Uri.php | 22 +++++++++++- tests/Utilities/UriTest.php | 66 ++++++++++++++++++++++++++++++++++- 10 files changed, 140 insertions(+), 2 deletions(-) create mode 100644 docs/Static-methods/Uri.md diff --git a/README.md b/README.md index 6eddebb..b8fe66f 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/docs/Base-test-case.md b/docs/Base-test-case.md index bb8ac79..3452796 100644 --- a/docs/Base-test-case.md +++ b/docs/Base-test-case.md @@ -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) diff --git a/docs/Exceptions.md b/docs/Exceptions.md index de4a570..49c38f2 100644 --- a/docs/Exceptions.md +++ b/docs/Exceptions.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) diff --git a/docs/Static-methods.md b/docs/Static-methods.md index f1f0f7a..0b225e0 100644 --- a/docs/Static-methods.md +++ b/docs/Static-methods.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) diff --git a/docs/Static-methods/Arrays.md b/docs/Static-methods/Arrays.md index 35547c2..7169a55 100644 --- a/docs/Static-methods/Arrays.md +++ b/docs/Static-methods/Arrays.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) diff --git a/docs/Static-methods/Regex.md b/docs/Static-methods/Regex.md index f86e3fb..1a4a48b 100644 --- a/docs/Static-methods/Regex.md +++ b/docs/Static-methods/Regex.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) diff --git a/docs/Static-methods/Uri.md b/docs/Static-methods/Uri.md new file mode 100644 index 0000000..5808d31 --- /dev/null +++ b/docs/Static-methods/Uri.md @@ -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) diff --git a/docs/Value-Objects.md b/docs/Value-Objects.md index fa8d6ec..83184de 100644 --- a/docs/Value-Objects.md +++ b/docs/Value-Objects.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) diff --git a/src/Utilities/Uri.php b/src/Utilities/Uri.php index f1a47b8..602262a 100644 --- a/src/Utilities/Uri.php +++ b/src/Utilities/Uri.php @@ -9,7 +9,7 @@ namespace Meritoo\Common\Utilities; /** - * Useful uri methods (only static functions) + * Useful methods related to uri * * @author Meritoo * @copyright Meritoo @@ -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) + ); + } } diff --git a/tests/Utilities/UriTest.php b/tests/Utilities/UriTest.php index 6b3bf18..3739abb 100644 --- a/tests/Utilities/UriTest.php +++ b/tests/Utilities/UriTest.php @@ -19,7 +19,7 @@ use Meritoo\Common\Utilities\Uri; * @copyright Meritoo * * @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', + '', + ]; + } }