Skip to content

Added unbounded array patterns #17

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 1 commit into from
Jun 19, 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
20 changes: 17 additions & 3 deletions src/Coduo/PHPMatcher/Matcher/ArrayMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

class ArrayMatcher extends Matcher
{
const UNBOUNDED_PATTERN = '@...@';

/**
* @var PropertyMatcher
*/
Expand Down Expand Up @@ -54,21 +56,33 @@ public function canMatch($pattern)
*/
private function iterateMatch(array $value, array $pattern, $parentPath = "")
{
$lastPattern = array_values($pattern);
$unboundedMode = end($lastPattern) === self::UNBOUNDED_PATTERN;

if ($unboundedMode) {
$unboundedPattern = prev($lastPattern);
array_pop($pattern);
}

foreach ($value as $key => $element) {
$path = sprintf("[%s]", $key);

if (!$this->hasValue($pattern, $path)) {
if ($this->hasValue($pattern, $path)) {
$elementPattern = $this->getValue($pattern, $path);
} else if ($unboundedMode) {
$elementPattern = $unboundedPattern;
} else {
$this->error = sprintf('There is no element under path %s%s in pattern.', $parentPath, $path);
return false;
}
$elementPattern = $this->getValue($pattern, $path);

if ($this->propertyMatcher->canMatch($elementPattern)) {
if (true === $this->propertyMatcher->match($element, $elementPattern)) {
continue;
}
}

if (!is_array($element)) {
if (!is_array($element) || !is_array($elementPattern)) {
$this->error = $this->propertyMatcher->getError();
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Coduo/PHPMatcher/Matcher/JsonMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class JsonMatcher extends Matcher
{
const TRANSFORM_QUOTATION_PATTERN = '/([^"])@(integer|string|array|double|wildcard|boolean|null)@([^"])/';
const TRANSFORM_QUOTATION_PATTERN = '/([^"])@(integer|string|array|double|wildcard|boolean|\.\.\.|null)@([^"])/';
const TRANSFORM_QUOTATION_REPLACEMENT = '$1"@$2@"$3';

/**
Expand Down
17 changes: 17 additions & 0 deletions tests/Coduo/PHPMatcher/Matcher/ArrayMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Coduo\PHPMatcher\Matcher\ArrayMatcher;
use Coduo\PHPMatcher\Matcher\ChainMatcher;
use Coduo\PHPMatcher\Matcher\ScalarMatcher;
use Coduo\PHPMatcher\Matcher\TypeMatcher;
use Coduo\PHPMatcher\Matcher\WildcardMatcher;

class ArrayMatcherTest extends \PHPUnit_Framework_TestCase
Expand All @@ -18,6 +19,7 @@ public function setUp()
$this->matcher = new ArrayMatcher(
new ChainMatcher(array(
new ScalarMatcher(),
new TypeMatcher(),
new WildcardMatcher()
))
);
Expand Down Expand Up @@ -111,8 +113,23 @@ public static function positiveMatchData()
6.66
);

$simpleArrPattern = array(
'users' => array(
array(
'firstName' => '@string@',
'lastName' => '@string@'
),
'@...@'
),
true,
false,
1,
6.66
);

return array(
array($simpleArr, $simpleArr),
array($simpleArr, $simpleArrPattern),
array(array(), array()),
array(array('key' => 'val'), array('key' => 'val')),
array(array(1), array(1)),
Expand Down
16 changes: 16 additions & 0 deletions tests/Coduo/PHPMatcher/Matcher/JsonMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ public static function positiveMatches()
'{"users":["Norbert","Michał"]}',
'{"users":["@string@","@string@"]}'
),
array(
'{"users":["Norbert","Michał"]}',
'{"users":["@string@","@...@"]}'
),
array(
'{"users":["Norbert","Michał"]}',
'{"users":["@string@",@...@]}'
),
array(
'{"numbers":[1,2]}',
'{"numbers":[@integer@, @integer@]}'
Expand All @@ -151,6 +159,14 @@ public static function negativeMatches()
'{"users":["Norbert","Michał"]}',
'{"users":["Michał","@string@"]}'
),
array(
'{"users":["Norbert","Michał", "John"], "stuff": [1, 2, 3]}',
'{"users":["@string@", @...@], "stuff": [1, 2]}'
),
array(
'{"users":["Norbert","Michał", []]}',
'{"users":["@string@", @...@]}'
),
array(
'{this_is_not_valid_json',
'{"users":["Michał","@string@"]}'
Expand Down