Skip to content

Commit

Permalink
Pattern verify
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed Oct 6, 2015
1 parent bfc468a commit 2d3f62c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
19 changes: 19 additions & 0 deletions src/Strings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
19 changes: 3 additions & 16 deletions tests/StringsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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))
);
}
}

Expand Down

0 comments on commit 2d3f62c

Please sign in to comment.