Skip to content

Commit d1e1a41

Browse files
committed
Added expression matcher
1 parent 33a3744 commit d1e1a41

File tree

5 files changed

+112
-2
lines changed

5 files changed

+112
-2
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"license": "MIT",
66
"require": {
77
"php": ">=5.3.0",
8-
"symfony/property-access": "~2.3"
8+
"symfony/property-access": "~2.3",
9+
"symfony/expression-language": "~2.4"
910
},
1011
"require-dev": {
1112
"phpunit/phpunit": "3.7.*"

match.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use JsonMatcher\Matcher\ArrayMatcher;
44
use JsonMatcher\Matcher\ChainMatcher;
5+
use JsonMatcher\Matcher\ExpressionMatcher;
56
use JsonMatcher\Matcher\JsonMatcher;
67
use JsonMatcher\Matcher\ScalarMatcher;
78
use JsonMatcher\Matcher\TypeMatcher;
@@ -31,6 +32,7 @@
3132
function match($value, $pattern)
3233
{
3334
$scalarMatchers = new ChainMatcher(array(
35+
new ExpressionMatcher(),
3436
new TypeMatcher(),
3537
new ScalarMatcher(),
3638
new WildcardMatcher()
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace JsonMatcher\Matcher;
4+
5+
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
6+
7+
class ExpressionMatcher implements PropertyMatcher
8+
{
9+
/**
10+
* {@inheritDoc}
11+
*/
12+
public function match($value, $pattern)
13+
{
14+
$language = new ExpressionLanguage();
15+
preg_match("/^expr\((.*?)\)$/", $pattern, $matches);
16+
return $language->evaluate($matches[1], array('value' => $value));
17+
}
18+
19+
/**
20+
* {@inheritDoc}
21+
*/
22+
public function canMatch($pattern)
23+
{
24+
return is_string($pattern) && 0 !== preg_match("/^expr\((.*?)\)$/", $pattern);
25+
}
26+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
namespace JsonMatcher\Tests\Matcher;
3+
4+
use JsonMatcher\Matcher\ExpressionMatcher;
5+
6+
class ExpressionMatcherTest extends \PHPUnit_Framework_TestCase
7+
{
8+
/**
9+
* @dataProvider positiveCanMatchData
10+
*/
11+
public function test_positive_can_matches($pattern)
12+
{
13+
$matcher = new ExpressionMatcher();
14+
$this->assertTrue($matcher->canMatch($pattern));
15+
}
16+
17+
/**
18+
* @dataProvider negativeCanMatchData
19+
*/
20+
public function test_negative_can_matches($pattern)
21+
{
22+
$matcher = new ExpressionMatcher();
23+
$this->assertFalse($matcher->canMatch($pattern));
24+
}
25+
26+
/**
27+
* @dataProvider positiveMatchData
28+
*/
29+
public function test_positive_match($value, $pattern)
30+
{
31+
$matcher = new ExpressionMatcher();
32+
$this->assertTrue($matcher->match($value, $pattern));
33+
}
34+
35+
/**
36+
* @dataProvider negativeMatchData
37+
*/
38+
public function test_negative_match($value, $pattern)
39+
{
40+
$matcher = new ExpressionMatcher();
41+
$this->assertFalse($matcher->match($value, $pattern));
42+
}
43+
44+
public static function positiveCanMatchData()
45+
{
46+
return array(
47+
array("expr(1 > 2)"),
48+
array("expr(value == 'foo')"),
49+
);
50+
}
51+
52+
public static function negativeCanMatchData()
53+
{
54+
return array(
55+
array("@integer"),
56+
array("expr("),
57+
array("@string"),
58+
array(new \stdClass),
59+
array(array("foobar"))
60+
);
61+
}
62+
63+
public static function positiveMatchData()
64+
{
65+
return array(
66+
array(4, "expr(value > 2)"),
67+
array("foo", "expr(value == 'foo')"),
68+
array(new \DateTime('2014-04-01'), "expr(value.format('Y-m-d') == '2014-04-01')")
69+
);
70+
}
71+
72+
public static function negativeMatchData()
73+
{
74+
return array(
75+
array(4, "expr(value < 2)"),
76+
array("foo", "expr(value != 'foo')"),
77+
);
78+
}
79+
}

tests/JsonMatcher/MatcherTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use JsonMatcher\Matcher\ArrayMatcher;
55
use JsonMatcher\Matcher\ChainMatcher;
6+
use JsonMatcher\Matcher\ExpressionMatcher;
67
use JsonMatcher\Matcher\JsonMatcher;
78
use JsonMatcher\Matcher\ScalarMatcher;
89
use JsonMatcher\Matcher\TypeMatcher;
@@ -18,6 +19,7 @@ class MatcherTest extends \PHPUnit_Framework_TestCase
1819
public function setUp()
1920
{
2021
$scalarMatchers = new ChainMatcher(array(
22+
new ExpressionMatcher(),
2123
new TypeMatcher(),
2224
new ScalarMatcher(),
2325
new WildcardMatcher()
@@ -155,7 +157,7 @@ public function test_matcher_with_json()
155157
"id": "@integer@",
156158
"firstName": "Michał",
157159
"lastName": "Dąbrowski",
158-
"enabled": "@boolean@",
160+
"enabled": "expr(value == false)",
159161
"roles": "@array@"
160162
}
161163
],

0 commit comments

Comments
 (0)