Skip to content

Commit

Permalink
Merge branch 'master' of github.com:defrag/JsonMatcher
Browse files Browse the repository at this point in the history
* 'master' of github.com:defrag/JsonMatcher:
  Itroduce ArrayMatcher
  • Loading branch information
defrag committed Apr 15, 2014
2 parents f085f01 + 388f2fe commit 64dd127
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
vendor/
bin/
composer.lock
.DS_Store
.idea
.idea
16 changes: 13 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,24 @@
"keywords": ["json", "matcher"],
"license": "MIT",
"require": {
"php": ">=5.3.0"
"php": ">=5.3.0",
"symfony/property-access": "~2.3"
},
"require-dev": {
"phpunit/phpunit": "3.7.*@dev"
},
"autoload": {
"psr-0": { "JsonMatcher": "src/" }
"psr-0": {
"JsonMatcher": "src/",
"JsonMatcher\\Tests": "tests/"
}
},
"config": {
"bin-dir": "bin"
},
"extra": {
"branch-alias": {
"dev-master": "2.0-dev"
}
}
}
}
62 changes: 62 additions & 0 deletions src/JsonMatcher/ArrayMatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace JsonMatcher;

use Symfony\Component\PropertyAccess\PropertyAccess;

class ArrayMatcher
{
/**
* @var array
*/
private $matcher;

/**
* @var array
*/
private $paths;

public function __construct(array $matcher)
{
$this->matcher = $matcher;
$this->paths = array();
foreach ($this->matcher as $key => $element) {
$path = sprintf("[%s]", $key);

if (is_array($element)) {
$this->buildPath($element, $path);
continue;
}

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

public function match(array $pattern)
{
$accessorBuilder = PropertyAccess::createPropertyAccessorBuilder();
$accessorBuilder->enableExceptionOnInvalidIndex();
$accessor = $accessorBuilder->getPropertyAccessor();

foreach ($this->paths as $path) {
$value = $accessor->getValue($this->matcher, $path);
$patternValue = $accessor->getValue($pattern, $path);
}

return false;
}

private function buildPath(array $array, $parentPath)
{
foreach ($array as $key => $element) {
$path = sprintf("%s[%s]", $parentPath, $key);

if (is_array($element)) {
$this->buildPath($element, $path);
continue;
}

$this->paths[] = $path;
}
}
}
36 changes: 36 additions & 0 deletions tests/JsonMatcher/ArrayMatcherTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
namespace JsonMatcher\Tests;

use JsonMatcher\ArrayMatcher;

class ArrayMatcherTest extends \PHPUnit_Framework_TestCase
{
function test_matcher()
{
$matcher = new ArrayMatcher([
'users' => [
[
'firstName' => 'Norbert',
'lastName' => 'Orzechowicz'
],
[
'firstName' => 'Michał',
'lastName' => 'Dąbrowski'
]
]
]);

$this->assertTrue($matcher->match([
'users' => [
[
'firstName' => 'Norbert',
'lastName' => 'Orzechowicz'
],
[
'firstName' => 'Michał',
'lastName' => 'Dąbrowski'
]
]
]));
}
}
1 change: 0 additions & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<?php

$loader = require_once __DIR__ . '/../vendor/autoload.php';
$loader->add('JsonMatcher\Tests', __DIR__);

0 comments on commit 64dd127

Please sign in to comment.