From 79c09a26a6c4d20538aff648a2f3a31540e543ec Mon Sep 17 00:00:00 2001 From: Meritoo Date: Thu, 21 Feb 2019 23:10:53 +0100 Subject: [PATCH] Regex > createSlug() method > returns slug for given value --- README.md | 1 + docs/Base-test-case.md | 1 + docs/Collection-of-elements.md | 1 + docs/Exceptions.md | 1 + docs/Static-methods.md | 1 + docs/Static-methods/Regex.md | 44 ++++++++++++++++ docs/Value-Objects.md | 1 + src/Utilities/Regex.php | 35 ++++++++++++- tests/Utilities/RegexTest.php | 94 ++++++++++++++++++++++++++++++++++ 9 files changed, 178 insertions(+), 1 deletion(-) create mode 100644 docs/Static-methods/Regex.md diff --git a/README.md b/README.md index bcc9fc0..ed6c735 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ composer require meritoo/common-library 2. [Collection of elements](docs/Collection-of-elements.md) 3. [Exceptions](docs/Static-methods.md) 4. [Static methods](docs/Static-methods.md) + 1. [Regex](docs/Static-methods/Regex.md) 5. [Value Objects](docs/Value-Objects.md) # Development diff --git a/docs/Base-test-case.md b/docs/Base-test-case.md index b0a038b..aad3b0d 100644 --- a/docs/Base-test-case.md +++ b/docs/Base-test-case.md @@ -48,6 +48,7 @@ class MimeTypesTest extends BaseTestCase 2. [Collection of elements](Collection-of-elements.md) 3. [Exceptions](Exceptions.md) 4. [Static methods](Static-methods.md) + 1. [Regex](Static-methods/Regex.md) 5. [Value Objects](Value-Objects.md) [‹ Back to `Readme`](../README.md) diff --git a/docs/Collection-of-elements.md b/docs/Collection-of-elements.md index 67fde9f..40e366f 100644 --- a/docs/Collection-of-elements.md +++ b/docs/Collection-of-elements.md @@ -46,6 +46,7 @@ var_dump($simpleCollection->has('dolor')); // bool(true) 2. [**Collection of elements**](Collection-of-elements.md) 3. [Exceptions](Exceptions.md) 4. [Static methods](Static-methods.md) + 1. [Regex](Static-methods/Regex.md) 5. [Value Objects](Value-Objects.md) [‹ Back to `Readme`](../README.md) diff --git a/docs/Exceptions.md b/docs/Exceptions.md index 87b12c1..29709c3 100644 --- a/docs/Exceptions.md +++ b/docs/Exceptions.md @@ -57,6 +57,7 @@ class UnknownSimpleTypeException extends UnknownTypeException 2. [Collection of elements](Collection-of-elements.md) 3. [**Exceptions**](Exceptions.md) 4. [Static methods](Static-methods.md) + 1. [Regex](Static-methods/Regex.md) 5. [Value Objects](Value-Objects.md) [‹ Back to `Readme`](../README.md) diff --git a/docs/Static-methods.md b/docs/Static-methods.md index e503fe9..29fb42c 100644 --- a/docs/Static-methods.md +++ b/docs/Static-methods.md @@ -19,6 +19,7 @@ var_dump($firstElement); // string(5) "lorem" 2. [Collection of elements](Collection-of-elements.md) 3. [Exceptions](Exceptions.md) 4. [**Static methods**](Static-methods.md) + 1. [Regex](Static-methods/Regex.md) 5. [Value Objects](Value-Objects.md) [‹ Back to `Readme`](../README.md) diff --git a/docs/Static-methods/Regex.md b/docs/Static-methods/Regex.md new file mode 100644 index 0000000..8358534 --- /dev/null +++ b/docs/Static-methods/Regex.md @@ -0,0 +1,44 @@ +# Meritoo Common Library + +Common and useful classes, methods, exceptions etc. + +# Regex + +*Useful methods related to regular expressions* + +Class: `Meritoo\Common\Utilities\Regex` +File: `src/Utilities/Regex.php` + +### createSlug($value) + +*Returns slug for given value* + +##### Arguments + +- `string $value` - Value that should be transformed to slug + +##### Example 1 + +- value: non-scalar or `null` +- result: `false` + +##### Example 2 + +- value: `""` (an empty string) +- result: `""` (an empty string) + +##### Example 3 + +- value: `"Lorem ipsum. Dolor sit 12.34 amet."` +- result: `"lorem-ipsum-dolor-sit-1234-amet"` + +# More + +1. [Base test case (with common methods and data providers)](../Base-test-case.md) +2. [Collection of elements](../Collection-of-elements.md) +3. [Exceptions](../Exceptions.md) +4. [Static methods](../Static-methods.md) + 1. [**Regex**](../Static-methods/Regex.md) +5. [Value Objects](../Value-Objects.md) + +[‹ Back to `Readme`](../../README.md) diff --git a/docs/Value-Objects.md b/docs/Value-Objects.md index d26a0ae..9aa0325 100644 --- a/docs/Value-Objects.md +++ b/docs/Value-Objects.md @@ -48,6 +48,7 @@ New instance can be created using: 2. [Collection of elements](Collection-of-elements.md) 3. [Exceptions](Exceptions.md) 4. [Static methods](Static-methods.md) + 1. [Regex](Static-methods/Regex.md) 5. [**Value Objects**](Value-Objects.md) [‹ Back to `Readme`](../README.md) diff --git a/src/Utilities/Regex.php b/src/Utilities/Regex.php index f2eac7e..46d5b37 100644 --- a/src/Utilities/Regex.php +++ b/src/Utilities/Regex.php @@ -12,7 +12,7 @@ use Meritoo\Common\Exception\Regex\IncorrectColorHexLengthException; use Meritoo\Common\Exception\Regex\InvalidColorHexValueException; /** - * Useful regular expressions methods + * Useful methods related to regular expressions * * @author Meritoo * @copyright Meritoo @@ -919,4 +919,37 @@ class Regex return (bool)preg_match($pattern, $value); } + + /** + * Returns slug for given value + * + * @param string $value Value that should be transformed to slug + * @return string|bool + */ + public static function createSlug($value) + { + /* + * Not a scalar value? + * Nothing to do + */ + if (!is_scalar($value)) { + return false; + } + + /* + * It's an empty string? + * Nothing to do + */ + if ('' === $value) { + return ''; + } + + $id = 'Latin-ASCII; NFD; [:Nonspacing Mark:] Remove; NFC; [:Punctuation:] Remove; Lower();'; + $transliterator = \Transliterator::create($id); + + $cleanValue = trim($value); + $result = $transliterator->transliterate($cleanValue); + + return preg_replace('/[-\s]+/', '-', $result); + } } diff --git a/tests/Utilities/RegexTest.php b/tests/Utilities/RegexTest.php index a672d83..be0b29e 100644 --- a/tests/Utilities/RegexTest.php +++ b/tests/Utilities/RegexTest.php @@ -624,6 +624,17 @@ class RegexTest extends BaseTestCase self::assertEquals($expected, Regex::getValidColorHexValue($color)); } + /** + * @param string $value Value that should be transformed to slug + * @param string $expected Expected slug + * + * @dataProvider provideValueSlug + */ + public function testCreateSlug($value, $expected) + { + self::assertSame($expected, Regex::createSlug($value)); + } + /** * Provides name of bundle and information if it's valid name * @@ -1694,6 +1705,89 @@ class RegexTest extends BaseTestCase ]; } + /** + * Provide value to create slug + * + * @return Generator + */ + public function provideValueSlug() + { + yield[ + [], + false, + ]; + + yield[ + null, + false, + ]; + + yield[ + '', + '', + ]; + + yield[ + 1234, + '1234', + ]; + + yield[ + '1234', + '1234', + ]; + + yield[ + '1/2/3/4', + '1234', + ]; + + yield[ + '1 / 2 / 3 / 4', + '1-2-3-4', + ]; + + yield[ + 'test', + 'test', + ]; + + yield[ + 'test test', + 'test-test', + ]; + + yield[ + 'lorem ipsum dolor sit', + 'lorem-ipsum-dolor-sit', + ]; + + yield[ + 'Lorem ipsum. Dolor sit 12.34 amet.', + 'lorem-ipsum-dolor-sit-1234-amet', + ]; + + yield[ + 'Was sind Löwen, Bären, Vögel und Käfer (für die Prüfung)?', + 'was-sind-lowen-baren-vogel-und-kafer-fur-die-prufung', + ]; + + yield[ + 'äöü (ÄÖÜ)', + 'aou-aou', + ]; + + yield[ + 'Półka dębowa. Kolor: żółędziowy. Wymiary: 80 x 30 cm.', + 'polka-debowa-kolor-zoledziowy-wymiary-80-x-30-cm', + ]; + + yield[ + 'ąęółńśżźć (ĄĘÓŁŃŚŻŹĆ)', + 'aeolnszzc-aeolnszzc', + ]; + } + /** * {@inheritdoc} */