Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
7 / 7 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
PregReplace | |
100.00% |
7 / 7 |
|
100.00% |
2 / 2 |
4 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
filter | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | |
3 | namespace Verja\Filter; |
4 | |
5 | use Verja\Filter; |
6 | |
7 | class PregReplace extends Filter |
8 | { |
9 | /** @var string */ |
10 | protected $pattern; |
11 | |
12 | /** @var string */ |
13 | protected $replace; |
14 | |
15 | /** |
16 | * PregReplace constructor. |
17 | * |
18 | * @param string|array $pattern |
19 | * @param string|callable $replace |
20 | */ |
21 | public function __construct($pattern, $replace) |
22 | { |
23 | $this->pattern = $pattern; |
24 | $this->replace = $replace; |
25 | } |
26 | |
27 | /** |
28 | * Filter $value |
29 | * |
30 | * @param mixed $value |
31 | * @param array $context |
32 | * @return mixed |
33 | */ |
34 | public function filter($value, array $context = []) |
35 | { |
36 | if (!is_string($value)) { |
37 | return $value; |
38 | } |
39 | |
40 | if (is_callable($this->replace)) { |
41 | return preg_replace_callback($this->pattern, $this->replace, $value); |
42 | } |
43 | |
44 | return preg_replace($this->pattern, $this->replace, $value); |
45 | } |
46 | } |