-
-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:defrag/JsonMatcher
* 'master' of github.com:defrag/JsonMatcher: Itroduce ArrayMatcher
- Loading branch information
Showing
5 changed files
with
113 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
vendor/ | ||
bin/ | ||
composer.lock | ||
.DS_Store | ||
.idea | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
] | ||
] | ||
])); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__); |