File tree Expand file tree Collapse file tree 3 files changed +80
-1
lines changed Expand file tree Collapse file tree 3 files changed +80
-1
lines changed Original file line number Diff line number Diff line change 14
14
"psr-0" : {
15
15
"JsonMatcher" : " src/" ,
16
16
"JsonMatcher\\ Tests" : " tests/"
17
- }
17
+ },
18
+ "files" : [" match.php" ]
18
19
},
19
20
"config" : {
20
21
"bin-dir" : " bin"
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -74,6 +74,28 @@ public function test_matcher_with_array_value()
74
74
'data ' => '@wildcard@ ' ,
75
75
)
76
76
));
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
+ ));
77
99
}
78
100
79
101
public function test_matcher_with_scalar_values()
@@ -82,10 +104,18 @@ public function test_matcher_with_scalar_values()
82
104
'Norbert Orzechowicz ' ,
83
105
'@string@ '
84
106
));
107
+ $ this ->assertTrue(match (
108
+ 'Norbert Orzechowicz ' ,
109
+ '@string@ '
110
+ ));
85
111
$ this ->assertTrue ($ this ->matcher ->match (
86
112
6.66 ,
87
113
'@double@ '
88
114
));
115
+ $ this ->assertTrue(match (
116
+ 6.66 ,
117
+ '@double@ '
118
+ ));
89
119
}
90
120
91
121
public function test_matcher_with_json()
@@ -135,5 +165,6 @@ public function test_matcher_with_json()
135
165
136
166
137
167
$ this ->assertTrue ($ this ->matcher ->match ($ json , $ jsonPattern ));
168
+ $ this ->assertTrue(match ($ json , $ jsonPattern ));
138
169
}
139
170
}
You can’t perform that action at this time.
0 commit comments