1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File containing the {@see Mailcode_Parser_Safeguard_Formatter} class. |
4
|
|
|
* |
5
|
|
|
* @package Mailcode |
6
|
|
|
* @subpackage Parser |
7
|
|
|
* @see Mailcode_Parser_Safeguard_Formatter |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Mailcode; |
13
|
|
|
|
14
|
|
|
use AppUtils\ConvertHelper; |
15
|
|
|
use function AppUtils\parseVariable; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Abstract base class for safeguard formatters: these |
19
|
|
|
* are used to apply diverse formattings to the string |
20
|
|
|
* being parsed. |
21
|
|
|
* |
22
|
|
|
* @package Mailcode |
23
|
|
|
* @subpackage Parser |
24
|
|
|
* @author Sebastian Mordziol <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
abstract class Mailcode_Parser_Safeguard_Formatter |
27
|
|
|
{ |
28
|
|
|
const ERROR_INVALID_LOCATION_INSTANCE = 65601; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Mailcode_Parser_Safeguard_Formatting |
32
|
|
|
*/ |
33
|
|
|
protected $formatting; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var Mailcode_StringContainer |
37
|
|
|
*/ |
38
|
|
|
protected $subject; |
39
|
|
|
|
40
|
|
|
public function __construct(Mailcode_Parser_Safeguard_Formatting $formatting) |
41
|
|
|
{ |
42
|
|
|
$this->formatting = $formatting; |
43
|
|
|
$this->subject = $formatting->getSubject(); |
44
|
|
|
|
45
|
|
|
$this->initFormatting(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function getID() : string |
49
|
|
|
{ |
50
|
|
|
$tokens = explode('_', get_class($this)); |
51
|
|
|
|
52
|
|
|
return array_pop($tokens); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function getSubject() : Mailcode_StringContainer |
56
|
|
|
{ |
57
|
|
|
return $this->subject; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getSafeguard() : Mailcode_Parser_Safeguard |
61
|
|
|
{ |
62
|
|
|
return $this->formatting->getSafeguard(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
abstract public function getPriority() : int; |
66
|
|
|
|
67
|
|
|
abstract protected function initFormatting() : void; |
68
|
|
|
|
69
|
|
|
protected function createLocation(Mailcode_Parser_Safeguard_Placeholder $placeholder) : Mailcode_Parser_Safeguard_Formatter_Location |
70
|
|
|
{ |
71
|
|
|
$class = sprintf('Mailcode\Mailcode_Parser_Safeguard_Formatter_Type_%s_Location', $this->getID()); |
72
|
|
|
|
73
|
|
|
$instance = new $class($this, $placeholder); |
74
|
|
|
|
75
|
|
|
if($instance instanceof Mailcode_Parser_Safeguard_Formatter_Location) |
76
|
|
|
{ |
77
|
|
|
return $instance; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
throw new Mailcode_Exception( |
81
|
|
|
'Invalid location instance created.', |
82
|
|
|
sprintf( |
83
|
|
|
'Expected a class of type [%s], got [%s].', |
84
|
|
|
Mailcode_Parser_Safeguard_Formatter_Location::class, |
85
|
|
|
parseVariable($instance)->enableType()->toString() |
86
|
|
|
), |
87
|
|
|
self::ERROR_INVALID_LOCATION_INSTANCE |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Retrieves all formatter-specific placeholder locations |
93
|
|
|
* in the subject string. |
94
|
|
|
* |
95
|
|
|
* @return Mailcode_Parser_Safeguard_Formatter_Location[] |
96
|
|
|
*/ |
97
|
|
|
protected function resolveLocations() : array |
98
|
|
|
{ |
99
|
|
|
$placeholders = $this->formatting->getSafeguard()->getPlaceholders(); |
100
|
|
|
|
101
|
|
|
$result = array(); |
102
|
|
|
|
103
|
|
|
foreach($placeholders as $placeholder) |
104
|
|
|
{ |
105
|
|
|
$result[] = $this->createLocation($placeholder); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $result; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Resolves the newline character used in the string. |
113
|
|
|
* |
114
|
|
|
* @param string $subject |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
protected function resolveNewlineChar(string $subject) : string |
118
|
|
|
{ |
119
|
|
|
$eol = ConvertHelper::detectEOLCharacter($subject); |
120
|
|
|
|
121
|
|
|
if($eol) |
122
|
|
|
{ |
123
|
|
|
return $eol->getCharacter(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return PHP_EOL; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|