Skip to content

JsonMatcher and some smaller fixes #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 17, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 71 additions & 31 deletions src/JsonMatcher/Matcher/ArrayMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\Exception\NoSuchIndexException;
use Symfony\Component\PropertyAccess\PropertyAccessor;

class ArrayMatcher implements PropertyMatcher
{
Expand All @@ -14,6 +15,11 @@ class ArrayMatcher implements PropertyMatcher

private $paths;

/**
* @var PropertyAccessor
*/
private $accessor;

public function __construct(PropertyMatcher $propertyMatcher)
{
$this->propertyMatcher = $propertyMatcher;
Expand All @@ -24,60 +30,94 @@ public function __construct(PropertyMatcher $propertyMatcher)
*/
public function match($value, $pattern)
{
$accessorBuilder = PropertyAccess::createPropertyAccessorBuilder();
$accessorBuilder->enableExceptionOnInvalidIndex();
$accessor = $accessorBuilder->getPropertyAccessor();
if (!is_array($value)) {
return false;
}

if (false === $this->iterateMatch($value, $pattern)) {
return false;
}

return true;
}

$this->paths = array();
/**
* {@inheritDoc}
*/
public function canMatch($pattern)
{
return is_array($pattern);
}

/**
* @param array $value
* @param array $pattern
* @return bool
*/
private function iterateMatch(array $value, array $pattern)
{
foreach ($value as $key => $element) {
$path = sprintf("[%s]", $key);

if (is_array($element)) {
$this->buildPath($element, $path);
continue;
if (!$this->hasValue($pattern, $path)) {
return false;
}
$elementPattern = $this->getValue($pattern, $path);
if ($this->propertyMatcher->canMatch($elementPattern)) {
if (true === $this->propertyMatcher->match($element, $elementPattern)) {
continue;
}
}

$this->paths[] = $path;
}

foreach ($this->paths as $path) {
$elementValue = $accessor->getValue($value, $path);
try {
$patternValue = $accessor->getValue($pattern, $path);
} catch (NoSuchIndexException $e) {
if (!is_array($element)) {
return false;
}

if ($this->propertyMatcher->canMatch($patternValue)) {
if (false === $this->propertyMatcher->match($elementValue, $patternValue)) {
return false;
}
if (false === $this->iterateMatch($element, $elementPattern)) {
return false;
}
}
}

/**
* @param $array
* @param $path
* @return bool
*/
private function hasValue($array, $path)
{
try {
$this->getPropertyAccessor()->getValue($array, $path);
} catch (NoSuchIndexException $e) {
return false;
}

return true;
}

/**
* {@inheritDoc}
* @param $array
* @param $path
* @return mixed
*/
public function canMatch($pattern)
private function getValue($array, $path)
{
return is_array($pattern);
return $this->getPropertyAccessor()->getValue($array, $path);
}

private function buildPath(array $array, $parentPath)
/**
* @return \Symfony\Component\PropertyAccess\PropertyAccessorInterface
*/
private function getPropertyAccessor()
{
foreach ($array as $key => $element) {
$path = sprintf("%s[%s]", $parentPath, $key);
if (isset($this->accessor)) {
return $this->accessor;
}

if (is_array($element)) {
$this->buildPath($element, $path);
continue;
}
$accessorBuilder = PropertyAccess::createPropertyAccessorBuilder();
$accessorBuilder->enableExceptionOnInvalidIndex();
$this->accessor = $accessorBuilder->getPropertyAccessor();

$this->paths[] = $path;
}
return $this->accessor;
}
}
45 changes: 45 additions & 0 deletions src/JsonMatcher/Matcher/JsonMatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace JsonMatcher\Matcher;

class JsonMatcher implements PropertyMatcher
{
/**
* @var
*/
private $matcher;

/**
* @param PropertyMatcher $matcher
*/
public function __construct(PropertyMatcher $matcher)
{
$this->matcher = $matcher;
}

/**
* {@inheritDoc}
*/
public function match($value, $pattern)
{
if (!is_string($value) || !$this->isValidJson($value)) {
return false;
}

return $this->matcher->match(json_decode($value, true), json_decode($pattern, true));
}

/**
* {@inheritDoc}
*/
public function canMatch($pattern)
{
return is_string($pattern) && $this->isValidJson($pattern);
}

private function isValidJson($string)
{
@json_decode($string, true);
return (json_last_error() == JSON_ERROR_NONE);
}
}
4 changes: 1 addition & 3 deletions src/JsonMatcher/Matcher/TypeMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

class TypeMatcher implements PropertyMatcher
{

/**
* {@inheritDoc}
*/
Expand All @@ -18,12 +17,11 @@ public function match($value, $pattern)
*/
public function canMatch($pattern)
{
return is_string($pattern) && 0 !== preg_match("/^@(string|integer|boolean|double)@$/", $pattern);
return is_string($pattern) && 0 !== preg_match("/^@(string|integer|boolean|double|array)@$/", $pattern);
}

private function extractType($pattern)
{
return str_replace("@", "", $pattern);
}

}
2 changes: 1 addition & 1 deletion src/JsonMatcher/Matcher/WildcardMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function match($matcher, $pattern)
*/
public function canMatch($pattern)
{
return '*' === $pattern;
return is_string($pattern) && 0 !== preg_match("/^@(\*|wildcard)@$/", $pattern);
}

}
26 changes: 0 additions & 26 deletions src/autoload.php

This file was deleted.

25 changes: 20 additions & 5 deletions tests/JsonMatcher/Matcher/ArrayMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,39 @@
use JsonMatcher\Matcher\ArrayMatcher;
use JsonMatcher\Matcher\ChainMatcher;
use JsonMatcher\Matcher\ScalarMatcher;
use JsonMatcher\Matcher\WildcardMatcher;

class ArrayMatcherTest extends \PHPUnit_Framework_TestCase
{
/**
* @var ArrayMatcher
*/
private $matcher;

public function setUp()
{
$this->matcher = new ArrayMatcher(
new ChainMatcher(array(
new ScalarMatcher(),
new WildcardMatcher()
))
);
}

/**
* @dataProvider positiveMatchData
*/
public function test_positive_match_arrays($value, $pattern)
{
$matcher = new ArrayMatcher(new ScalarMatcher());
$this->assertTrue($matcher->match($value, $pattern));
$this->assertTrue($this->matcher->match($value, $pattern));
}

/**
* @dataProvider negativeMatchData
*/
public function test_negative_match_arrays($value, $pattern)
{
$matcher = new ArrayMatcher(new ScalarMatcher());
$this->assertFalse($matcher->match($value, $pattern));
$this->assertFalse($this->matcher->match($value, $pattern));
}

public static function positiveMatchData()
Expand All @@ -48,7 +62,8 @@ public static function positiveMatchData()
array($simpleArr, $simpleArr),
array(array(), array()),
array(array('key' => 'val'), array('key' => 'val')),
array(array(1), array(1))
array(array(1), array(1)),
array(array('roles' => array('ROLE_ADMIN', 'ROLE_DEVELOPER')), array('roles' => '@wildcard@'))
);
}

Expand Down
Loading