From 86df2d35795005bd3bcbdbfad7eb96871f0f6adf Mon Sep 17 00:00:00 2001 From: Thomas Bisignani Date: Tue, 24 Apr 2018 22:42:42 +0200 Subject: [PATCH 1/2] Added UUID pattern support for the TextMatcher --- src/Matcher/Pattern/RegexConverter.php | 2 ++ tests/Matcher/TextMatcherTest.php | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/src/Matcher/Pattern/RegexConverter.php b/src/Matcher/Pattern/RegexConverter.php index d405750e..8b1f4aa2 100644 --- a/src/Matcher/Pattern/RegexConverter.php +++ b/src/Matcher/Pattern/RegexConverter.php @@ -21,6 +21,8 @@ public function toRegex(TypePattern $typePattern) : string return '(\\-?[0-9]*)'; case 'double': return '(\\-?[0-9]*[\\.|\\,][0-9]*)'; + case 'uuid': + return "([\da-f]{8}-[\da-f]{4}-[1-5][\da-f]{3}-[89ab][\da-f]{3}-[\da-f]{12})"; default: throw new UnknownTypeException($typePattern->getType()); } diff --git a/tests/Matcher/TextMatcherTest.php b/tests/Matcher/TextMatcherTest.php index ff2c62ba..3e2458ab 100644 --- a/tests/Matcher/TextMatcherTest.php +++ b/tests/Matcher/TextMatcherTest.php @@ -107,6 +107,16 @@ public function matchingData() '/users/12345/active', '/users/@integer@.greaterThan(0)/active', true + ], + [ + '/user/ebd1fb0e-45ae-11e8-842f-0ed5f89f718b/profile', + '/user/@uuid@/@string@', + true + ], + [ + '/user/12345/profile', + '/user/@uuid@/@string@', + false ] ]; } From ead754257a12d2bc216c757ed66b8b46f1b0768e Mon Sep 17 00:00:00 2001 From: Thomas Bisignani Date: Wed, 25 Apr 2018 16:34:42 +0200 Subject: [PATCH 2/2] Updated UuidMatcher constants --- src/Matcher/Pattern/RegexConverter.php | 3 ++- src/Matcher/UuidMatcher.php | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Matcher/Pattern/RegexConverter.php b/src/Matcher/Pattern/RegexConverter.php index 8b1f4aa2..a338287f 100644 --- a/src/Matcher/Pattern/RegexConverter.php +++ b/src/Matcher/Pattern/RegexConverter.php @@ -5,6 +5,7 @@ namespace Coduo\PHPMatcher\Matcher\Pattern; use Coduo\PHPMatcher\Exception\UnknownTypeException; +use Coduo\PHPMatcher\Matcher\UuidMatcher; final class RegexConverter { @@ -22,7 +23,7 @@ public function toRegex(TypePattern $typePattern) : string case 'double': return '(\\-?[0-9]*[\\.|\\,][0-9]*)'; case 'uuid': - return "([\da-f]{8}-[\da-f]{4}-[1-5][\da-f]{3}-[89ab][\da-f]{3}-[\da-f]{12})"; + return '('.UuidMatcher::UUID_PATTERN.')'; default: throw new UnknownTypeException($typePattern->getType()); } diff --git a/src/Matcher/UuidMatcher.php b/src/Matcher/UuidMatcher.php index fdbf082f..fd8a3829 100644 --- a/src/Matcher/UuidMatcher.php +++ b/src/Matcher/UuidMatcher.php @@ -10,7 +10,8 @@ final class UuidMatcher extends Matcher { const PATTERN = 'uuid'; - const UUID_FORMAT_PATTERN = '|^[\da-f]{8}-[\da-f]{4}-[1-5][\da-f]{3}-[89ab][\da-f]{3}-[\da-f]{12}$|'; + const UUID_PATTERN = '[\da-f]{8}-[\da-f]{4}-[1-5][\da-f]{3}-[89ab][\da-f]{3}-[\da-f]{12}'; + const UUID_FORMAT_PATTERN = '|^'.self::UUID_PATTERN.'$|'; private $parser;