|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* File containing the {@see Mailcode_Parser_Safeguard_Restorer} class. |
|
4
|
|
|
* |
|
5
|
|
|
* @package Mailcode |
|
6
|
|
|
* @subpackage Parser |
|
7
|
|
|
* @see Mailcode_Parser_Safeguard_Restorer |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Mailcode; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* The restorer is used to determine which strings the |
|
16
|
|
|
* safeguard's placeholders have to be made whole with, |
|
17
|
|
|
* depending on the specified settings. |
|
18
|
|
|
* |
|
19
|
|
|
* @package Mailcode |
|
20
|
|
|
* @subpackage Parser |
|
21
|
|
|
* @author Sebastian Mordziol <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
class Mailcode_Parser_Safeguard_Restorer |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var Mailcode_Parser_Safeguard |
|
27
|
|
|
*/ |
|
28
|
|
|
private $safeguard; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var bool |
|
32
|
|
|
*/ |
|
33
|
|
|
private $highlighted; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var bool |
|
37
|
|
|
*/ |
|
38
|
|
|
private $normalize; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var array<string,string> |
|
42
|
|
|
*/ |
|
43
|
|
|
private $replaces = array(); |
|
44
|
|
|
|
|
45
|
|
|
public function __construct(Mailcode_Parser_Safeguard $safeguard, bool $highlighted, bool $normalize) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->safeguard = $safeguard; |
|
48
|
|
|
$this->highlighted = $highlighted; |
|
49
|
|
|
$this->normalize = $normalize; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return array<string,string> |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getReplaces() : array |
|
56
|
|
|
{ |
|
57
|
|
|
$placeholders = $this->safeguard->getPlaceholders(); |
|
58
|
|
|
|
|
59
|
|
|
$this->replaces = array(); |
|
60
|
|
|
|
|
61
|
|
|
foreach($placeholders as $placeholder) |
|
62
|
|
|
{ |
|
63
|
|
|
$this->processPlaceholder($placeholder); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $this->replaces; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
private function processPlaceholder(Mailcode_Parser_Safeguard_Placeholder $placeholder) : void |
|
70
|
|
|
{ |
|
71
|
|
|
$replace = ''; |
|
72
|
|
|
$needle = $placeholder->getReplacementText(); |
|
73
|
|
|
|
|
74
|
|
|
if($this->highlighted) |
|
75
|
|
|
{ |
|
76
|
|
|
$replace = $this->processPlaceholder_highlighted($placeholder, $needle); |
|
77
|
|
|
} |
|
78
|
|
|
else if($this->normalize) |
|
79
|
|
|
{ |
|
80
|
|
|
$replace = $placeholder->getNormalizedText(); |
|
81
|
|
|
} |
|
82
|
|
|
else |
|
83
|
|
|
{ |
|
84
|
|
|
$replace = $placeholder->getOriginalText(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$this->replaces[$needle] = $replace; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
private function processPlaceholder_highlighted(Mailcode_Parser_Safeguard_Placeholder $placeholder, string $needle) : string |
|
91
|
|
|
{ |
|
92
|
|
|
$formatter = $this->safeguard->getFormatter(); |
|
93
|
|
|
|
|
94
|
|
|
if(!$formatter) |
|
|
|
|
|
|
95
|
|
|
{ |
|
96
|
|
|
return $placeholder->getHighlightedText(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$formattedNeedle = $formatter->getReplaceNeedle($placeholder); |
|
100
|
|
|
|
|
101
|
|
|
if($formattedNeedle !== $needle) |
|
102
|
|
|
{ |
|
103
|
|
|
$this->replaces[$formattedNeedle] = $placeholder->getHighlightedText(); |
|
104
|
|
|
|
|
105
|
|
|
return $placeholder->getNormalizedText(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $placeholder->getHighlightedText(); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|