Skip to content

Commit cd3760e

Browse files
committed
Added unbounded array patterns
1 parent 4f9bf64 commit cd3760e

File tree

4 files changed

+51
-4
lines changed

4 files changed

+51
-4
lines changed

src/Coduo/PHPMatcher/Matcher/ArrayMatcher.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
class ArrayMatcher extends Matcher
99
{
10+
const UNBOUNDED_PATTERN = '@...@';
11+
1012
/**
1113
* @var PropertyMatcher
1214
*/
@@ -54,21 +56,33 @@ public function canMatch($pattern)
5456
*/
5557
private function iterateMatch(array $value, array $pattern, $parentPath = "")
5658
{
59+
$lastPattern = array_values($pattern);
60+
$unboundedMode = end($lastPattern) === self::UNBOUNDED_PATTERN;
61+
62+
if ($unboundedMode) {
63+
$unboundedPattern = prev($lastPattern);
64+
array_pop($pattern);
65+
}
66+
5767
foreach ($value as $key => $element) {
5868
$path = sprintf("[%s]", $key);
5969

60-
if (!$this->hasValue($pattern, $path)) {
70+
if ($this->hasValue($pattern, $path)) {
71+
$elementPattern = $this->getValue($pattern, $path);
72+
} else if ($unboundedMode) {
73+
$elementPattern = $unboundedPattern;
74+
} else {
6175
$this->error = sprintf('There is no element under path %s%s in pattern.', $parentPath, $path);
6276
return false;
6377
}
64-
$elementPattern = $this->getValue($pattern, $path);
78+
6579
if ($this->propertyMatcher->canMatch($elementPattern)) {
6680
if (true === $this->propertyMatcher->match($element, $elementPattern)) {
6781
continue;
6882
}
6983
}
7084

71-
if (!is_array($element)) {
85+
if (!is_array($element) || !is_array($elementPattern)) {
7286
$this->error = $this->propertyMatcher->getError();
7387
return false;
7488
}

src/Coduo/PHPMatcher/Matcher/JsonMatcher.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

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

1010
/**

tests/Coduo/PHPMatcher/Matcher/ArrayMatcherTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use Coduo\PHPMatcher\Matcher\ArrayMatcher;
55
use Coduo\PHPMatcher\Matcher\ChainMatcher;
66
use Coduo\PHPMatcher\Matcher\ScalarMatcher;
7+
use Coduo\PHPMatcher\Matcher\TypeMatcher;
78
use Coduo\PHPMatcher\Matcher\WildcardMatcher;
89

910
class ArrayMatcherTest extends \PHPUnit_Framework_TestCase
@@ -18,6 +19,7 @@ public function setUp()
1819
$this->matcher = new ArrayMatcher(
1920
new ChainMatcher(array(
2021
new ScalarMatcher(),
22+
new TypeMatcher(),
2123
new WildcardMatcher()
2224
))
2325
);
@@ -111,8 +113,23 @@ public static function positiveMatchData()
111113
6.66
112114
);
113115

116+
$simpleArrPattern = array(
117+
'users' => array(
118+
array(
119+
'firstName' => '@string@',
120+
'lastName' => '@string@'
121+
),
122+
'@...@'
123+
),
124+
true,
125+
false,
126+
1,
127+
6.66
128+
);
129+
114130
return array(
115131
array($simpleArr, $simpleArr),
132+
array($simpleArr, $simpleArrPattern),
116133
array(array(), array()),
117134
array(array('key' => 'val'), array('key' => 'val')),
118135
array(array(1), array(1)),

tests/Coduo/PHPMatcher/Matcher/JsonMatcherTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,14 @@ public static function positiveMatches()
125125
'{"users":["Norbert","Michał"]}',
126126
'{"users":["@string@","@string@"]}'
127127
),
128+
array(
129+
'{"users":["Norbert","Michał"]}',
130+
'{"users":["@string@","@...@"]}'
131+
),
132+
array(
133+
'{"users":["Norbert","Michał"]}',
134+
'{"users":["@string@",@...@]}'
135+
),
128136
array(
129137
'{"numbers":[1,2]}',
130138
'{"numbers":[@integer@, @integer@]}'
@@ -151,6 +159,14 @@ public static function negativeMatches()
151159
'{"users":["Norbert","Michał"]}',
152160
'{"users":["Michał","@string@"]}'
153161
),
162+
array(
163+
'{"users":["Norbert","Michał", "John"], "stuff": [1, 2, 3]}',
164+
'{"users":["@string@", @...@], "stuff": [1, 2]}'
165+
),
166+
array(
167+
'{"users":["Norbert","Michał", []]}',
168+
'{"users":["@string@", @...@]}'
169+
),
154170
array(
155171
'{this_is_not_valid_json',
156172
'{"users":["Michał","@string@"]}'

0 commit comments

Comments
 (0)