-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathReplaceDecoder.php
36 lines (28 loc) · 920 Bytes
/
ReplaceDecoder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
declare(strict_types=1);
namespace Codewithkyrian\Transformers\Decoders;
class ReplaceDecoder extends Decoder
{
/**
* @param array $config
*/
public function __construct(array $config)
{
parent::__construct($config);
}
protected function decodeChain(array $tokens): array
{
$pattern = $this->config['pattern'] ?? null;
return $pattern == null ?
$tokens :
array_map(function ($token) use ($pattern) {
if (isset($pattern['Regex'])) {
return preg_replace("/{$pattern['Regex']}/u", $this->config['content'], (string)$token);
} elseif (isset($pattern['String'])) {
return str_replace($pattern['String'], $this->config['content'], (string)$token);
} else {
return $token;
}
}, $tokens);
}
}