1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File containing the {@see Mailcode_Commands_Command} class. |
4
|
|
|
* |
5
|
|
|
* @package Mailcode |
6
|
|
|
* @subpackage Commands |
7
|
|
|
* @see Mailcode_Commands_Command |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Mailcode; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Base command class with the common functionality for all commands. |
16
|
|
|
* |
17
|
|
|
* @package Mailcode |
18
|
|
|
* @subpackage Commands |
19
|
|
|
* @author Sebastian Mordziol <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
abstract class Mailcode_Commands_Command |
22
|
|
|
{ |
23
|
|
|
const ERROR_NON_DUMMY_OPERATION = 46001; |
24
|
|
|
|
25
|
|
|
const ERROR_NO_VALIDATION_RESULT_AVAILABLE = 46002; |
26
|
|
|
|
27
|
|
|
const VALIDATION_MISSING_PARAMETERS = 1; |
28
|
|
|
|
29
|
|
|
const VALIDATION_ADDONS_NOT_SUPPORTED = 2; |
30
|
|
|
|
31
|
|
|
const VALIDATION_ADDON_NOT_SUPPORTED = 3; |
32
|
|
|
|
33
|
|
|
const VALIDATION_UNKNOWN_COMMAND_NAME = 4; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $type = ''; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $paramsString = ''; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
protected $matchedText = ''; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
protected $hash = ''; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var \AppUtils\OperationResult |
57
|
|
|
*/ |
58
|
|
|
protected $validationResult = null; |
59
|
|
|
|
60
|
|
|
public function __construct(string $type='', string $paramsString='', string $matchedText='') |
61
|
|
|
{ |
62
|
|
|
$this->type = $type; |
63
|
|
|
$this->paramsString = $paramsString; |
64
|
|
|
$this->matchedText = $matchedText; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return string The ID of the command = the name of the command class file. |
69
|
|
|
*/ |
70
|
|
|
public function getID() : string |
71
|
|
|
{ |
72
|
|
|
$tokens = explode('_', get_class($this)); |
73
|
|
|
return array_pop($tokens); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Checks whether this is a dummy command, which is only |
78
|
|
|
* used to access information on the command type. It cannot |
79
|
|
|
* be used as an actual live command. |
80
|
|
|
* |
81
|
|
|
* @return bool |
82
|
|
|
*/ |
83
|
|
|
public function isDummy() : bool |
84
|
|
|
{ |
85
|
|
|
return $this->type === '__dummy'; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Retrieves a hash of the actual matched command string, |
90
|
|
|
* which is used in collections to detect duplicate commands. |
91
|
|
|
* |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
|
public function getHash() : string |
95
|
|
|
{ |
96
|
|
|
$this->requireNonDummy(); |
97
|
|
|
|
98
|
|
|
if($this->hash === '') { |
99
|
|
|
$this->hash = md5($this->matchedText); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $this->hash; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
protected function requireNonDummy() : void |
106
|
|
|
{ |
107
|
|
|
if(!$this->isDummy()) |
108
|
|
|
{ |
109
|
|
|
return; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
throw new Mailcode_Exception( |
113
|
|
|
'Operation not allowed with dummy commands', |
114
|
|
|
null, |
115
|
|
|
self::ERROR_NON_DUMMY_OPERATION |
116
|
|
|
); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function isValid() : bool |
120
|
|
|
{ |
121
|
|
|
return $this->validate()->isValid(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
protected function validate() : \AppUtils\OperationResult |
125
|
|
|
{ |
126
|
|
|
$this->requireNonDummy(); |
127
|
|
|
|
128
|
|
|
if(isset($this->validationResult)) |
129
|
|
|
{ |
130
|
|
|
return $this->validationResult; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$this->validationResult = new \AppUtils\OperationResult($this); |
134
|
|
|
|
135
|
|
|
$this->validateSyntax(); |
136
|
|
|
|
137
|
|
|
$this->_validate(); |
138
|
|
|
|
139
|
|
|
return $this->validationResult; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function getValidationResult() : \AppUtils\OperationResult |
143
|
|
|
{ |
144
|
|
|
if(isset($this->validationResult)) |
145
|
|
|
{ |
146
|
|
|
return $this->validationResult; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
throw new Mailcode_Exception( |
150
|
|
|
'No validation result available', |
151
|
|
|
'The command has no validation error, the validation result cannot be accessed.', |
152
|
|
|
self::ERROR_NO_VALIDATION_RESULT_AVAILABLE |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
protected function validateSyntax() |
157
|
|
|
{ |
158
|
|
|
if($this->requiresParameters() && empty($this->paramsString)) |
159
|
|
|
{ |
160
|
|
|
$this->validationResult->makeError( |
161
|
|
|
t('Parameters have to be specified.'), |
162
|
|
|
self::VALIDATION_MISSING_PARAMETERS |
163
|
|
|
); |
164
|
|
|
return; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
if($this->supportsType() && !empty($this->type)) |
168
|
|
|
{ |
169
|
|
|
$types = $this->getSupportedTypes(); |
170
|
|
|
|
171
|
|
|
if(!in_array($this->type, $types)) |
172
|
|
|
{ |
173
|
|
|
$this->validationResult->makeError( |
174
|
|
|
t('The command addon %1$s is not supported.', $this->type).' '. |
175
|
|
|
t('Valid addons are %1$s.', implode(', ', $types)), |
176
|
|
|
self::VALIDATION_ADDON_NOT_SUPPORTED |
177
|
|
|
); |
178
|
|
|
|
179
|
|
|
return; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
if(!$this->supportsType() && !empty($this->type)) |
184
|
|
|
{ |
185
|
|
|
$this->validationResult->makeError( |
186
|
|
|
t('Command addons are not supported (the %1$s part).', $this->type), |
187
|
|
|
self::VALIDATION_ADDONS_NOT_SUPPORTED |
188
|
|
|
); |
189
|
|
|
|
190
|
|
|
return; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
public function hasType() : bool |
195
|
|
|
{ |
196
|
|
|
return $this->supportsType() && !empty($this->type); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function getType() : string |
200
|
|
|
{ |
201
|
|
|
if($this->supportsType()) |
202
|
|
|
{ |
203
|
|
|
return $this->type; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
return ''; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
public function hasParameters() : bool |
210
|
|
|
{ |
211
|
|
|
return $this->requiresParameters() && !empty($this->paramsString); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
public function getMatchedText() : string |
215
|
|
|
{ |
216
|
|
|
return $this->matchedText; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
public function getHighlighted() : string |
220
|
|
|
{ |
221
|
|
|
$highlighter = new Mailcode_Commands_Highlighter($this); |
222
|
|
|
return $highlighter->highlight(); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
public function getParamsString() : string |
226
|
|
|
{ |
227
|
|
|
if($this->requiresParameters()) |
228
|
|
|
{ |
229
|
|
|
return $this->paramsString; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
return ''; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
abstract protected function _validate() : void; |
236
|
|
|
|
237
|
|
|
abstract public function getName() : string; |
238
|
|
|
|
239
|
|
|
abstract public function getLabel() : string; |
240
|
|
|
|
241
|
|
|
abstract public function requiresParameters() : bool; |
242
|
|
|
|
243
|
|
|
abstract public function supportsType() : bool; |
244
|
|
|
|
245
|
|
|
abstract public function generatesContent() : bool; |
246
|
|
|
|
247
|
|
|
public function getSupportedTypes() : array |
248
|
|
|
{ |
249
|
|
|
return array(); |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|