Skip to content

Commit 80ce761

Browse files
jmfeurpriernorberttech
authored andcommitted
Added UUID matcher. (#89)
1 parent 615247c commit 80ce761

File tree

5 files changed

+171
-3
lines changed

5 files changed

+171
-3
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ $matcher->getError(); // returns null or error message
6464
* ``@null@``
6565
* ``@*@`` || ``@wildcard@``
6666
* ``expr(expression)``
67+
* ``@uuid@``
6768

6869
### Available pattern expanders
6970

@@ -204,6 +205,19 @@ $matcher->match(new \DateTime('2014-04-01'), "expr(value.format('Y-m-d') == '201
204205
$matcher->match("Norbert", "expr(value === 'Norbert')");
205206
```
206207

208+
### UUID matching
209+
210+
```php
211+
<?php
212+
213+
use Coduo\PHPMatcher\Factory\SimpleFactory;
214+
215+
$factory = new SimpleFactory();
216+
$matcher = $factory->createMatcher();
217+
218+
$matcher->match('9f4db639-0e87-4367-9beb-d64e3f42ae18', '@uuid@');
219+
```
220+
207221
### Array matching
208222

209223
```php

src/Factory/SimpleFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ protected function buildScalarMatchers()
7676
new Matcher\DoubleMatcher($parser),
7777
new Matcher\NumberMatcher(),
7878
new Matcher\ScalarMatcher(),
79-
new Matcher\WildcardMatcher()
79+
new Matcher\WildcardMatcher(),
80+
new Matcher\UuidMatcher(),
8081
));
8182
}
8283

src/Matcher/UuidMatcher.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Coduo\PHPMatcher\Matcher;
4+
5+
use Coduo\ToString\StringConverter;
6+
7+
final class UuidMatcher extends Matcher
8+
{
9+
const UUID_PATTERN = '/^@uuid@$/';
10+
const UUID_FORMAT_PATTERN = '|^[\da-f]{8}-[\da-f]{4}-4[\da-f]{3}-[89ab][\da-f]{3}-[\da-f]{12}$|';
11+
12+
/**
13+
* {@inheritDoc}
14+
*/
15+
public function match($value, $pattern)
16+
{
17+
if (!is_string($value)) {
18+
$this->error = sprintf(
19+
"%s \"%s\" is not a valid UUID: not a string.",
20+
gettype($value),
21+
new StringConverter($value)
22+
);
23+
return false;
24+
}
25+
26+
if (1 !== preg_match(self::UUID_FORMAT_PATTERN, $value)) {
27+
$this->error = sprintf(
28+
"%s \"%s\" is not a valid UUID: invalid format.",
29+
gettype($value),
30+
$value
31+
);
32+
return false;
33+
}
34+
35+
return true;
36+
}
37+
38+
/**
39+
* {@inheritDoc}
40+
*/
41+
public function canMatch($pattern)
42+
{
43+
return is_string($pattern) && 0 !== preg_match(self::UUID_PATTERN, $pattern);
44+
}
45+
}

tests/Matcher/UuidMatcherTest.php

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
namespace Coduo\PHPMatcher\Tests\Matcher;
4+
5+
use Coduo\PHPMatcher\Matcher\UuidMatcher;
6+
7+
class UuidMatcherTest extends \PHPUnit_Framework_TestCase
8+
{
9+
/**
10+
* @var UuidMatcher
11+
*/
12+
private $matcher;
13+
14+
public function setUp()
15+
{
16+
$this->matcher = new UuidMatcher();
17+
}
18+
/**
19+
* @dataProvider positiveCanMatchData
20+
*/
21+
public function test_positive_can_matches($pattern)
22+
{
23+
$this->assertTrue($this->matcher->canMatch($pattern));
24+
}
25+
26+
/**
27+
* @dataProvider negativeCanMatchData
28+
*/
29+
public function test_negative_can_matches($pattern)
30+
{
31+
$this->assertFalse($this->matcher->canMatch($pattern));
32+
}
33+
34+
/**
35+
* @dataProvider positiveMatchData
36+
*/
37+
public function test_positive_match($value, $pattern)
38+
{
39+
$this->assertTrue($this->matcher->match($value, $pattern));
40+
}
41+
42+
/**
43+
* @dataProvider negativeMatchData
44+
*/
45+
public function test_negative_match($value, $pattern)
46+
{
47+
$this->assertFalse($this->matcher->match($value, $pattern));
48+
}
49+
50+
/**
51+
* @dataProvider negativeMatchDescription
52+
*/
53+
public function test_negative_match_description($value, $pattern, $error)
54+
{
55+
$this->matcher->match($value, $pattern);
56+
$this->assertEquals($error, $this->matcher->getError());
57+
}
58+
59+
public static function positiveCanMatchData()
60+
{
61+
return array(
62+
array("@uuid@"),
63+
);
64+
}
65+
66+
public static function positiveMatchData()
67+
{
68+
return array(
69+
array("9f4db639-0e87-4367-9beb-d64e3f42ae18", "@uuid@"),
70+
);
71+
}
72+
73+
public static function negativeCanMatchData()
74+
{
75+
return array(
76+
array("@uuid"),
77+
array("uuid"),
78+
array(1),
79+
);
80+
}
81+
82+
public static function negativeMatchData()
83+
{
84+
return array(
85+
array(1, "@uuid@"),
86+
array(0, "@uuid@"),
87+
array("9f4d-b639-0e87-4367-9beb-d64e3f42ae18", "@uuid@"),
88+
array("9f4db639-0e87-4367-9beb-d64e3f42ae1", "@uuid@"),
89+
array("9f4db639-0e87-4367-9beb-d64e3f42ae181", "@uuid@"),
90+
array("9f4db6390e8743679bebd64e3f42ae18", "@uuid@"),
91+
array("9f4db6390e87-4367-9beb-d64e-3f42ae18", "@uuid@"),
92+
array("9f4db639-0e87-4367-9beb-d64e3f42ae1g", "@uuid@"),
93+
array("9f4db639-0e87-0367-9beb-d64e3f42ae18", "@uuid@"),
94+
);
95+
}
96+
97+
public static function negativeMatchDescription()
98+
{
99+
return array(
100+
array(new \stdClass, "@uuid@", "object \"\\stdClass\" is not a valid UUID: not a string."),
101+
array(1.1, "@uuid@", "double \"1.1\" is not a valid UUID: not a string."),
102+
array(false, "@uuid@", "boolean \"false\" is not a valid UUID: not a string."),
103+
array(1, "@uuid@", "integer \"1\" is not a valid UUID: not a string."),
104+
array("lorem ipsum", "@uuid@", "string \"lorem ipsum\" is not a valid UUID: invalid format."),
105+
array("9f4db639-0e87-4367-9beb-d64e3f42ae1z", "@uuid@", "string \"9f4db639-0e87-4367-9beb-d64e3f42ae1z\" is not a valid UUID: invalid format."),
106+
);
107+
}
108+
}

tests/MatcherTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,6 @@ public function test_text_matcher()
167167
$this->assertTrue(PHPMatcher::match($value, $pattern));
168168
}
169169

170-
171170
public function test_error_when_json_value_does_not_match_json_pattern()
172171
{
173172
$pattern = '{"a": @null@, "b": 4}';
@@ -220,7 +219,8 @@ public function scalarValueExamples()
220219
array('Norbert Orzechowicz', '@string@'),
221220
array(6.66, '@double@'),
222221
array(1, '@integer@'),
223-
array(array('foo'), '@array@')
222+
array(array('foo'), '@array@'),
223+
array('9f4db639-0e87-4367-9beb-d64e3f42ae18', '@uuid@'),
224224
);
225225
}
226226

0 commit comments

Comments
 (0)