Skip to content

Commit 41d01f6

Browse files
committed
Replaced Type matcher with String, Integer, Number, Double, Boolean and Array matchers
1 parent 5210c93 commit 41d01f6

16 files changed

+627
-107
lines changed

src/Coduo/PHPMatcher/Factory/SimpleFactory.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,12 @@ protected function buildScalarMatchers()
3838
return new Matcher\ChainMatcher(array(
3939
new Matcher\CallbackMatcher(),
4040
new Matcher\ExpressionMatcher(),
41-
new Matcher\TypeMatcher(),
4241
new Matcher\NullMatcher(),
42+
new Matcher\StringMatcher(),
43+
new Matcher\IntegerMatcher(),
44+
new Matcher\BooleanMatcher(),
45+
new Matcher\DoubleMatcher(),
46+
new Matcher\NumberMatcher(),
4347
new Matcher\ScalarMatcher(),
4448
new Matcher\WildcardMatcher()
4549
));

src/Coduo/PHPMatcher/Matcher/ArrayMatcher.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
namespace Coduo\PHPMatcher\Matcher;
44

5+
use Coduo\ToString\String;
56
use Symfony\Component\PropertyAccess\PropertyAccess;
67
use Symfony\Component\PropertyAccess\PropertyAccessor;
78

89
class ArrayMatcher extends Matcher
910
{
11+
const ARRAY_PATTERN = "/^@array@$/";
12+
1013
const UNBOUNDED_PATTERN = '@...@';
1114

1215
/**
@@ -33,9 +36,14 @@ public function __construct(ValueMatcher $propertyMatcher)
3336
public function match($value, $pattern)
3437
{
3538
if (!is_array($value)) {
39+
$this->error = sprintf("%s \"%s\" is not a valid array.", gettype($value), new String($value));
3640
return false;
3741
}
3842

43+
if ($this->isArrayPattern($pattern)) {
44+
return true;
45+
}
46+
3947
if (false === $this->iterateMatch($value, $pattern)) {
4048
return false;
4149
}
@@ -48,7 +56,12 @@ public function match($value, $pattern)
4856
*/
4957
public function canMatch($pattern)
5058
{
51-
return is_array($pattern);
59+
return is_array($pattern) || $this->isArrayPattern($pattern);
60+
}
61+
62+
private function isArrayPattern($pattern)
63+
{
64+
return is_string($pattern) && 0 !== preg_match(self::ARRAY_PATTERN, $pattern);
5265
}
5366

5467
/**
@@ -86,6 +99,10 @@ private function iterateMatch(array $values, array $patterns, $parentPath = "")
8699
return false;
87100
}
88101

102+
if ($this->isArrayPattern($pattern)) {
103+
continue;
104+
}
105+
89106
if (false === $this->iterateMatch($value, $pattern, $this->formatFullPath($parentPath, $path))) {
90107
return false;
91108
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Coduo\PHPMatcher\Matcher;
4+
5+
use Coduo\ToString\String;
6+
7+
class BooleanMatcher extends Matcher
8+
{
9+
const BOOLEAN_PATTERN = '/^@boolean@$/';
10+
11+
/**
12+
* {@inheritDoc}
13+
*/
14+
public function match($value, $pattern)
15+
{
16+
if (!is_bool($value)) {
17+
$this->error = sprintf("%s \"%s\" is not a valid boolean.", gettype($value), new String($value));
18+
return false;
19+
}
20+
21+
return true;
22+
}
23+
24+
/**
25+
* {@inheritDoc}
26+
*/
27+
public function canMatch($pattern)
28+
{
29+
return is_string($pattern) && 0 !== preg_match(self::BOOLEAN_PATTERN, $pattern);
30+
}
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Coduo\PHPMatcher\Matcher;
4+
5+
use Coduo\ToString\String;
6+
7+
class DoubleMatcher extends Matcher
8+
{
9+
const DOUBLE_PATTERN = '/^@double@$/';
10+
11+
/**
12+
* {@inheritDoc}
13+
*/
14+
public function match($value, $pattern)
15+
{
16+
if (!is_double($value)) {
17+
$this->error = sprintf("%s \"%s\" is not a valid double.", gettype($value), new String($value));
18+
return false;
19+
}
20+
21+
return true;
22+
}
23+
24+
/**
25+
* {@inheritDoc}
26+
*/
27+
public function canMatch($pattern)
28+
{
29+
return is_string($pattern) && 0 !== preg_match(self::DOUBLE_PATTERN, $pattern);
30+
}
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Coduo\PHPMatcher\Matcher;
4+
5+
use Coduo\ToString\String;
6+
7+
class IntegerMatcher extends Matcher
8+
{
9+
const INTEGER_PATTERN = '/^@integer@$/';
10+
11+
/**
12+
* {@inheritDoc}
13+
*/
14+
public function match($value, $pattern)
15+
{
16+
if (!is_integer($value)) {
17+
$this->error = sprintf("%s \"%s\" is not a valid integer.", gettype($value), new String($value));
18+
return false;
19+
}
20+
21+
return true;
22+
}
23+
24+
/**
25+
* {@inheritDoc}
26+
*/
27+
public function canMatch($pattern)
28+
{
29+
return is_string($pattern) && 0 !== preg_match(self::INTEGER_PATTERN, $pattern);
30+
}
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Coduo\PHPMatcher\Matcher;
4+
5+
use Coduo\ToString\String;
6+
7+
class NumberMatcher extends Matcher
8+
{
9+
const NUMBER_PATTERN = '/^@number@$/';
10+
11+
/**
12+
* {@inheritDoc}
13+
*/
14+
public function match($value, $pattern)
15+
{
16+
if (!is_numeric($value)) {
17+
$this->error = sprintf("%s \"%s\" is not a valid number.", gettype($value), new String($value));
18+
return false;
19+
}
20+
21+
return true;
22+
}
23+
24+
/**
25+
* {@inheritDoc}
26+
*/
27+
public function canMatch($pattern)
28+
{
29+
return is_string($pattern) && 0 !== preg_match(self::NUMBER_PATTERN, $pattern);
30+
}
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Coduo\PHPMatcher\Matcher;
4+
5+
use Coduo\ToString\String;
6+
7+
class StringMatcher extends Matcher
8+
{
9+
const STRING_PATTERN = '/^@string@$/';
10+
11+
/**
12+
* {@inheritDoc}
13+
*/
14+
public function match($value, $pattern)
15+
{
16+
if (!is_string($value)) {
17+
$this->error = sprintf("%s \"%s\" is not a valid string.", gettype($value), new String($value));
18+
return false;
19+
}
20+
21+
return true;
22+
}
23+
24+
/**
25+
* {@inheritDoc}
26+
*/
27+
public function canMatch($pattern)
28+
{
29+
return is_string($pattern) && 0 !== preg_match(self::STRING_PATTERN, $pattern);
30+
}
31+
}

src/Coduo/PHPMatcher/Matcher/TypeMatcher.php

Lines changed: 0 additions & 37 deletions
This file was deleted.

tests/Coduo/PHPMatcher/Matcher/ArrayMatcherTest.php

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
11
<?php
22
namespace Coduo\PHPMatcher\Tests\Matcher;
33

4-
use Coduo\PHPMatcher\Matcher\ArrayMatcher;
5-
use Coduo\PHPMatcher\Matcher\ChainMatcher;
6-
use Coduo\PHPMatcher\Matcher\ScalarMatcher;
7-
use Coduo\PHPMatcher\Matcher\TypeMatcher;
8-
use Coduo\PHPMatcher\Matcher\WildcardMatcher;
4+
use Coduo\PHPMatcher\Matcher;
95

106
class ArrayMatcherTest extends \PHPUnit_Framework_TestCase
117
{
128
/**
13-
* @var ArrayMatcher
9+
* @var Matcher\ArrayMatcher
1410
*/
1511
private $matcher;
1612

1713
public function setUp()
1814
{
19-
$this->matcher = new ArrayMatcher(
20-
new ChainMatcher(array(
21-
new ScalarMatcher(),
22-
new TypeMatcher(),
23-
new WildcardMatcher()
15+
$this->matcher = new Matcher\ArrayMatcher(
16+
new Matcher\ChainMatcher(array(
17+
new Matcher\CallbackMatcher(),
18+
new Matcher\ExpressionMatcher(),
19+
new Matcher\NullMatcher(),
20+
new Matcher\StringMatcher(),
21+
new Matcher\IntegerMatcher(),
22+
new Matcher\BooleanMatcher(),
23+
new Matcher\DoubleMatcher(),
24+
new Matcher\NumberMatcher(),
25+
new Matcher\ScalarMatcher(),
26+
new Matcher\WildcardMatcher(),
2427
))
2528
);
2629
}
@@ -43,9 +46,9 @@ public function test_negative_match_arrays($value, $pattern)
4346

4447
public function test_negative_match_when_cant_find_matcher_that_can_match_array_element()
4548
{
46-
$matcher = new ArrayMatcher(
47-
new ChainMatcher(array(
48-
new WildcardMatcher()
49+
$matcher = new Matcher\ArrayMatcher(
50+
new Matcher\ChainMatcher(array(
51+
new Matcher\WildcardMatcher()
4952
))
5053
);
5154

@@ -94,6 +97,17 @@ public function test_error_when_matching_fail()
9497
$this->assertEquals($this->matcher->getError(), '"foo value" does not match "bar value".');
9598
}
9699

100+
public function test_error_message_when_matching_non_array_value()
101+
{
102+
$this->assertFalse($this->matcher->match(new \DateTime(), "@array@"));
103+
$this->assertEquals($this->matcher->getError(), "object \"\\DateTime\" is not a valid array.");
104+
}
105+
106+
public function test_matching_array_to_array_pattern()
107+
{
108+
$this->assertTrue($this->matcher->match(array("foo", "bar"), "@array@"));
109+
}
110+
97111
public static function positiveMatchData()
98112
{
99113
$simpleArr = array(

0 commit comments

Comments
 (0)