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
|
|
|
* str_replace transformer. |
18
|
|
|
* |
19
|
|
|
* @author webeweb <https://github.com/webeweb/> |
20
|
|
|
* @package WBW\Library\Core\Transformer |
21
|
|
|
*/ |
22
|
|
|
class StrReplaceTransformer implements TransformerInterface { |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Replaces. |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
private $replaces; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Searches. |
33
|
|
|
* |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
private $searches; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Constructor. |
40
|
|
|
* |
41
|
|
|
* @param array $searches The searches. |
42
|
|
|
* @param array $replaces The replaces. |
43
|
|
|
*/ |
44
|
|
|
public function __construct(array $searches, array $replaces) { |
45
|
|
|
$this->setReplaces($replaces); |
46
|
|
|
$this->setSearches($searches); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get the replaces. |
51
|
|
|
* |
52
|
|
|
* @return array Returns the replaces. |
53
|
|
|
*/ |
54
|
|
|
public function getReplaces(): array { |
55
|
|
|
return $this->replaces; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get the searches. |
60
|
|
|
* |
61
|
|
|
* @return array Returns the searches. |
62
|
|
|
*/ |
63
|
|
|
public function getSearches(): array { |
64
|
|
|
return $this->searches; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Set the replaces. |
69
|
|
|
* |
70
|
|
|
* @param array $replaces The replaces. |
71
|
|
|
* @return TransformerInterface Returns this str_replace transformer. |
72
|
|
|
*/ |
73
|
|
|
public function setReplaces(array $replaces): TransformerInterface { |
74
|
|
|
$this->replaces = $replaces; |
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Set the searches. |
80
|
|
|
* |
81
|
|
|
* @param array $searches The searches. |
82
|
|
|
* @return TransformerInterface Returns this str_replace transformer. |
83
|
|
|
*/ |
84
|
|
|
public function setSearches(array $searches): TransformerInterface { |
85
|
|
|
$this->searches = $searches; |
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 str_replace($this->getSearches(), $this->getReplaces(), $value); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|