1
1
<?php
2
2
namespace JsonMatcher ;
3
3
4
- class Matcher
5
- {
6
- public function match ($ matcher , $ matchTo )
7
- {
8
-
9
- if (is_array ($ matcher )) {
10
- if (-1 === $ this ->matchArray ($ matcher , $ matchTo )) {
11
- return false ;
12
- }
13
- } elseif (is_scalar ($ matcher ) && strpos ($ matchTo , '@ ' ) > -1 ) {
14
- if (-1 === $ this ->matchType ($ matcher , $ matchTo )) {
15
- return false ;
16
- }
17
- } elseif (is_scalar ($ matcher )) {
18
- if (-1 === $ this ->matchScalar ($ matcher , $ matchTo )) {
19
- return false ;
20
- }
21
- }
22
- return true ;
23
- }
4
+ use JsonMatcher \Matcher \PropertyMatcher ;
24
5
6
+ class Matcher implements PropertyMatcher
7
+ {
8
+ /**
9
+ * @var Matcher\PropertyMatcher
10
+ */
11
+ private $ matcher ;
25
12
26
- private function matchScalar ( $ json , $ expectedJson )
13
+ public function __construct ( PropertyMatcher $ matcher )
27
14
{
28
- if ($ json !== $ expectedJson ) {
29
- return -1 ;
30
- }
15
+ $ this ->matcher = $ matcher ;
31
16
}
32
17
33
- private function matchType ( $ json , $ type )
18
+ public function match ( $ matcher , $ pattern )
34
19
{
35
- $ type = str_replace ("@ " , "" , $ type );
36
-
37
- if (gettype ($ json ) !== $ type ) {
38
- return -1 ;
39
- }
20
+ return $ this ->matcher ->match ($ matcher , $ pattern );
40
21
}
41
22
42
-
43
- private function matchArray (array $ array , array $ arrayToMatch )
23
+ public function canMatch ($ pattern )
44
24
{
45
-
46
- if (count ($ array ) !== count ($ arrayToMatch ) ) {
47
- return -1 ;
48
- }
49
-
50
- $ match = function ($ value , $ array , $ arrayToMatch ) {
51
-
52
- $ key = array_search ($ value , $ arrayToMatch );
53
- if ($ key !== false ) {
54
- $ this ->match ($ array [$ key ], $ arrayToMatch [$ key ]);
55
-
56
- return true ;
57
- }
58
-
59
- $ key = array_search ($ value , $ array );
60
- if (is_scalar ($ arrayToMatch [$ key ]) && strpos ($ arrayToMatch [$ key ], '@ ' ) > - 1 ) {
61
- $ this ->match ($ value , $ arrayToMatch [$ key ]);
62
-
63
- return true ;
64
- } else {
65
- return false ;
66
- }
67
-
68
- };
69
-
70
- foreach ($ array as $ value ) {
71
-
72
- if ($ match ($ value , $ array , $ arrayToMatch )) {
73
- //unset($array[$key], $arrayToMatch[$key]);
74
- } else {
75
- return -1 ;
76
- }
77
- }
25
+ return true ;
78
26
}
79
27
80
-
28
+
81
29
}
0 commit comments