2
2
3
3
namespace Coduo \PHPMatcher \Matcher ;
4
4
5
+ use Coduo \PHPMatcher \Matcher \Pattern \Assert \Json ;
6
+
5
7
class JsonMatcher extends Matcher
6
8
{
7
- const TRANSFORM_QUOTATION_PATTERN = '/([^"])@([a-zA-Z0-9\.]+)@([^"])/ ' ;
8
- const TRANSFORM_QUOTATION_REPLACEMENT = '$1"@$2@"$3 ' ;
9
-
10
9
/**
11
10
* @var
12
11
*/
@@ -25,12 +24,12 @@ public function __construct(ValueMatcher $matcher)
25
24
*/
26
25
public function match ($ value , $ pattern )
27
26
{
28
- if (!is_string ( $ value ) || ! $ this -> isValidJson ($ value )) {
27
+ if (!Json:: isValid ($ value )) {
29
28
return false ;
30
29
}
31
30
32
- $ pattern = $ this -> transformPattern ($ pattern );
33
- $ match = $ this ->matcher ->match (json_decode ($ value , true ), json_decode ($ pattern , true ));
31
+ $ transformedPattern = Json:: transformPattern ($ pattern );
32
+ $ match = $ this ->matcher ->match (json_decode ($ value , true ), json_decode ($ transformedPattern , true ));
34
33
if (!$ match ) {
35
34
$ this ->error = $ this ->matcher ->getError ();
36
35
return false ;
@@ -44,29 +43,6 @@ public function match($value, $pattern)
44
43
*/
45
44
public function canMatch ($ pattern )
46
45
{
47
- if (!is_string ($ pattern )) {
48
- return false ;
49
- }
50
-
51
- return $ this ->isValidJson ($ this ->transformPattern ($ pattern ));
46
+ return Json::isValidPattern ($ pattern );
52
47
}
53
-
54
- private function isValidJson ($ string )
55
- {
56
- @json_decode ($ string , true );
57
-
58
- return (json_last_error () === JSON_ERROR_NONE );
59
- }
60
-
61
- /**
62
- * Wraps placeholders which arent wrapped with quotes yet
63
- *
64
- * @param $pattern
65
- * @return mixed
66
- */
67
- private function transformPattern ($ pattern )
68
- {
69
- return preg_replace (self ::TRANSFORM_QUOTATION_PATTERN , self ::TRANSFORM_QUOTATION_REPLACEMENT , $ pattern );
70
- }
71
-
72
48
}
0 commit comments