1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Mailcode; |
6
|
|
|
|
7
|
|
|
use function AppUtils\sb; |
8
|
|
|
|
9
|
|
|
class Mailcode_PreParser_CommandDef |
10
|
|
|
{ |
11
|
|
|
private string $name; |
12
|
|
|
private string $openingText; |
13
|
|
|
private string $params; |
14
|
|
|
private string $closingText; |
15
|
|
|
private int $contentID = 0; |
16
|
|
|
private int $length = 0; |
17
|
|
|
private int $startPos = 0; |
18
|
|
|
|
19
|
|
|
public function __construct(string $name, string $openingText, string $params, string $closingText) |
20
|
|
|
{ |
21
|
|
|
$this->name = $name; |
22
|
|
|
$this->openingText = $openingText; |
23
|
|
|
$this->params = $params; |
24
|
|
|
$this->closingText = $closingText; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return string |
29
|
|
|
*/ |
30
|
|
|
public function getName() : string |
31
|
|
|
{ |
32
|
|
|
return $this->name; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return string |
37
|
|
|
*/ |
38
|
|
|
public function getOpeningText() : string |
39
|
|
|
{ |
40
|
|
|
return $this->openingText; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
|
|
public function getParams() : string |
47
|
|
|
{ |
48
|
|
|
return $this->params; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
|
|
public function getClosingText() : string |
55
|
|
|
{ |
56
|
|
|
return $this->closingText; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param int $id |
61
|
|
|
* @return $this |
62
|
|
|
*/ |
63
|
|
|
public function setContentID(int $id) : self |
64
|
|
|
{ |
65
|
|
|
$this->contentID = $id; |
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return int |
71
|
|
|
*/ |
72
|
|
|
public function getContentID() : int |
73
|
|
|
{ |
74
|
|
|
return $this->contentID; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return int |
79
|
|
|
*/ |
80
|
|
|
public function getLength() : int |
81
|
|
|
{ |
82
|
|
|
return $this->length; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return int |
87
|
|
|
*/ |
88
|
|
|
public function getStartPos() : int |
89
|
|
|
{ |
90
|
|
|
return $this->startPos; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function getReplacementCommand() : string |
94
|
|
|
{ |
95
|
|
|
return sprintf( |
96
|
|
|
'{%s: %s %s}', |
97
|
|
|
$this->getName(), |
98
|
|
|
$this->getContentID(), |
99
|
|
|
$this->getParams() |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function getContent() : string |
104
|
|
|
{ |
105
|
|
|
return Mailcode_Parser_PreParser::getContent($this->getContentID()); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function extractContent(string $subject) : void |
109
|
|
|
{ |
110
|
|
|
$this->startPos = (int)strpos($subject, $this->openingText); |
111
|
|
|
$posContent = $this->startPos + strlen($this->openingText); |
112
|
|
|
$posEnd = (int)strpos($subject, $this->closingText); |
113
|
|
|
$this->length = ($posEnd + strlen($this->closingText)) - $this->startPos; |
114
|
|
|
|
115
|
|
|
// Extract the content, and store it |
116
|
|
|
$content = substr($subject, $posContent, $posEnd - $posContent); |
117
|
|
|
$this->contentID = Mailcode_Parser_PreParser::storeContent($content); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return array{openingCommand:string,params:string,closingCommand:string,content:string,contentID:int,replacementCommand:string,startPosition:int,length:int} |
122
|
|
|
*/ |
123
|
|
|
public function toArray() : array |
124
|
|
|
{ |
125
|
|
|
return array( |
126
|
|
|
'openingCommand' => $this->getOpeningText(), |
127
|
|
|
'params' => $this->getParams(), |
128
|
|
|
'closingCommand' => $this->getClosingText(), |
129
|
|
|
'content' => $this->getContent(), |
130
|
|
|
'contentID' => $this->getContentID(), |
131
|
|
|
'replacementCommand' => $this->getReplacementCommand(), |
132
|
|
|
'startPosition' => $this->getStartPos(), |
133
|
|
|
'length' => $this->getLength() |
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function validateContent(Mailcode_Parser_PreParser $parser, Mailcode_Collection $collection) : bool |
138
|
|
|
{ |
139
|
|
|
$commands = $parser->getCommands(); |
140
|
|
|
$content = $this->getContent(); |
141
|
|
|
|
142
|
|
|
foreach($commands as $command) |
143
|
|
|
{ |
144
|
|
|
$string = $command->getOpeningText(); |
145
|
|
|
|
146
|
|
|
if(strpos($content, $string) !== false) |
147
|
|
|
{ |
148
|
|
|
$this->addEscapeError($command, $collection); |
149
|
|
|
return false; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return true; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
private function addEscapeError(Mailcode_PreParser_CommandDef $command, Mailcode_Collection $collection) : void |
157
|
|
|
{ |
158
|
|
|
$collection->addErrorMessage( |
159
|
|
|
$command->getOpeningText(), |
160
|
|
|
(string)sb() |
161
|
|
|
->t('Command nesting error:') |
162
|
|
|
->t( |
163
|
|
|
'The %1$s command contains a nested %2$s command.', |
164
|
|
|
$this->getName(), |
165
|
|
|
$command->getName() |
166
|
|
|
) |
167
|
|
|
->t('Nesting commands that contain text within each other is allowed, but they must be escaped.') |
168
|
|
|
->t('To solve this issue, please escape the nested command\'s brackets.') |
169
|
|
|
->t('For example:') |
170
|
|
|
->sf('\{%s\}', $command->getName()), |
171
|
|
|
Mailcode_Commands_CommonConstants::VALIDATION_UNESCAPED_NESTED_COMMAND |
172
|
|
|
); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|