Completed
Push — master ( ca82a9...888d00 )
by WEBEWEB
01:51
created

PregReplaceScrapingCleaner::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 2
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\Scraping\Cleaner;
13
14
/**
15
 * preg_replace scraping cleaner.
16
 *
17
 * @author webeweb <https://github.com/webeweb/>
18
 * @package WBW\Library\Core\Scraping\Cleaner
19
 */
20
class PregReplaceScrapingCleaner {
21
22
    /**
23
     * Pattern.
24
     *
25
     * @var string
26
     */
27
    private $pattern;
28
29
    /**
30
     * Replacement.
31
     *
32
     * @var string
33
     */
34
    private $replacement;
35
36
    /**
37
     * Constructor.
38
     *
39
     * @param string $pattern The pattern.
40
     * @param string $replacement The replacement.
41
     */
42
    public function __construct($pattern, $replacement) {
43
        $this->setPattern($pattern);
44
        $this->setReplacement($replacement);
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function clean($value) {
51
        if (null === $value || false === is_string($value)) {
52
            return null;
53
        }
54
        return preg_replace($this->getPattern(), $this->getReplacement(), $value);
55
    }
56
57
    /**
58
     * Get the pattern.
59
     *
60
     * @return string Returns the pattern.
61
     */
62
    public function getPattern() {
63
        return $this->pattern;
64
    }
65
66
    /**
67
     * Get the replacement.
68
     *
69
     * @return string Returns the replacement.
70
     */
71
    public function getReplacement() {
72
        return $this->replacement;
73
    }
74
75
    /**
76
     * Set the pattern.
77
     *
78
     * @param string $pattern The pattern.
79
     * @return SrapingPregReplaceCleaner Returns this preg_replace cleaner.
80
     */
81
    protected function setPattern($pattern) {
82
        $this->pattern = $pattern;
83
        return $this;
84
    }
85
86
    /**
87
     * Set the replacement.
88
     *
89
     * @param string $replacement The replacement.
90
     * @return SrapingPregReplaceCleaner Returns this preg_replace cleaner.
91
     */
92
    protected function setReplacement($replacement) {
93
        $this->replacement = $replacement;
94
        return $this;
95
    }
96
97
}
98