PregReplaceTransformer::setReplacement()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2018 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Library\Core\Transformer;
13
14
use WBW\Library\Core\Transformer\API\TransformerInterface;
15
16
/**
17
 * preg_replace transformer.
18
 *
19
 * @author webeweb <https://github.com/webeweb/>
20
 * @package WBW\Library\Core\Transformer
21
 */
22
class PregReplaceTransformer implements TransformerInterface {
23
24
    /**
25
     * Pattern.
26
     *
27
     * @var string
28
     */
29
    private $pattern;
30
31
    /**
32
     * Replacement.
33
     *
34
     * @var string
35
     */
36
    private $replacement;
37
38
    /**
39
     * Constructor.
40
     *
41
     * @param string $pattern The pattern.
42
     * @param string $replacement The replacement.
43
     */
44
    public function __construct(string $pattern, string $replacement) {
45
        $this->setPattern($pattern);
46
        $this->setReplacement($replacement);
47
    }
48
49
    /**
50
     * Get the pattern.
51
     *
52
     * @return string Returns the pattern.
53
     */
54
    public function getPattern(): string {
55
        return $this->pattern;
56
    }
57
58
    /**
59
     * Get the replacement.
60
     *
61
     * @return string Returns the replacement.
62
     */
63
    public function getReplacement(): string {
64
        return $this->replacement;
65
    }
66
67
    /**
68
     * Set the pattern.
69
     *
70
     * @param string $pattern The pattern.
71
     * @return TransformerInterface Returns this preg_replace transformer.
72
     */
73
    public function setPattern(string $pattern): TransformerInterface {
74
        $this->pattern = $pattern;
75
        return $this;
76
    }
77
78
    /**
79
     * Set the replacement.
80
     *
81
     * @param string $replacement The replacement.
82
     * @return TransformerInterface Returns this preg_replace transformer.
83
     */
84
    public function setReplacement(string $replacement): TransformerInterface {
85
        $this->replacement = $replacement;
86
        return $this;
87
    }
88
89
    /**
90
     * {@inheritDoc}
91
     */
92
    public function transform($value) {
93
        if (null === $value || false === is_string($value)) {
94
            return null;
95
        }
96
        return preg_replace($this->getPattern(), $this->getReplacement(), $value);
97
    }
98
}
99