Regex > createSlug() method > returns slug for given value

This commit is contained in:
Meritoo
2019-02-21 23:10:53 +01:00
parent d46548d102
commit 79c09a26a6
9 changed files with 178 additions and 1 deletions

View File

@@ -20,6 +20,7 @@ composer require meritoo/common-library
2. [Collection of elements](docs/Collection-of-elements.md) 2. [Collection of elements](docs/Collection-of-elements.md)
3. [Exceptions](docs/Static-methods.md) 3. [Exceptions](docs/Static-methods.md)
4. [Static methods](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) 5. [Value Objects](docs/Value-Objects.md)
# Development # Development

View File

@@ -48,6 +48,7 @@ class MimeTypesTest extends BaseTestCase
2. [Collection of elements](Collection-of-elements.md) 2. [Collection of elements](Collection-of-elements.md)
3. [Exceptions](Exceptions.md) 3. [Exceptions](Exceptions.md)
4. [Static methods](Static-methods.md) 4. [Static methods](Static-methods.md)
1. [Regex](Static-methods/Regex.md)
5. [Value Objects](Value-Objects.md) 5. [Value Objects](Value-Objects.md)
[‹ Back to `Readme`](../README.md) [‹ Back to `Readme`](../README.md)

View File

@@ -46,6 +46,7 @@ var_dump($simpleCollection->has('dolor')); // bool(true)
2. [**Collection of elements**](Collection-of-elements.md) 2. [**Collection of elements**](Collection-of-elements.md)
3. [Exceptions](Exceptions.md) 3. [Exceptions](Exceptions.md)
4. [Static methods](Static-methods.md) 4. [Static methods](Static-methods.md)
1. [Regex](Static-methods/Regex.md)
5. [Value Objects](Value-Objects.md) 5. [Value Objects](Value-Objects.md)
[‹ Back to `Readme`](../README.md) [‹ Back to `Readme`](../README.md)

View File

@@ -57,6 +57,7 @@ class UnknownSimpleTypeException extends UnknownTypeException
2. [Collection of elements](Collection-of-elements.md) 2. [Collection of elements](Collection-of-elements.md)
3. [**Exceptions**](Exceptions.md) 3. [**Exceptions**](Exceptions.md)
4. [Static methods](Static-methods.md) 4. [Static methods](Static-methods.md)
1. [Regex](Static-methods/Regex.md)
5. [Value Objects](Value-Objects.md) 5. [Value Objects](Value-Objects.md)
[‹ Back to `Readme`](../README.md) [‹ Back to `Readme`](../README.md)

View File

@@ -19,6 +19,7 @@ var_dump($firstElement); // string(5) "lorem"
2. [Collection of elements](Collection-of-elements.md) 2. [Collection of elements](Collection-of-elements.md)
3. [Exceptions](Exceptions.md) 3. [Exceptions](Exceptions.md)
4. [**Static methods**](Static-methods.md) 4. [**Static methods**](Static-methods.md)
1. [Regex](Static-methods/Regex.md)
5. [Value Objects](Value-Objects.md) 5. [Value Objects](Value-Objects.md)
[‹ Back to `Readme`](../README.md) [‹ Back to `Readme`](../README.md)

View File

@@ -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)

View File

@@ -48,6 +48,7 @@ New instance can be created using:
2. [Collection of elements](Collection-of-elements.md) 2. [Collection of elements](Collection-of-elements.md)
3. [Exceptions](Exceptions.md) 3. [Exceptions](Exceptions.md)
4. [Static methods](Static-methods.md) 4. [Static methods](Static-methods.md)
1. [Regex](Static-methods/Regex.md)
5. [**Value Objects**](Value-Objects.md) 5. [**Value Objects**](Value-Objects.md)
[‹ Back to `Readme`](../README.md) [‹ Back to `Readme`](../README.md)

View File

@@ -12,7 +12,7 @@ use Meritoo\Common\Exception\Regex\IncorrectColorHexLengthException;
use Meritoo\Common\Exception\Regex\InvalidColorHexValueException; use Meritoo\Common\Exception\Regex\InvalidColorHexValueException;
/** /**
* Useful regular expressions methods * Useful methods related to regular expressions
* *
* @author Meritoo <github@meritoo.pl> * @author Meritoo <github@meritoo.pl>
* @copyright Meritoo <http://www.meritoo.pl> * @copyright Meritoo <http://www.meritoo.pl>
@@ -919,4 +919,37 @@ class Regex
return (bool)preg_match($pattern, $value); 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);
}
} }

View File

@@ -624,6 +624,17 @@ class RegexTest extends BaseTestCase
self::assertEquals($expected, Regex::getValidColorHexValue($color)); 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 * 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} * {@inheritdoc}
*/ */