Skip to content

Commit a31a2db

Browse files
committed
Added support for omitting parentheses in Serializer
1 parent 5d7c60b commit a31a2db

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

src/Serializer.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ public function __construct(OutputInterface $output, MetaCharacters $meta, Escap
4444
* Serialize given strings into a regular expression
4545
*
4646
* @param array[] $strings
47+
* @param bool $groupAlternations Whether alternations should be parenthesized into a group
4748
* @return string
4849
*/
49-
public function serializeStrings(array $strings): string
50+
public function serializeStrings(array $strings, bool $groupAlternations = true): string
5051
{
5152
$info = $this->analyzeStrings($strings);
5253
$alternations = array_map([$this, 'serializeString'], $info['strings']);
@@ -57,7 +58,7 @@ public function serializeStrings(array $strings): string
5758
}
5859

5960
$expr = implode('|', $alternations);
60-
if ($this->needsParentheses($info))
61+
if ($this->needsParentheses($info, $groupAlternations))
6162
{
6263
$expr = '(?:' . $expr . ')';
6364
}
@@ -189,11 +190,13 @@ protected function isSingleQuantifiableString(array $strings): bool
189190
* Test whether an expression needs parentheses based on the strings info
190191
*
191192
* @param array $info
193+
* @param bool $groupAlternations Whether alternations should be parenthesized into a group
192194
* @return bool
193195
*/
194-
protected function needsParentheses(array $info): bool
196+
protected function needsParentheses(array $info, bool $groupAlternations): bool
195197
{
196-
return ($info['alternationsCount'] > 1 || ($info['quantifier'] && !$this->isQuantifiable($info)));
198+
return (($groupAlternations && $info['alternationsCount'] > 1)
199+
|| ($info['quantifier'] && !$this->isQuantifiable($info)));
197200
}
198201

199202
/**

tests/SerializerTest.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,16 @@ class SerializerTest extends TestCase
1717
/**
1818
* @dataProvider getSerializerTests
1919
*/
20-
public function test($original, $expected)
20+
public function test($original, $expected, bool $groupAlternations = null)
2121
{
22+
$args = [$original];
23+
if (isset($groupAlternations))
24+
{
25+
$args[] = $groupAlternations;
26+
}
27+
2228
$serializer = new Serializer(new Output, new MetaCharacters(new Input), new Escaper);
23-
$this->assertSame($expected, $serializer->serializeStrings($original, false));
29+
$this->assertSame($expected, $serializer->serializeStrings(...$args));
2430
}
2531

2632
public function getSerializerTests()
@@ -130,6 +136,31 @@ public function getSerializerTests()
130136
],
131137
'[a-e]'
132138
],
139+
[
140+
[
141+
[97, 97],
142+
[98, 98]
143+
],
144+
'(?:aa|bb)',
145+
true
146+
],
147+
[
148+
[
149+
[97, 97],
150+
[98, 98]
151+
],
152+
'aa|bb',
153+
false
154+
],
155+
[
156+
[
157+
[],
158+
[97, 97],
159+
[98, 98]
160+
],
161+
'(?:aa|bb)?',
162+
false
163+
],
133164
];
134165
}
135166
}

0 commit comments

Comments
 (0)