22
33namespace Coduo \PHPMatcher \Matcher ;
44
5+ use Coduo \PHPMatcher \Matcher \Pattern \Assert \Json ;
6+
57class JsonMatcher extends Matcher
68{
7- const TRANSFORM_QUOTATION_PATTERN = '/([^"])@([a-zA-Z0-9\.]+)@([^"])/ ' ;
8- const TRANSFORM_QUOTATION_REPLACEMENT = '$1"@$2@"$3 ' ;
9-
109 /**
1110 * @var
1211 */
@@ -25,12 +24,12 @@ public function __construct(ValueMatcher $matcher)
2524 */
2625 public function match ($ value , $ pattern )
2726 {
28- if (!is_string ( $ value ) || ! $ this -> isValidJson ($ value )) {
27+ if (!Json:: isValid ($ value )) {
2928 return false ;
3029 }
3130
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 ));
3433 if (!$ match ) {
3534 $ this ->error = $ this ->matcher ->getError ();
3635 return false ;
@@ -44,29 +43,6 @@ public function match($value, $pattern)
4443 */
4544 public function canMatch ($ pattern )
4645 {
47- if (!is_string ($ pattern )) {
48- return false ;
49- }
50-
51- return $ this ->isValidJson ($ this ->transformPattern ($ pattern ));
46+ return Json::isValidPattern ($ pattern );
5247 }
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-
7248}
0 commit comments