|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* File containing the {@see Mailcode_Factory} class. |
|
4
|
|
|
* |
|
5
|
|
|
* @package Mailcode |
|
6
|
|
|
* @subpackage Core |
|
7
|
|
|
* @see Mailcode_Factory |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Mailcode; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Factory utility used to create commands. |
|
16
|
|
|
* |
|
17
|
|
|
* @package Mailcode |
|
18
|
|
|
* @subpackage Core |
|
19
|
|
|
* @author Sebastian Mordziol <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
class Mailcode_Factory |
|
22
|
|
|
{ |
|
23
|
|
|
const ERROR_INVALID_COMMAND_CREATED = 50001; |
|
24
|
|
|
|
|
25
|
|
|
const ERROR_UNEXPECTED_COMMAND_TYPE = 50002; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Creates a ShowVariable command. |
|
29
|
|
|
* |
|
30
|
|
|
* @param string $variableName A variable name, with or without the $ sign prepended. |
|
31
|
|
|
* @return Mailcode_Commands_Command_ShowVariable |
|
32
|
|
|
*/ |
|
33
|
|
|
public static function showVariable(string $variableName) : Mailcode_Commands_Command_ShowVariable |
|
34
|
|
|
{ |
|
35
|
|
|
$variableName = self::_filterVariableName($variableName); |
|
36
|
|
|
|
|
37
|
|
|
$cmd = new Mailcode_Commands_Command_ShowVariable( |
|
38
|
|
|
'', |
|
39
|
|
|
$variableName, |
|
40
|
|
|
'{showvar:'.$variableName.'}' |
|
41
|
|
|
); |
|
42
|
|
|
|
|
43
|
|
|
self::_checkCommand($cmd); |
|
44
|
|
|
|
|
45
|
|
|
return $cmd; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Creates a SetVariable command. |
|
50
|
|
|
* |
|
51
|
|
|
* @param string $variableName A variable name, with or without the $ sign prepended. |
|
52
|
|
|
* @param string $value |
|
53
|
|
|
* @param bool $quoteValue Whether to treat the value as a string literal, and add quotes to it. |
|
54
|
|
|
* @return Mailcode_Commands_Command_SetVariable |
|
55
|
|
|
* @throws Mailcode_Factory_Exception |
|
56
|
|
|
* |
|
57
|
|
|
* @see Mailcode_Factory::ERROR_INVALID_COMMAND_CREATED |
|
58
|
|
|
*/ |
|
59
|
|
|
public static function setVariable(string $variableName, string $value, bool $quoteValue=true) : Mailcode_Commands_Command_SetVariable |
|
60
|
|
|
{ |
|
61
|
|
|
$variableName = self::_filterVariableName($variableName); |
|
62
|
|
|
|
|
63
|
|
|
if($quoteValue) |
|
64
|
|
|
{ |
|
65
|
|
|
$value = self::_quoteString($value); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$params = $variableName.' = '.$value; |
|
69
|
|
|
|
|
70
|
|
|
$cmd = new Mailcode_Commands_Command_SetVariable( |
|
71
|
|
|
'', // type |
|
72
|
|
|
$params, |
|
73
|
|
|
'{setvar: '.$params.'}' |
|
74
|
|
|
); |
|
75
|
|
|
|
|
76
|
|
|
self::_checkCommand($cmd); |
|
77
|
|
|
|
|
78
|
|
|
return $cmd; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public static function comment(string $comments) : Mailcode_Commands_Command_Comment |
|
82
|
|
|
{ |
|
83
|
|
|
$cmd = new Mailcode_Commands_Command_Comment( |
|
84
|
|
|
'', // type, |
|
85
|
|
|
$comments, // params, |
|
86
|
|
|
sprintf( |
|
87
|
|
|
'{comment: %s}', |
|
88
|
|
|
$comments |
|
89
|
|
|
) |
|
90
|
|
|
); |
|
91
|
|
|
|
|
92
|
|
|
self::_checkCommand($cmd); |
|
93
|
|
|
|
|
94
|
|
|
return $cmd; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public static function else() : Mailcode_Commands_Command_Else |
|
98
|
|
|
{ |
|
99
|
|
|
$cmd = new Mailcode_Commands_Command_Else( |
|
100
|
|
|
'', // type, |
|
101
|
|
|
'', // params, |
|
102
|
|
|
'{else}' |
|
103
|
|
|
); |
|
104
|
|
|
|
|
105
|
|
|
self::_checkCommand($cmd); |
|
106
|
|
|
|
|
107
|
|
|
return $cmd; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public static function end() : Mailcode_Commands_Command_End |
|
111
|
|
|
{ |
|
112
|
|
|
$cmd = new Mailcode_Commands_Command_End( |
|
113
|
|
|
'', // type, |
|
114
|
|
|
'', // params, |
|
115
|
|
|
'{end}' |
|
116
|
|
|
); |
|
117
|
|
|
|
|
118
|
|
|
self::_checkCommand($cmd); |
|
119
|
|
|
|
|
120
|
|
|
return $cmd; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
protected static function _buildIf(string $cmd, string $condition, string $type='') : Mailcode_Commands_Command |
|
124
|
|
|
{ |
|
125
|
|
|
$stringType = $type; |
|
126
|
|
|
|
|
127
|
|
|
if(!empty($type)) |
|
128
|
|
|
{ |
|
129
|
|
|
$stringType = ' '.$type; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
$class = '\Mailcode\Mailcode_Commands_Command_'.$cmd; |
|
133
|
|
|
|
|
134
|
|
|
$cmd = new $class( |
|
135
|
|
|
$type, // type, |
|
136
|
|
|
$condition, // params, |
|
137
|
|
|
sprintf( |
|
138
|
|
|
'{%s%s: %s}', |
|
139
|
|
|
strtolower($cmd), |
|
140
|
|
|
$stringType, |
|
141
|
|
|
$condition |
|
142
|
|
|
) |
|
143
|
|
|
); |
|
144
|
|
|
|
|
145
|
|
|
self::_checkCommand($cmd); |
|
146
|
|
|
|
|
147
|
|
|
return $cmd; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
protected static function _buildIfVar(string $cmd, string $variable, string $operand, string $value, bool $quoteValue=false) : Mailcode_Commands_Command |
|
151
|
|
|
{ |
|
152
|
|
|
if($quoteValue) |
|
153
|
|
|
{ |
|
154
|
|
|
$value = self::_quoteString($value); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
$condition = sprintf( |
|
158
|
|
|
"%s %s %s", |
|
159
|
|
|
self::_filterVariableName($variable), |
|
160
|
|
|
$operand, |
|
161
|
|
|
$value |
|
162
|
|
|
); |
|
163
|
|
|
|
|
164
|
|
|
return self::_buildIf($cmd, $condition, 'variable'); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
public static function if(string $condition, string $type='') : Mailcode_Commands_Command_If |
|
168
|
|
|
{ |
|
169
|
|
|
$cmd = self::_buildIf('If', $condition, $type); |
|
170
|
|
|
|
|
171
|
|
|
if($cmd instanceof Mailcode_Commands_Command_If) |
|
172
|
|
|
{ |
|
173
|
|
|
return $cmd; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
throw self::_exceptionUnexpectedType($cmd); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
public static function ifVar(string $variable, string $operand, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_If |
|
180
|
|
|
{ |
|
181
|
|
|
$cmd = self::_buildIfVar('If', $variable, $operand, $value, $quoteValue); |
|
182
|
|
|
|
|
183
|
|
|
if($cmd instanceof Mailcode_Commands_Command_If) |
|
184
|
|
|
{ |
|
185
|
|
|
return $cmd; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
throw self::_exceptionUnexpectedType($cmd); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
public static function ifVarEquals(string $variable, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_If |
|
192
|
|
|
{ |
|
193
|
|
|
$cmd = self::_buildIfVar('If', $variable, '==', $value, $quoteValue); |
|
194
|
|
|
|
|
195
|
|
|
if($cmd instanceof Mailcode_Commands_Command_If) |
|
196
|
|
|
{ |
|
197
|
|
|
return $cmd; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
throw self::_exceptionUnexpectedType($cmd); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
public static function ifVarNotEquals(string $variable, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_If |
|
204
|
|
|
{ |
|
205
|
|
|
$cmd = self::_buildIfVar('If', $variable, '!=', $value, $quoteValue); |
|
206
|
|
|
|
|
207
|
|
|
if($cmd instanceof Mailcode_Commands_Command_If) |
|
208
|
|
|
{ |
|
209
|
|
|
return $cmd; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
throw self::_exceptionUnexpectedType($cmd); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
public static function elseIf(string $condition, string $type='') : Mailcode_Commands_Command_ElseIf |
|
216
|
|
|
{ |
|
217
|
|
|
$cmd = self::_buildIf('ElseIf', $condition, $type); |
|
218
|
|
|
|
|
219
|
|
|
if($cmd instanceof Mailcode_Commands_Command_ElseIf) |
|
220
|
|
|
{ |
|
221
|
|
|
return $cmd; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
throw self::_exceptionUnexpectedType($cmd); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
public static function elseIfVar(string $variable, string $operand, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_ElseIf |
|
228
|
|
|
{ |
|
229
|
|
|
$cmd = self::_buildIfVar('ElseIf', $variable, $operand, $value, $quoteValue); |
|
230
|
|
|
|
|
231
|
|
|
if($cmd instanceof Mailcode_Commands_Command_ElseIf) |
|
232
|
|
|
{ |
|
233
|
|
|
return $cmd; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
throw self::_exceptionUnexpectedType($cmd); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
public static function elseIfVarEquals(string $variable, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_ElseIf |
|
240
|
|
|
{ |
|
241
|
|
|
$cmd = self::_buildIfVar('ElseIf', $variable, '==', $value, $quoteValue); |
|
242
|
|
|
|
|
243
|
|
|
if($cmd instanceof Mailcode_Commands_Command_ElseIf) |
|
244
|
|
|
{ |
|
245
|
|
|
return $cmd; |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
throw self::_exceptionUnexpectedType($cmd); |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
public static function elseIfVarNotEquals(string $variable, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_ElseIf |
|
252
|
|
|
{ |
|
253
|
|
|
$cmd = self::_buildIfVar('ElseIf', $variable, '!=', $value, $quoteValue); |
|
254
|
|
|
|
|
255
|
|
|
if($cmd instanceof Mailcode_Commands_Command_ElseIf) |
|
256
|
|
|
{ |
|
257
|
|
|
return $cmd; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
throw self::_exceptionUnexpectedType($cmd); |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
protected static function _filterVariableName(string $name) : string |
|
264
|
|
|
{ |
|
265
|
|
|
return '$'.ltrim($name, '$'); |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* Quotes a string literal: adds the quotes, and escapes any quotes already present in it. |
|
270
|
|
|
* |
|
271
|
|
|
* @param string $string |
|
272
|
|
|
* @return string |
|
273
|
|
|
*/ |
|
274
|
|
|
protected static function _quoteString(string $string) : string |
|
275
|
|
|
{ |
|
276
|
|
|
return '"'.str_replace('"', '\"', $string).'"'; |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
protected static function _checkCommand(Mailcode_Commands_Command $command) : void |
|
280
|
|
|
{ |
|
281
|
|
|
if($command->isValid()) |
|
282
|
|
|
{ |
|
283
|
|
|
return; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
throw new Mailcode_Factory_Exception( |
|
287
|
|
|
'Invalid command created.', |
|
288
|
|
|
'Validation message: '.$command->getValidationResult()->getErrorMessage(), |
|
289
|
|
|
self::ERROR_INVALID_COMMAND_CREATED, |
|
290
|
|
|
null, |
|
291
|
|
|
$command |
|
292
|
|
|
); |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
protected static function _exceptionUnexpectedType(Mailcode_Commands_Command $command) : Mailcode_Factory_Exception |
|
296
|
|
|
{ |
|
297
|
|
|
return new Mailcode_Factory_Exception( |
|
298
|
|
|
'Invalid command class type created.', |
|
299
|
|
|
null, |
|
300
|
|
|
self::ERROR_UNEXPECTED_COMMAND_TYPE, |
|
301
|
|
|
null, |
|
302
|
|
|
$command |
|
303
|
|
|
); |
|
304
|
|
|
} |
|
305
|
|
|
} |
|
306
|
|
|
|