Skip to content

Commit f2aef27

Browse files
authored
Fix Repeat expander in JSON Context
Fixes coduo#150
1 parent b9b53e4 commit f2aef27

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-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);

0 commit comments

Comments
 (0)