Skip to content

Commit 33a3744

Browse files
committed
Added match global function
1 parent ed142bd commit 33a3744

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"psr-0": {
1515
"JsonMatcher": "src/",
1616
"JsonMatcher\\Tests": "tests/"
17-
}
17+
},
18+
"files": ["match.php"]
1819
},
1920
"config": {
2021
"bin-dir": "bin"

match.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
use JsonMatcher\Matcher\ArrayMatcher;
4+
use JsonMatcher\Matcher\ChainMatcher;
5+
use JsonMatcher\Matcher\JsonMatcher;
6+
use JsonMatcher\Matcher\ScalarMatcher;
7+
use JsonMatcher\Matcher\TypeMatcher;
8+
use JsonMatcher\Matcher\WildcardMatcher;
9+
use JsonMatcher\Matcher;
10+
11+
if (is_dir($vendor = __DIR__ . '/../vendor')) {
12+
require_once($vendor . '/autoload.php');
13+
} elseif (is_dir($vendor = __DIR__ . '/../../../vendor')) {
14+
require_once($vendor . '/autoload.php');
15+
} elseif (is_dir($vendor = __DIR__ . '/vendor')) {
16+
require_once($vendor . '/autoload.php');
17+
} else {
18+
die(
19+
'You must set up the project dependencies, run the following commands:' . PHP_EOL .
20+
'curl -s http://getcomposer.org/installer | php' . PHP_EOL .
21+
'php composer.phar install' . PHP_EOL
22+
);
23+
}
24+
25+
if (!function_exists('match')) {
26+
/**
27+
* @param mixed $value
28+
* @param mixed $pattern
29+
* @return boolean
30+
*/
31+
function match($value, $pattern)
32+
{
33+
$scalarMatchers = new ChainMatcher(array(
34+
new TypeMatcher(),
35+
new ScalarMatcher(),
36+
new WildcardMatcher()
37+
));
38+
$arrayMatcher = new ArrayMatcher($scalarMatchers);
39+
$matcher = new Matcher(new ChainMatcher(array(
40+
$scalarMatchers,
41+
$arrayMatcher,
42+
new JsonMatcher($arrayMatcher)
43+
)));
44+
45+
return $matcher->match($value, $pattern);
46+
}
47+
}

tests/JsonMatcher/MatcherTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,28 @@ public function test_matcher_with_array_value()
7474
'data' => '@wildcard@',
7575
)
7676
));
77+
78+
$this->assertTrue(match(
79+
$this->arrayValue,
80+
array(
81+
'users' => array(
82+
array(
83+
'id' => '@integer@',
84+
'firstName' => '@string@',
85+
'lastName' => 'Orzechowicz',
86+
'enabled' => '@boolean@'
87+
),
88+
array(
89+
'id' => '@integer@',
90+
'firstName' => '@string@',
91+
'lastName' => 'Dąbrowski',
92+
'enabled' => '@boolean@',
93+
)
94+
),
95+
'readyToUse' => true,
96+
'data' => '@wildcard@',
97+
)
98+
));
7799
}
78100

79101
public function test_matcher_with_scalar_values()
@@ -82,10 +104,18 @@ public function test_matcher_with_scalar_values()
82104
'Norbert Orzechowicz',
83105
'@string@'
84106
));
107+
$this->assertTrue(match(
108+
'Norbert Orzechowicz',
109+
'@string@'
110+
));
85111
$this->assertTrue($this->matcher->match(
86112
6.66,
87113
'@double@'
88114
));
115+
$this->assertTrue(match(
116+
6.66,
117+
'@double@'
118+
));
89119
}
90120

91121
public function test_matcher_with_json()
@@ -135,5 +165,6 @@ public function test_matcher_with_json()
135165

136166

137167
$this->assertTrue($this->matcher->match($json, $jsonPattern));
168+
$this->assertTrue(match($json, $jsonPattern));
138169
}
139170
}

0 commit comments

Comments
 (0)