Skip to content

Commit

Permalink
Add UUID support
Browse files Browse the repository at this point in the history
  • Loading branch information
hotmeteor committed Aug 18, 2021
1 parent b983ecc commit 9e6511e
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 20 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Match methods return true or false depending on if the subject value contains an
You may optional allow whitespace by passing `true` as a second parameter.

#### Methods

```php
Regex::isAlpha($subject, $allowWhitespace = false)
```
Expand Down Expand Up @@ -56,6 +57,12 @@ Regex::isNumeric($subject)
```
Checks if the value contains anything but numeric values, including decimals and negative numbers. Does not allow for whitespace.

***

```php
Regex::isUuid($subject)
```
Checks if the value is a UUID. Does not allow for whitespace.

### Replace

Expand Down Expand Up @@ -97,3 +104,10 @@ Regex::numeric($subject, $replace = '')
```
Replaces all characters in the subject except numeric values, including decimals and negative numbers.

***

```php
Regex::uuid($subject)
```
Replaces all characters in the subject to form it into a UUID. Does not accept a replacement value.

64 changes: 44 additions & 20 deletions src/Regex.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Regex
const PATTERN_ALPHADASH = '\pL\pM\pN._-';
const PATTERN_DIGITS = '0-9';
const PATTERN_NUMERIC = '-?\d*(\.\d+)?';
const PATTERN_UUID = '\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}';

/**
* Filter to only alpha.
Expand All @@ -21,7 +22,7 @@ class Regex
* @param string $replace
* @return array|string|string[]|null
*/
public static function alpha($subject, $replace = '')
public static function alpha($subject, string $replace = '')
{
return static::replace($subject, self::PATTERN_ALPHA, $replace);
}
Expand All @@ -33,7 +34,7 @@ public static function alpha($subject, $replace = '')
* @param string $replace
* @return array|string|string[]|null
*/
public static function alphadash($subject, $replace = '')
public static function alphadash($subject, string $replace = '')
{
return static::replace($subject, self::PATTERN_ALPHADASH, $replace);
}
Expand All @@ -45,7 +46,7 @@ public static function alphadash($subject, $replace = '')
* @param string $replace
* @return array|string|string[]|null
*/
public static function alphanumeric($subject, $replace = '')
public static function alphanumeric($subject, string $replace = '')
{
return static::replace($subject, self::PATTERN_ALPHANUMERIC, $replace);
}
Expand All @@ -57,7 +58,7 @@ public static function alphanumeric($subject, $replace = '')
* @param string $replace
* @return array|string|string[]|null
*/
public static function digits($subject, $replace = '')
public static function digits($subject, string $replace = '')
{
return static::replace($subject, self::PATTERN_DIGITS, $replace);
}
Expand All @@ -69,20 +70,33 @@ public static function digits($subject, $replace = '')
* @param string $replace
* @return array|string|string[]|null
*/
public static function numeric($subject, $replace = '')
public static function numeric($subject, string $replace = '')
{
return static::replace($subject, self::PATTERN_NUMERIC, $replace);
}

/**
* Filter to only UUID format.
*
* @param $subject
* @return array|string|string[]|null
*/
public static function uuid($subject)
{
$subject = static::alphanumeric($subject);

return preg_replace("/(\w{8})(\w{4})(\w{4})(\w{4})(\w{12})/i", '$1-$2-$3-$4-$5', $subject);
}

/**
* @param $subject
* @param $pattern
* @param string $replacement
* @param string $replace
* @return array|string|string[]|null
*/
public static function replace($subject, $pattern, $replacement = '')
public static function replace($subject, $pattern, string $replace = '', $prefix = '/[^', $suffix = ']+/u')
{
return preg_replace(self::wrapReplacePattern($pattern), $replacement, $subject);
return preg_replace(self::wrapReplacePattern($pattern, $prefix, $suffix), $replace, $subject);
}

/**
Expand All @@ -92,7 +106,7 @@ public static function replace($subject, $pattern, $replacement = '')
* @param bool $allowWhitespace
* @return bool
*/
public static function isAlpha($subject, bool $allowWhitespace = false)
public static function isAlpha($subject, bool $allowWhitespace = false): bool
{
return static::match($subject, self::PATTERN_ALPHA, $allowWhitespace);
}
Expand All @@ -104,7 +118,7 @@ public static function isAlpha($subject, bool $allowWhitespace = false)
* @param bool $allowWhitespace
* @return bool
*/
public static function isAlphadash($subject, bool $allowWhitespace = false)
public static function isAlphadash($subject, bool $allowWhitespace = false): bool
{
return static::match($subject, self::PATTERN_ALPHADASH, $allowWhitespace);
}
Expand All @@ -116,7 +130,7 @@ public static function isAlphadash($subject, bool $allowWhitespace = false)
* @param bool $allowWhitespace
* @return bool
*/
public static function isAlphanumeric($subject, bool $allowWhitespace = false)
public static function isAlphanumeric($subject, bool $allowWhitespace = false): bool
{
return static::match($subject, self::PATTERN_ALPHANUMERIC, $allowWhitespace);
}
Expand All @@ -128,7 +142,7 @@ public static function isAlphanumeric($subject, bool $allowWhitespace = false)
* @param bool $allowWhitespace
* @return bool
*/
public static function isDigits($subject, bool $allowWhitespace = false)
public static function isDigits($subject, bool $allowWhitespace = false): bool
{
return static::match($subject, self::PATTERN_DIGITS, $allowWhitespace);
}
Expand All @@ -137,23 +151,33 @@ public static function isDigits($subject, bool $allowWhitespace = false)
* Check if it's only numeric.
*
* @param $subject
* @param bool $allowWhitespace
* @return bool
*/
public static function isNumeric($subject, bool $allowWhitespace = false)
public static function isNumeric($subject): bool
{
return static::match($subject, self::PATTERN_NUMERIC);
}

/**
* Check if it's only numeric.
*
* @param $subject
* @return bool
*/
public static function isUuid($subject): bool
{
return static::match($subject, '/^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/iD', false, '', '');
}

/**
* @param $subject
* @param $pattern
* @param bool $allowWhitespace
* @return bool
*/
public static function match($subject, $pattern, bool $allowWhitespace = false): bool
public static function match($subject, $pattern, bool $allowWhitespace = false, $prefix = '/^[', $suffix = ']+$/u'): bool
{
$result = preg_match(self::wrapMatchPattern($pattern, $allowWhitespace), $subject);
$result = preg_match(self::wrapMatchPattern($pattern, $allowWhitespace, $prefix, $suffix), $subject);

return is_numeric($result) ? $result === 1 : $result;
}
Expand All @@ -164,9 +188,9 @@ public static function match($subject, $pattern, bool $allowWhitespace = false):
* @param $pattern
* @return string
*/
protected static function wrapReplacePattern($pattern): string
protected static function wrapReplacePattern($pattern, $prefix, $suffix): string
{
return implode('', ['/[^', $pattern, ']+/u']);
return implode('', [$prefix, $pattern, $suffix]);
}

/**
Expand All @@ -176,8 +200,8 @@ protected static function wrapReplacePattern($pattern): string
* @param bool $allowWhitespace
* @return string
*/
protected static function wrapMatchPattern($pattern, bool $allowWhitespace = false): string
protected static function wrapMatchPattern($pattern, bool $allowWhitespace, $prefix, $suffix): string
{
return implode('', ['/^[', $allowWhitespace ? '\s' : '', $pattern, ']+$/u']);
return implode('', [$prefix, $allowWhitespace ? '\s' : '', $pattern, $suffix]);
}
}
26 changes: 26 additions & 0 deletions tests/RegexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ public function providesReplacementSubjects()
];
}

public function test_uuid_replace()
{
$expected = '440526d4-04bd-43c4-9ac5-55e15c835d0d';

$result = Regex::uuid('440526d4-04bd-43c4-9ac5-55e15c835d0d');

$this->assertSame($expected, $result);

$result = Regex::uuid('4405 26d4-04bd43c4-9ac5 - 55e15c 835d0d ');

$this->assertSame($expected, $result);
}

/**
* @dataProvider providesMatchSubjects
*/
Expand Down Expand Up @@ -202,4 +215,17 @@ public function providesMatchSubjects()

];
}

public function test_uuid_match()
{
$expected = '440526d4-04bd-43c4-9ac5-55e15c835d0d';

$result = Regex::isUuid('440526d4-04bd-43c4-9ac5-55e15c835d0d');

$this->assertTrue($result);

$result = Regex::isUuid('4405 26d4-04bd43c4-9ac5 - 55e15c 835d0d ');

$this->assertFalse($result);
}
}

0 comments on commit 9e6511e

Please sign in to comment.