| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Coduo\PHPMatcher\Matcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Coduo\PHPMatcher\Matcher\Pattern\Assert\Json; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | final class JsonMatcher extends Matcher | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |      * @var | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     private $matcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * @param ValueMatcher $matcher | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     public function __construct(ValueMatcher $matcher) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         $this->matcher = $matcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     public function match($value, $pattern) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         if (parent::match($value, $pattern)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         if (!Json::isValid($value)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             $this->error = sprintf("Invalid given JSON of value. %s", $this->getErrorMessage()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         if (!Json::isValidPattern($pattern) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             $this->error = sprintf("Invalid given JSON of pattern. %s", $this->getErrorMessage()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $transformedPattern = Json::transformPattern($pattern); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $match = $this->matcher->match(json_decode($value, true), json_decode($transformedPattern, true)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         if (!$match) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             $this->error = $this->matcher->getError(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     public function canMatch($pattern) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         return Json::isValidPattern($pattern); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |     private function getErrorMessage() | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |         switch(json_last_error()) { | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |             case JSON_ERROR_DEPTH: | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |                 return 'Maximum stack depth exceeded'; | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |             case JSON_ERROR_STATE_MISMATCH: | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |                 return 'Underflow or the modes mismatch'; | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |             case JSON_ERROR_CTRL_CHAR: | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |                 return 'Unexpected control character found'; | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |             case JSON_ERROR_SYNTAX: | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |                 return 'Syntax error, malformed JSON'; | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |             case JSON_ERROR_UTF8: | 
            
                                                        
            
                                    
            
            
                | 71 |  |  |                 return 'Malformed UTF-8 characters, possibly incorrectly encoded'; | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |             default: | 
            
                                                        
            
                                    
            
            
                | 73 |  |  |                 return 'Unknown error'; | 
            
                                                        
            
                                    
            
            
                | 74 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 76 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 77 |  |  |  |