Skip to content

Commit 5ff1924

Browse files
JarJaknorberttech
authored andcommitted
Fix Repeat expander in JSON Context (#151)
* Fix Repeat expander in JSON Context Fixes #150 * Add repeat expanders tests * Fix repeat test * Too bad one can't use array universal key inside repeat expander * Fix JSON in tests
1 parent 201abf8 commit 5ff1924

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

src/Matcher/Pattern/Expander/Repeat.php

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,25 @@ public static function is(string $name) : bool
2626
return self::NAME === $name;
2727
}
2828

29-
public function __construct(string $pattern, bool $isStrict = true)
29+
/**
30+
* @param array|string $pattern array to be matched or json-encoded string
31+
*/
32+
public function __construct($pattern, bool $isStrict = true)
3033
{
31-
if (!\is_string($pattern)) {
32-
throw new \InvalidArgumentException('Repeat pattern must be a string.');
33-
}
34-
3534
$this->pattern = $pattern;
3635
$this->isStrict = $isStrict;
3736
$this->isScalar = true;
3837

39-
$json = \json_decode($pattern, true);
40-
41-
if ($json !== null && \json_last_error() === JSON_ERROR_NONE) {
42-
$this->pattern = $json;
38+
if (\is_string($pattern)) {
39+
$json = \json_decode($pattern, true);
40+
if ($json !== null && \json_last_error() === JSON_ERROR_NONE) {
41+
$this->pattern = $json;
42+
$this->isScalar = false;
43+
}
44+
} elseif (\is_array($pattern)) {
4345
$this->isScalar = false;
46+
} else {
47+
throw new \InvalidArgumentException('Repeat pattern must be a string or an array.');
4448
}
4549
}
4650

@@ -80,11 +84,6 @@ private function matchScalar(array $values, Matcher $matcher) : bool
8084
return true;
8185
}
8286

83-
/**
84-
* @param array $values
85-
* @param Matcher $matcher
86-
* @return bool
87-
*/
8887
private function matchJson(array $values, Matcher $matcher) : bool
8988
{
9089
$patternKeys = \array_keys($this->pattern);

tests/Matcher/JsonMatcherTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ public static function negativePatterns()
150150
return [
151151
['@string@'],
152152
['["Norbert", '],
153+
['@[email protected]({"name": "@string@", "value": "@array@"})']
153154
];
154155
}
155156

@@ -215,6 +216,10 @@ public static function positiveMatches()
215216
[
216217
'[{"name": "Norbert","lastName":"Orzechowicz"},{"name":"Michał"},{"name":"Bob"},{"name":"Martin"}]',
217218
'[{"name": "Norbert","@*@":"@*@"},@...@]'
219+
],
220+
[
221+
'[{"name": "Norbert"},{"name":"Michał"},{"name":"Bob"},{"name":"Martin"}]',
222+
'"@[email protected]({\"name\": \"@string@\"})"'
218223
]
219224
];
220225
}
@@ -242,6 +247,10 @@ public static function negativeMatches()
242247
'{"foo":"foo val","bar":"bar val"}',
243248
'{"foo":"foo val"}'
244249
],
250+
[
251+
'[{"name": "Norbert","lastName":"Orzechowicz"},{"name":"Michał"},{"name":"Bob"},{"name":"Martin"}]',
252+
'"@[email protected]({\"name\": \"@string@\"})"'
253+
],
245254
[
246255
[],
247256
'[]'

0 commit comments

Comments
 (0)