Skip to content

Commit 8c52bff

Browse files
authored
Added possibility to use new lines in json patterns (#169)
1 parent d3c031a commit 8c52bff

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

Diff for: src/Matcher/Pattern/Assert/Json.php

+12-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66

77
final class Json
88
{
9-
const TRANSFORM_QUOTATION_PATTERN = '/([^"])@([a-zA-Z0-9\.]+)@([^"])/';
10-
const TRANSFORM_QUOTATION_REPLACEMENT = '$1"@$2@"$3';
9+
private const TRANSFORM_NEW_LINES = '/\r?\n|\r/';
10+
private const TRANSFORM_QUOTATION_PATTERN = '/([^"])@([a-zA-Z0-9\.]+)@([^"])/';
11+
private const TRANSFORM_QUOTATION_REPLACEMENT = '$1"@$2@"$3';
1112

1213
public static function isValid($value) : bool
1314
{
@@ -33,6 +34,14 @@ public static function isValidPattern($value) : bool
3334

3435
public static function transformPattern(string $pattern) : string
3536
{
36-
return \preg_replace(self::TRANSFORM_QUOTATION_PATTERN, self::TRANSFORM_QUOTATION_REPLACEMENT, $pattern);
37+
return \preg_replace(
38+
self::TRANSFORM_NEW_LINES,
39+
'',
40+
\preg_replace(
41+
self::TRANSFORM_QUOTATION_PATTERN,
42+
self::TRANSFORM_QUOTATION_REPLACEMENT,
43+
$pattern
44+
)
45+
);
3746
}
3847
}

Diff for: tests/Matcher/JsonMatcherTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,15 @@ public static function positiveMatches()
186186
'{"foobar":[1.22, 2, "hello"]}',
187187
'{"foobar":[@double@, @integer@, @string@]}'
188188
],
189+
[
190+
'{"entries":[{"id":1,"question":"test","answer":"test","os":"test"}]}',
191+
'{"entries": "@array@.repeat({
192+
\"id\": \"@number@\",
193+
\"question\": \"@string@\",
194+
\"answer\": \"@string@\",
195+
\"os\": \"@*@\"
196+
})"}'
197+
],
189198
[
190199
'{"null":[null]}',
191200
'{"null":[@null@]}'

0 commit comments

Comments
 (0)