Skip to content

Commit dd2481f

Browse files
fix: pattern handling in Replace normalizer
1 parent 1466f13 commit dd2481f

2 files changed

Lines changed: 31 additions & 14 deletions

File tree

src/Decoders/ReplaceDecoder.php

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,22 @@ protected function decodeChain(array $tokens): array
2020
{
2121
$pattern = $this->config['pattern'] ?? null;
2222

23-
24-
return $pattern == null ?
25-
$tokens :
26-
array_map(function ($token) use ($pattern) {
27-
if (isset($pattern['Regex'])) {
28-
return preg_replace("/{$pattern['Regex']}/u", $this->config['content'], (string)$token);
29-
} elseif (isset($pattern['String'])) {
30-
return str_replace($pattern['String'], $this->config['content'], (string)$token);
31-
} else {
32-
return $token;
33-
}
34-
}, $tokens);
23+
if ($pattern === null) {
24+
return $tokens;
25+
}
26+
27+
$regex = $pattern['Regex'] ?? null;
28+
$string = $pattern['String'] ?? null;
29+
$replacement = $this->config['content'] ?? '';
30+
31+
return array_map(function ($token) use ($regex, $string, $replacement) {
32+
if ($regex !== null) {
33+
return preg_replace("/{$regex}/u", $replacement, (string)$token);
34+
}
35+
if ($string !== null) {
36+
return str_replace($string, $replacement, (string)$token);
37+
}
38+
return $token;
39+
}, $tokens);
3540
}
3641
}

src/Normalizers/Replace.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,20 @@ public function normalize(string $text): string
1515
{
1616
$pattern = $this->config['pattern'] ?? null;
1717

18-
if ($pattern != null) {
19-
$text = str_replace($pattern, $this->config['content'], $text);
18+
if ($pattern === null) {
19+
return $text;
20+
}
21+
22+
$regex = $pattern['Regex'] ?? null;
23+
$string = $pattern['String'] ?? null;
24+
$replacement = $this->config['content'] ?? '';
25+
26+
if ($regex !== null) {
27+
return preg_replace("/{$regex}/u", $replacement, $text);
28+
}
29+
30+
if ($string !== null) {
31+
return str_replace($string, $replacement, $text);
2032
}
2133

2234
return $text;

0 commit comments

Comments
 (0)