Coduo\PHPMatcher\Matcher\JsonMatcher::match PHP Méthode

match() public méthode

{@inheritDoc}
public match ( $value, $pattern )
    public function match($value, $pattern)
    {
        if (parent::match($value, $pattern)) {
            return true;
        }
        if (!Json::isValid($value)) {
            $this->error = sprintf("Invalid given JSON of value. %s", $this->getErrorMessage());
            return false;
        }
        if (!Json::isValidPattern($pattern)) {
            $this->error = sprintf("Invalid given JSON of pattern. %s", $this->getErrorMessage());
            return false;
        }
        $transformedPattern = Json::transformPattern($pattern);
        $match = $this->matcher->match(json_decode($value, true), json_decode($transformedPattern, true));
        if (!$match) {
            $this->error = $this->matcher->getError();
            return false;
        }
        return true;
    }

Usage Example

 public function test_error_when_path_in_nested_value_does_not_exist()
 {
     $value = json_encode(array('foo' => array('bar' => array())));
     $pattern = json_encode(array('foo' => array('bar' => array('faz' => 'faz value'))));
     $this->assertFalse($this->matcher->match($value, $pattern));
     $this->assertEquals($this->matcher->getError(), 'There is no element under path [foo][bar][faz] in value.');
 }
All Usage Examples Of Coduo\PHPMatcher\Matcher\JsonMatcher::match