From 2d3f62c85d936b431bef2f442ef3203593eba08a Mon Sep 17 00:00:00 2001 From: Brooke Bryan Date: Tue, 6 Oct 2015 13:11:28 +0100 Subject: [PATCH] Pattern verify --- src/Strings.php | 19 +++++++++++++++++++ tests/StringsTest.php | 19 +++---------------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/Strings.php b/src/Strings.php index 1a0d2bf..02ac2e0 100644 --- a/src/Strings.php +++ b/src/Strings.php @@ -314,6 +314,25 @@ public static function pattern($pattern = 'XX00-XX00-00XX-00XX-XXXX') return $return; } + public static function verifyPattern($template, $pattern) + { + $regexPattern = str_replace( + ['X', 'x', '0', '5', '?', '!', '*'], + [ + '[A-Z]', + '[a-z]', + '[0-9]', + '[0-5]', + '[a-z0-9]', + '[A-Z0-9]', + '[a-zA-Z0-9]' + ], + $template + ); + + return preg_match("/$regexPattern/", $pattern) === 1; + } + /** * Take a short extract from a string. * diff --git a/tests/StringsTest.php b/tests/StringsTest.php index 46ba58a..e45a175 100644 --- a/tests/StringsTest.php +++ b/tests/StringsTest.php @@ -606,24 +606,11 @@ public function testStripStart() */ public function testPattern($pattern) { - $regexPattern = str_replace( - ['X', 'x', '0', '5', '?', '!', '*'], - [ - '[A-Z]', - '[a-z]', - '[0-9]', - '[0-5]', - '[a-z0-9]', - '[A-Z0-9]', - '[a-zA-Z0-9]' - ], - $pattern - ); - for($i = 0; $i < 100; $i++) { - $generated = Strings::pattern($pattern); - $this->assertRegExp("/$regexPattern/", $generated); + $this->assertTrue( + Strings::verifyPattern($pattern, Strings::pattern($pattern)) + ); } }