|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* File containing the {@see Mailcode_Factory} class. |
|
4
|
|
|
* |
|
5
|
|
|
* @package Mailcode |
|
6
|
|
|
* @subpackage Utilities |
|
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 Utilities |
|
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
|
|
|
* @var Mailcode_Renderer |
|
29
|
|
|
*/ |
|
30
|
|
|
protected static $renderer; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Creates a ShowVariable command. |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $variableName A variable name, with or without the $ sign prepended. |
|
36
|
|
|
* @return Mailcode_Commands_Command_ShowVariable |
|
37
|
|
|
*/ |
|
38
|
|
|
public static function showVar(string $variableName) : Mailcode_Commands_Command_ShowVariable |
|
39
|
|
|
{ |
|
40
|
|
|
$variableName = self::_filterVariableName($variableName); |
|
41
|
|
|
|
|
42
|
|
|
$cmd = new Mailcode_Commands_Command_ShowVariable( |
|
43
|
|
|
'', |
|
44
|
|
|
$variableName, |
|
45
|
|
|
'{showvar:'.$variableName.'}' |
|
46
|
|
|
); |
|
47
|
|
|
|
|
48
|
|
|
self::_checkCommand($cmd); |
|
49
|
|
|
|
|
50
|
|
|
return $cmd; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Creates a SetVariable command. |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $variableName A variable name, with or without the $ sign prepended. |
|
57
|
|
|
* @param string $value |
|
58
|
|
|
* @param bool $quoteValue Whether to treat the value as a string literal, and add quotes to it. |
|
59
|
|
|
* @return Mailcode_Commands_Command_SetVariable |
|
60
|
|
|
* @throws Mailcode_Factory_Exception |
|
61
|
|
|
* |
|
62
|
|
|
* @see Mailcode_Factory::ERROR_INVALID_COMMAND_CREATED |
|
63
|
|
|
*/ |
|
64
|
|
|
public static function setVar(string $variableName, string $value, bool $quoteValue=true) : Mailcode_Commands_Command_SetVariable |
|
65
|
|
|
{ |
|
66
|
|
|
$variableName = self::_filterVariableName($variableName); |
|
67
|
|
|
|
|
68
|
|
|
if($quoteValue) |
|
69
|
|
|
{ |
|
70
|
|
|
$value = self::_quoteString($value); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$params = $variableName.' = '.$value; |
|
74
|
|
|
|
|
75
|
|
|
$cmd = new Mailcode_Commands_Command_SetVariable( |
|
76
|
|
|
'', // type |
|
77
|
|
|
$params, |
|
78
|
|
|
'{setvar: '.$params.'}' |
|
79
|
|
|
); |
|
80
|
|
|
|
|
81
|
|
|
self::_checkCommand($cmd); |
|
82
|
|
|
|
|
83
|
|
|
return $cmd; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Like setVar(), but treats the value as a string literal |
|
88
|
|
|
* and automatically adds quotes to it. |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $variableName |
|
91
|
|
|
* @param string $value |
|
92
|
|
|
* @return Mailcode_Commands_Command_SetVariable |
|
93
|
|
|
*/ |
|
94
|
|
|
public static function setVarString(string $variableName, string $value) : Mailcode_Commands_Command_SetVariable |
|
95
|
|
|
{ |
|
96
|
|
|
return self::setVar($variableName, $value, true); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public static function comment(string $comments) : Mailcode_Commands_Command_Comment |
|
100
|
|
|
{ |
|
101
|
|
|
$cmd = new Mailcode_Commands_Command_Comment( |
|
102
|
|
|
'', // type, |
|
103
|
|
|
$comments, // params, |
|
104
|
|
|
sprintf( |
|
105
|
|
|
'{comment: %s}', |
|
106
|
|
|
$comments |
|
107
|
|
|
) |
|
108
|
|
|
); |
|
109
|
|
|
|
|
110
|
|
|
self::_checkCommand($cmd); |
|
111
|
|
|
|
|
112
|
|
|
return $cmd; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public static function else() : Mailcode_Commands_Command_Else |
|
116
|
|
|
{ |
|
117
|
|
|
$cmd = new Mailcode_Commands_Command_Else( |
|
118
|
|
|
'', // type, |
|
119
|
|
|
'', // params, |
|
120
|
|
|
'{else}' |
|
121
|
|
|
); |
|
122
|
|
|
|
|
123
|
|
|
self::_checkCommand($cmd); |
|
124
|
|
|
|
|
125
|
|
|
return $cmd; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public static function end() : Mailcode_Commands_Command_End |
|
129
|
|
|
{ |
|
130
|
|
|
$cmd = new Mailcode_Commands_Command_End( |
|
131
|
|
|
'', // type, |
|
132
|
|
|
'', // params, |
|
133
|
|
|
'{end}' |
|
134
|
|
|
); |
|
135
|
|
|
|
|
136
|
|
|
self::_checkCommand($cmd); |
|
137
|
|
|
|
|
138
|
|
|
return $cmd; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
protected static function _buildIf(string $cmd, string $condition, string $type='') : Mailcode_Commands_Command |
|
142
|
|
|
{ |
|
143
|
|
|
$stringType = $type; |
|
144
|
|
|
|
|
145
|
|
|
if(!empty($type)) |
|
146
|
|
|
{ |
|
147
|
|
|
$stringType = ' '.$type; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
$class = '\Mailcode\Mailcode_Commands_Command_'.$cmd; |
|
151
|
|
|
|
|
152
|
|
|
$cmd = new $class( |
|
153
|
|
|
$type, // type, |
|
154
|
|
|
$condition, // params, |
|
155
|
|
|
sprintf( |
|
156
|
|
|
'{%s%s: %s}', |
|
157
|
|
|
strtolower($cmd), |
|
158
|
|
|
$stringType, |
|
159
|
|
|
$condition |
|
160
|
|
|
) |
|
161
|
|
|
); |
|
162
|
|
|
|
|
163
|
|
|
self::_checkCommand($cmd); |
|
164
|
|
|
|
|
165
|
|
|
return $cmd; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
protected static function _buildIfVar(string $cmd, string $variable, string $operand, string $value, bool $quoteValue=false) : Mailcode_Commands_Command |
|
169
|
|
|
{ |
|
170
|
|
|
if($quoteValue) |
|
171
|
|
|
{ |
|
172
|
|
|
$value = self::_quoteString($value); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
$condition = sprintf( |
|
176
|
|
|
"%s %s %s", |
|
177
|
|
|
self::_filterVariableName($variable), |
|
178
|
|
|
$operand, |
|
179
|
|
|
$value |
|
180
|
|
|
); |
|
181
|
|
|
|
|
182
|
|
|
return self::_buildIf($cmd, $condition, 'variable'); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
public static function if(string $condition, string $type='') : Mailcode_Commands_Command_If |
|
186
|
|
|
{ |
|
187
|
|
|
$cmd = self::_buildIf('If', $condition, $type); |
|
188
|
|
|
|
|
189
|
|
|
if($cmd instanceof Mailcode_Commands_Command_If) |
|
190
|
|
|
{ |
|
191
|
|
|
return $cmd; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
throw self::_exceptionUnexpectedType($cmd); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
public static function ifVar(string $variable, string $operand, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_If |
|
198
|
|
|
{ |
|
199
|
|
|
$cmd = self::_buildIfVar('If', $variable, $operand, $value, $quoteValue); |
|
200
|
|
|
|
|
201
|
|
|
if($cmd instanceof Mailcode_Commands_Command_If) |
|
202
|
|
|
{ |
|
203
|
|
|
return $cmd; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
throw self::_exceptionUnexpectedType($cmd); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
public static function ifVarString(string $variable, string $operand, string $value) : Mailcode_Commands_Command_If |
|
210
|
|
|
{ |
|
211
|
|
|
$cmd = self::_buildIfVar('If', $variable, $operand, $value, true); |
|
212
|
|
|
|
|
213
|
|
|
if($cmd instanceof Mailcode_Commands_Command_If) |
|
214
|
|
|
{ |
|
215
|
|
|
return $cmd; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
throw self::_exceptionUnexpectedType($cmd); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
public static function ifVarEquals(string $variable, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_If |
|
222
|
|
|
{ |
|
223
|
|
|
$cmd = self::_buildIfVar('If', $variable, '==', $value, $quoteValue); |
|
224
|
|
|
|
|
225
|
|
|
if($cmd instanceof Mailcode_Commands_Command_If) |
|
226
|
|
|
{ |
|
227
|
|
|
return $cmd; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
throw self::_exceptionUnexpectedType($cmd); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
public static function ifVarEqualsString(string $variable, string $value) : Mailcode_Commands_Command_If |
|
234
|
|
|
{ |
|
235
|
|
|
$cmd = self::_buildIfVar('If', $variable, '==', $value, true); |
|
236
|
|
|
|
|
237
|
|
|
if($cmd instanceof Mailcode_Commands_Command_If) |
|
238
|
|
|
{ |
|
239
|
|
|
return $cmd; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
throw self::_exceptionUnexpectedType($cmd); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
public static function ifVarNotEquals(string $variable, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_If |
|
246
|
|
|
{ |
|
247
|
|
|
$cmd = self::_buildIfVar('If', $variable, '!=', $value, $quoteValue); |
|
248
|
|
|
|
|
249
|
|
|
if($cmd instanceof Mailcode_Commands_Command_If) |
|
250
|
|
|
{ |
|
251
|
|
|
return $cmd; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
throw self::_exceptionUnexpectedType($cmd); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
public static function ifVarNotEqualsString(string $variable, string $value) : Mailcode_Commands_Command_If |
|
258
|
|
|
{ |
|
259
|
|
|
$cmd = self::_buildIfVar('If', $variable, '!=', $value, true); |
|
260
|
|
|
|
|
261
|
|
|
if($cmd instanceof Mailcode_Commands_Command_If) |
|
262
|
|
|
{ |
|
263
|
|
|
return $cmd; |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
throw self::_exceptionUnexpectedType($cmd); |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
public static function elseIf(string $condition, string $type='') : Mailcode_Commands_Command_ElseIf |
|
270
|
|
|
{ |
|
271
|
|
|
$cmd = self::_buildIf('ElseIf', $condition, $type); |
|
272
|
|
|
|
|
273
|
|
|
if($cmd instanceof Mailcode_Commands_Command_ElseIf) |
|
274
|
|
|
{ |
|
275
|
|
|
return $cmd; |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
throw self::_exceptionUnexpectedType($cmd); |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
public static function elseIfVar(string $variable, string $operand, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_ElseIf |
|
282
|
|
|
{ |
|
283
|
|
|
$cmd = self::_buildIfVar('ElseIf', $variable, $operand, $value, $quoteValue); |
|
284
|
|
|
|
|
285
|
|
|
if($cmd instanceof Mailcode_Commands_Command_ElseIf) |
|
286
|
|
|
{ |
|
287
|
|
|
return $cmd; |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
throw self::_exceptionUnexpectedType($cmd); |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
public static function elseIfVarString(string $variable, string $operand, string $value) : Mailcode_Commands_Command_ElseIf |
|
294
|
|
|
{ |
|
295
|
|
|
$cmd = self::_buildIfVar('ElseIf', $variable, $operand, $value, true); |
|
296
|
|
|
|
|
297
|
|
|
if($cmd instanceof Mailcode_Commands_Command_ElseIf) |
|
298
|
|
|
{ |
|
299
|
|
|
return $cmd; |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
throw self::_exceptionUnexpectedType($cmd); |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
public static function elseIfVarEquals(string $variable, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_ElseIf |
|
306
|
|
|
{ |
|
307
|
|
|
$cmd = self::_buildIfVar('ElseIf', $variable, '==', $value, $quoteValue); |
|
308
|
|
|
|
|
309
|
|
|
if($cmd instanceof Mailcode_Commands_Command_ElseIf) |
|
310
|
|
|
{ |
|
311
|
|
|
return $cmd; |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
throw self::_exceptionUnexpectedType($cmd); |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
public static function elseIfVarEqualsString(string $variable, string $value) : Mailcode_Commands_Command_ElseIf |
|
318
|
|
|
{ |
|
319
|
|
|
$cmd = self::_buildIfVar('ElseIf', $variable, '==', $value, true); |
|
320
|
|
|
|
|
321
|
|
|
if($cmd instanceof Mailcode_Commands_Command_ElseIf) |
|
322
|
|
|
{ |
|
323
|
|
|
return $cmd; |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
throw self::_exceptionUnexpectedType($cmd); |
|
327
|
|
|
} |
|
328
|
|
|
|
|
329
|
|
|
public static function elseIfVarNotEquals(string $variable, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_ElseIf |
|
330
|
|
|
{ |
|
331
|
|
|
$cmd = self::_buildIfVar('ElseIf', $variable, '!=', $value, $quoteValue); |
|
332
|
|
|
|
|
333
|
|
|
if($cmd instanceof Mailcode_Commands_Command_ElseIf) |
|
334
|
|
|
{ |
|
335
|
|
|
return $cmd; |
|
336
|
|
|
} |
|
337
|
|
|
|
|
338
|
|
|
throw self::_exceptionUnexpectedType($cmd); |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
|
|
public static function elseIfVarNotEqualsString(string $variable, string $value) : Mailcode_Commands_Command_ElseIf |
|
342
|
|
|
{ |
|
343
|
|
|
$cmd = self::_buildIfVar('ElseIf', $variable, '!=', $value, true); |
|
344
|
|
|
|
|
345
|
|
|
if($cmd instanceof Mailcode_Commands_Command_ElseIf) |
|
346
|
|
|
{ |
|
347
|
|
|
return $cmd; |
|
348
|
|
|
} |
|
349
|
|
|
|
|
350
|
|
|
throw self::_exceptionUnexpectedType($cmd); |
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
|
|
protected static function _filterVariableName(string $name) : string |
|
354
|
|
|
{ |
|
355
|
|
|
return '$'.ltrim($name, '$'); |
|
356
|
|
|
} |
|
357
|
|
|
|
|
358
|
|
|
/** |
|
359
|
|
|
* Quotes a string literal: adds the quotes, and escapes any quotes already present in it. |
|
360
|
|
|
* |
|
361
|
|
|
* @param string $string |
|
362
|
|
|
* @return string |
|
363
|
|
|
*/ |
|
364
|
|
|
protected static function _quoteString(string $string) : string |
|
365
|
|
|
{ |
|
366
|
|
|
return '"'.str_replace('"', '\"', $string).'"'; |
|
367
|
|
|
} |
|
368
|
|
|
|
|
369
|
|
|
protected static function _checkCommand(Mailcode_Commands_Command $command) : void |
|
370
|
|
|
{ |
|
371
|
|
|
if($command->isValid()) |
|
372
|
|
|
{ |
|
373
|
|
|
return; |
|
374
|
|
|
} |
|
375
|
|
|
|
|
376
|
|
|
throw new Mailcode_Factory_Exception( |
|
377
|
|
|
'Invalid command created.', |
|
378
|
|
|
'Validation message: '.$command->getValidationResult()->getErrorMessage(), |
|
379
|
|
|
self::ERROR_INVALID_COMMAND_CREATED, |
|
380
|
|
|
null, |
|
381
|
|
|
$command |
|
382
|
|
|
); |
|
383
|
|
|
} |
|
384
|
|
|
|
|
385
|
|
|
protected static function _exceptionUnexpectedType(Mailcode_Commands_Command $command) : Mailcode_Factory_Exception |
|
386
|
|
|
{ |
|
387
|
|
|
return new Mailcode_Factory_Exception( |
|
388
|
|
|
'Invalid command class type created.', |
|
389
|
|
|
null, |
|
390
|
|
|
self::ERROR_UNEXPECTED_COMMAND_TYPE, |
|
391
|
|
|
null, |
|
392
|
|
|
$command |
|
393
|
|
|
); |
|
394
|
|
|
} |
|
395
|
|
|
|
|
396
|
|
|
/** |
|
397
|
|
|
* Creates a renderer instance, which can be used to easily |
|
398
|
|
|
* create and convert commands to strings. |
|
399
|
|
|
* |
|
400
|
|
|
* @return Mailcode_Renderer |
|
401
|
|
|
*/ |
|
402
|
|
|
public static function createRenderer() : Mailcode_Renderer |
|
403
|
|
|
{ |
|
404
|
|
|
return new Mailcode_Renderer(); |
|
405
|
|
|
} |
|
406
|
|
|
|
|
407
|
|
|
/** |
|
408
|
|
|
* Creates a printer instance, which works like the renderer, |
|
409
|
|
|
* but outputs the generated strings to standard output. |
|
410
|
|
|
* |
|
411
|
|
|
* @return Mailcode_Printer |
|
412
|
|
|
*/ |
|
413
|
|
|
public static function createPrinter() : Mailcode_Printer |
|
414
|
|
|
{ |
|
415
|
|
|
return new Mailcode_Printer(); |
|
416
|
|
|
} |
|
417
|
|
|
} |
|
418
|
|
|
|