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
|
|
|
const ERROR_UNEXPECTED_COMMAND_TYPE = 50002; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Mailcode_Factory_CommandSets |
28
|
|
|
*/ |
29
|
|
|
private static $commandSets; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Creates a ShowVariable command. |
33
|
|
|
* |
34
|
|
|
* @param string $variableName A variable name, with or without the $ sign prepended. |
35
|
|
|
* @return Mailcode_Commands_Command_ShowVariable |
36
|
|
|
*/ |
37
|
|
|
public static function showVar(string $variableName) : Mailcode_Commands_Command_ShowVariable |
38
|
|
|
{ |
39
|
|
|
return self::$commandSets->show()->showVar($variableName); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Creates a ShowDate command, used to display date variables and |
44
|
|
|
* format the date using date format strings. |
45
|
|
|
* |
46
|
|
|
* @param string $variableName A variable name, with or without the $ sign prepended. |
47
|
|
|
* @param string $formatString A date format string, or empty string for default. |
48
|
|
|
* @return Mailcode_Commands_Command_ShowDate |
49
|
|
|
*/ |
50
|
|
|
public static function showDate(string $variableName, string $formatString="") : Mailcode_Commands_Command_ShowDate |
51
|
|
|
{ |
52
|
|
|
return self::$commandSets->show()->showDate($variableName, $formatString); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Creates a ShowSnippet command. |
57
|
|
|
* |
58
|
|
|
* @param string $snippetName A snippet name, with or without the $ sign prepended. |
59
|
|
|
* @return Mailcode_Commands_Command_ShowSnippet |
60
|
|
|
*/ |
61
|
|
|
public static function showSnippet(string $snippetName) : Mailcode_Commands_Command_ShowSnippet |
62
|
|
|
{ |
63
|
|
|
return self::$commandSets->show()->showSnippet($snippetName); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Creates a SetVariable command. |
68
|
|
|
* |
69
|
|
|
* @param string $variableName A variable name, with or without the $ sign prepended. |
70
|
|
|
* @param string $value |
71
|
|
|
* @param bool $quoteValue Whether to treat the value as a string literal, and add quotes to it. |
72
|
|
|
* @return Mailcode_Commands_Command_SetVariable |
73
|
|
|
* @throws Mailcode_Factory_Exception |
74
|
|
|
* |
75
|
|
|
* @see Mailcode_Factory::ERROR_INVALID_COMMAND_CREATED |
76
|
|
|
*/ |
77
|
|
|
public static function setVar(string $variableName, string $value, bool $quoteValue=true) : Mailcode_Commands_Command_SetVariable |
78
|
|
|
{ |
79
|
|
|
return self::$commandSets->set()->setVar($variableName, $value, $quoteValue); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Like setVar(), but treats the value as a string literal |
84
|
|
|
* and automatically adds quotes to it. |
85
|
|
|
* |
86
|
|
|
* @param string $variableName |
87
|
|
|
* @param string $value |
88
|
|
|
* @return Mailcode_Commands_Command_SetVariable |
89
|
|
|
*/ |
90
|
|
|
public static function setVarString(string $variableName, string $value) : Mailcode_Commands_Command_SetVariable |
91
|
|
|
{ |
92
|
|
|
return self::$commandSets->set()->setVar($variableName, $value, true); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public static function comment(string $comments) : Mailcode_Commands_Command_Comment |
96
|
|
|
{ |
97
|
|
|
return self::$commandSets->misc()->comment($comments); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public static function else() : Mailcode_Commands_Command_Else |
101
|
|
|
{ |
102
|
|
|
return self::$commandSets->if()->else(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public static function end() : Mailcode_Commands_Command_End |
106
|
|
|
{ |
107
|
|
|
return self::$commandSets->if()->end(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public static function if(string $condition, string $type='') : Mailcode_Commands_Command_If |
111
|
|
|
{ |
112
|
|
|
return self::$commandSets->if()->if($condition, $type); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public static function ifVar(string $variable, string $operand, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_If_Variable |
116
|
|
|
{ |
117
|
|
|
return self::$commandSets->if()->ifVar($variable, $operand, $value, $quoteValue); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public static function ifVarString(string $variable, string $operand, string $value) : Mailcode_Commands_Command_If_Variable |
121
|
|
|
{ |
122
|
|
|
return self::$commandSets->if()->ifVarString($variable, $operand, $value); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public static function ifVarEquals(string $variable, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_If_Variable |
126
|
|
|
{ |
127
|
|
|
return self::$commandSets->if()->ifVarEquals($variable, $value, $quoteValue); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public static function ifVarEqualsString(string $variable, string $value) : Mailcode_Commands_Command_If |
131
|
|
|
{ |
132
|
|
|
return self::$commandSets->if()->ifVarEqualsString($variable, $value); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public static function ifVarNotEquals(string $variable, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_If_Variable |
136
|
|
|
{ |
137
|
|
|
return self::$commandSets->if()->ifVarNotEquals($variable, $value, $quoteValue); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public static function ifVarNotEqualsString(string $variable, string $value) : Mailcode_Commands_Command_If_Variable |
141
|
|
|
{ |
142
|
|
|
return self::$commandSets->if()->ifVarNotEqualsString($variable, $value); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public static function elseIf(string $condition, string $type='') : Mailcode_Commands_Command_ElseIf |
146
|
|
|
{ |
147
|
|
|
return self::$commandSets->elseIf()->elseIf($condition, $type); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public static function elseIfEmpty(string $variable) : Mailcode_Commands_Command_ElseIf_Empty |
151
|
|
|
{ |
152
|
|
|
return self::$commandSets->elseIf()->elseIfEmpty($variable); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public static function elseIfNotEmpty(string $variable) : Mailcode_Commands_Command_ElseIf_NotEmpty |
156
|
|
|
{ |
157
|
|
|
return self::$commandSets->elseIf()->elseIfNotEmpty($variable); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public static function elseIfVar(string $variable, string $operand, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_ElseIf_Variable |
161
|
|
|
{ |
162
|
|
|
return self::$commandSets->elseIf()->elseIfVar($variable, $operand, $value, $quoteValue); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public static function elseIfVarString(string $variable, string $operand, string $value) : Mailcode_Commands_Command_ElseIf_Variable |
166
|
|
|
{ |
167
|
|
|
return self::$commandSets->elseIf()->elseIfVarString($variable, $operand, $value); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public static function elseIfVarEquals(string $variable, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_ElseIf_Variable |
171
|
|
|
{ |
172
|
|
|
return self::$commandSets->elseIf()->elseIfVarEquals($variable, $value, $quoteValue); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public static function elseIfVarEqualsString(string $variable, string $value) : Mailcode_Commands_Command_ElseIf_Variable |
176
|
|
|
{ |
177
|
|
|
return self::$commandSets->elseIf()->elseIfVarEqualsString($variable, $value); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public static function elseIfVarNotEquals(string $variable, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_ElseIf_Variable |
181
|
|
|
{ |
182
|
|
|
return self::$commandSets->elseIf()->elseIfVarNotEquals($variable, $value, $quoteValue); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public static function elseIfVarNotEqualsString(string $variable, string $value) : Mailcode_Commands_Command_ElseIf_Variable |
186
|
|
|
{ |
187
|
|
|
return self::$commandSets->elseIf()->elseIfVarNotEqualsString($variable, $value); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public static function ifBeginsWith(string $variable, string $search, bool $caseInsensitive=false) : Mailcode_Commands_Command_If_BeginsWith |
191
|
|
|
{ |
192
|
|
|
return self::$commandSets->if()->ifBeginsWith($variable, $search, $caseInsensitive); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
public static function ifEndsWith(string $variable, string $search, bool $caseInsensitive=false) : Mailcode_Commands_Command_If_EndsWith |
196
|
|
|
{ |
197
|
|
|
return self::$commandSets->if()->ifEndsWith($variable, $search, $caseInsensitive); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public static function elseIfBeginsWith(string $variable, string $search, bool $caseInsensitive=false) : Mailcode_Commands_Command_ElseIf_BeginsWith |
201
|
|
|
{ |
202
|
|
|
return self::$commandSets->elseIf()->elseIfBeginsWith($variable, $search, $caseInsensitive); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public static function elseIfEndsWith(string $variable, string $search, bool $caseInsensitive=false) : Mailcode_Commands_Command_ElseIf_EndsWith |
206
|
|
|
{ |
207
|
|
|
return self::$commandSets->elseIf()->elseIfEndsWith($variable, $search, $caseInsensitive); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
public static function ifContains(string $variable, string $search, bool $caseInsensitive=false) : Mailcode_Commands_Command_If_Contains |
211
|
|
|
{ |
212
|
|
|
return self::$commandSets->if()->ifContains($variable, array($search), $caseInsensitive); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Creates if contains command, with several search terms. |
217
|
|
|
* |
218
|
|
|
* @param string $variable |
219
|
|
|
* @param string[] $searchTerms List of search terms. Do not add surrounding quotes. |
220
|
|
|
* @param bool $caseInsensitive |
221
|
|
|
* @return Mailcode_Commands_Command_If_Contains |
222
|
|
|
*/ |
223
|
|
|
public static function ifContainsAny(string $variable, array $searchTerms, bool $caseInsensitive=false) : Mailcode_Commands_Command_If_Contains |
224
|
|
|
{ |
225
|
|
|
return self::$commandSets->if()->ifContains($variable, $searchTerms, $caseInsensitive); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
public static function elseIfContains(string $variable, string $search, bool $caseInsensitive=false) : Mailcode_Commands_Command_ElseIf_Contains |
229
|
|
|
{ |
230
|
|
|
return self::$commandSets->elseIf()->elseIfContains($variable, array($search), $caseInsensitive); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Creates else if contains command, with several search terms. |
235
|
|
|
* |
236
|
|
|
* @param string $variable |
237
|
|
|
* @param string[] $searchTerms List of search terms. Do not add surrounding quotes. |
238
|
|
|
* @param bool $caseInsensitive |
239
|
|
|
* @return Mailcode_Commands_Command_ElseIf_Contains |
240
|
|
|
*/ |
241
|
|
|
public static function elseIfContainsAny(string $variable, array $searchTerms, bool $caseInsensitive=false) : Mailcode_Commands_Command_ElseIf_Contains |
242
|
|
|
{ |
243
|
|
|
return self::$commandSets->elseIf()->elseIfContains($variable, $searchTerms, $caseInsensitive); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
public static function ifEmpty(string $variable) : Mailcode_Commands_Command_If_Empty |
247
|
|
|
{ |
248
|
|
|
return self::$commandSets->if()->ifEmpty($variable); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
public static function ifNotEmpty(string $variable) : Mailcode_Commands_Command_If_NotEmpty |
252
|
|
|
{ |
253
|
|
|
return self::$commandSets->if()->ifNotEmpty($variable); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Creates a renderer instance, which can be used to easily |
258
|
|
|
* create and convert commands to strings. |
259
|
|
|
* |
260
|
|
|
* @return Mailcode_Renderer |
261
|
|
|
*/ |
262
|
|
|
public static function createRenderer() : Mailcode_Renderer |
263
|
|
|
{ |
264
|
|
|
return new Mailcode_Renderer(); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Creates a printer instance, which works like the renderer, |
269
|
|
|
* but outputs the generated strings to standard output. |
270
|
|
|
* |
271
|
|
|
* @return Mailcode_Printer |
272
|
|
|
*/ |
273
|
|
|
public static function createPrinter() : Mailcode_Printer |
274
|
|
|
{ |
275
|
|
|
return new Mailcode_Printer(); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Gets/creates the global instance of the date format info |
280
|
|
|
* class, used to handle date formatting aspects. |
281
|
|
|
* |
282
|
|
|
* @return Mailcode_Date_FormatInfo |
283
|
|
|
*/ |
284
|
|
|
public static function createDateInfo() : Mailcode_Date_FormatInfo |
285
|
|
|
{ |
286
|
|
|
return Mailcode_Date_FormatInfo::getInstance(); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
public static function init() : void |
290
|
|
|
{ |
291
|
|
|
if(!isset(self::$commandSets)) |
292
|
|
|
{ |
293
|
|
|
self::$commandSets = new Mailcode_Factory_CommandSets(); |
294
|
|
|
} |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
Mailcode_Factory::init(); |
299
|
|
|
|