1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File containing the {@see Mailcode_Factory_Instantiator} class. |
4
|
|
|
* |
5
|
|
|
* @package Mailcode |
6
|
|
|
* @subpackage Utilities |
7
|
|
|
* @see Mailcode_Factory_Instantiator |
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_Instantiator |
22
|
|
|
{ |
23
|
|
|
public function buildIf(string $ifType, string $params, string $type='') : Mailcode_Commands_IfBase |
24
|
|
|
{ |
25
|
|
|
$stringType = $type; |
26
|
|
|
|
27
|
|
|
if(!empty($type)) |
28
|
|
|
{ |
29
|
|
|
$stringType = ' '.$type; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$command = Mailcode::create()->getCommands()->createCommand( |
33
|
|
|
$ifType, |
34
|
|
|
$type, |
35
|
|
|
$params, |
36
|
|
|
sprintf( |
37
|
|
|
'{%s%s: %s}', |
38
|
|
|
strtolower($ifType), |
39
|
|
|
$stringType, |
40
|
|
|
$params |
41
|
|
|
) |
42
|
|
|
); |
43
|
|
|
|
44
|
|
|
$this->checkCommand($command); |
45
|
|
|
|
46
|
|
|
if($command instanceof Mailcode_Commands_IfBase) |
47
|
|
|
{ |
48
|
|
|
return $command; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
throw $this->exceptionUnexpectedType('IfBase', $command); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function buildIfVar(string $ifType, string $variable, string $operand, string $value, bool $quoteValue=false) : Mailcode_Commands_IfBase |
55
|
|
|
{ |
56
|
|
|
if($quoteValue) |
57
|
|
|
{ |
58
|
|
|
$value = $this->quoteString($value); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$condition = sprintf( |
62
|
|
|
"%s %s %s", |
63
|
|
|
$this->filterVariableName($variable), |
64
|
|
|
$operand, |
65
|
|
|
$value |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
return $this->buildIf($ifType, $condition, 'variable'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function buildIfEmpty(string $ifType, string $variable) : Mailcode_Commands_IfBase |
72
|
|
|
{ |
73
|
|
|
return $this->buildIf($ifType, $this->filterVariableName($variable), 'empty'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function buildIfNotEmpty(string $ifType, string $variable) : Mailcode_Commands_IfBase |
77
|
|
|
{ |
78
|
|
|
return $this->buildIf($ifType, $this->filterVariableName($variable), 'not-empty'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $ifType |
83
|
|
|
* @param string $variable |
84
|
|
|
* @param string[] $searchTerms |
85
|
|
|
* @param bool $caseInsensitive |
86
|
|
|
* @param bool $regexEnabled |
87
|
|
|
* @param string $containsType |
88
|
|
|
* @return Mailcode_Commands_IfBase |
89
|
|
|
* @throws Mailcode_Factory_Exception |
90
|
|
|
*/ |
91
|
|
|
public function buildIfContains(string $ifType, string $variable, array $searchTerms, bool $caseInsensitive=false, bool $regexEnabled=false, string $containsType='contains') : Mailcode_Commands_IfBase |
92
|
|
|
{ |
93
|
|
|
$condition = sprintf( |
94
|
|
|
'%s%s"%s"', |
95
|
|
|
$this->filterVariableName($variable), |
96
|
|
|
$this->renderListKeywords($caseInsensitive, $regexEnabled), |
97
|
|
|
implode('" "', array_map(array($this, 'filterLiteral'), $searchTerms)) |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
return $this->buildIf($ifType, $condition, $containsType); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
private function renderListKeywords(bool $caseInsensitive=false, bool $regexEnabled=false) : string |
104
|
|
|
{ |
105
|
|
|
$keywords = array(); |
106
|
|
|
|
107
|
|
|
if($caseInsensitive) |
108
|
|
|
{ |
109
|
|
|
$keywords[] = Mailcode_Commands_Keywords::TYPE_INSENSITIVE; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if($regexEnabled) |
113
|
|
|
{ |
114
|
|
|
$keywords[] = Mailcode_Commands_Keywords::TYPE_REGEX; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$keywordsString = ''; |
118
|
|
|
|
119
|
|
|
if(!empty($keywords)) |
120
|
|
|
{ |
121
|
|
|
$keywordsString = ' '.implode(' ', $keywords); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $keywordsString; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param string $ifType |
129
|
|
|
* @param string $variable |
130
|
|
|
* @param string[] $searchTerms |
131
|
|
|
* @param bool $caseInsensitive |
132
|
|
|
* @param bool $regexEnabled |
133
|
|
|
* @return Mailcode_Commands_IfBase |
134
|
|
|
* @throws Mailcode_Factory_Exception |
135
|
|
|
*/ |
136
|
|
|
public function buildIfNotContains(string $ifType, string $variable, array $searchTerms, bool $caseInsensitive=false, bool $regexEnabled=false) : Mailcode_Commands_IfBase |
137
|
|
|
{ |
138
|
|
|
return $this->buildIfContains($ifType, $variable, $searchTerms, $caseInsensitive, $regexEnabled, 'not-contains'); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param string $ifType |
143
|
|
|
* @param string $variable |
144
|
|
|
* @param string[] $searchTerms |
145
|
|
|
* @param bool $caseInsensitive |
146
|
|
|
* @param bool $regexEnabled |
147
|
|
|
* @param string $containsType |
148
|
|
|
* @return Mailcode_Commands_IfBase |
149
|
|
|
* @throws Mailcode_Factory_Exception |
150
|
|
|
*/ |
151
|
|
|
public function buildIfListContains(string $ifType, string $variable, array $searchTerms, bool $caseInsensitive=false, bool $regexEnabled=false, string $containsType='list-contains') : Mailcode_Commands_IfBase |
152
|
|
|
{ |
153
|
|
|
return $this->buildIfContains($ifType, $variable, $searchTerms, $caseInsensitive, $regexEnabled, $containsType); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param string $ifType |
158
|
|
|
* @param string $variable |
159
|
|
|
* @param string[] $searchTerms |
160
|
|
|
* @param bool $caseInsensitive |
161
|
|
|
* @param bool $regexEnabled |
162
|
|
|
* @return Mailcode_Commands_IfBase |
163
|
|
|
* @throws Mailcode_Factory_Exception |
164
|
|
|
*/ |
165
|
|
|
public function buildIfListNotContains(string $ifType, string $variable, array $searchTerms, bool $caseInsensitive=false, bool $regexEnabled=false) : Mailcode_Commands_IfBase |
166
|
|
|
{ |
167
|
|
|
return $this->buildIfContains($ifType, $variable, $searchTerms, $caseInsensitive, $regexEnabled, 'list-not-contains'); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function buildIfBeginsWith(string $ifType, string $variable, string $search, bool $caseInsensitive=false) : Mailcode_Commands_IfBase |
171
|
|
|
{ |
172
|
|
|
return $this->buildIfSearch($ifType, 'begins-with', $variable, $search, $caseInsensitive); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function buildIfEndsWith(string $ifType, string $variable, string $search, bool $caseInsensitive=false) : Mailcode_Commands_IfBase |
176
|
|
|
{ |
177
|
|
|
return $this->buildIfSearch($ifType, 'ends-with', $variable, $search, $caseInsensitive); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
private function buildIfNumeric(string $ifType, string $variable, string $value, string $type) : Mailcode_Commands_IfBase |
181
|
|
|
{ |
182
|
|
|
$params = sprintf( |
183
|
|
|
'%1$s "%2$s"', |
184
|
|
|
'$'.ltrim($variable, '$'), |
185
|
|
|
$value |
186
|
|
|
); |
187
|
|
|
|
188
|
|
|
return $this->buildIf( |
189
|
|
|
$ifType, |
190
|
|
|
$params, |
191
|
|
|
$type |
192
|
|
|
); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
public function buildIfBiggerThan(string $ifType, string $variable, string $value) : Mailcode_Commands_IfBase |
196
|
|
|
{ |
197
|
|
|
return $this->buildIfNumeric( |
198
|
|
|
$ifType, |
199
|
|
|
$variable, |
200
|
|
|
$value, |
201
|
|
|
'bigger-than' |
202
|
|
|
); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function buildIfSmallerThan(string $ifType, string $variable, string $value) : Mailcode_Commands_IfBase |
206
|
|
|
{ |
207
|
|
|
return $this->buildIfNumeric( |
208
|
|
|
$ifType, |
209
|
|
|
$variable, |
210
|
|
|
$value, |
211
|
|
|
'smaller-than' |
212
|
|
|
); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
public function buildIfEquals(string $ifType, string $variable, string $value) : Mailcode_Commands_IfBase |
216
|
|
|
{ |
217
|
|
|
return $this->buildIfNumeric( |
218
|
|
|
$ifType, |
219
|
|
|
$variable, |
220
|
|
|
$value, |
221
|
|
|
'equals-number' |
222
|
|
|
); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
private function buildIfSearch(string $ifType, string $subType, string $variable, string $search, bool $caseInsensitive=false) : Mailcode_Commands_IfBase |
226
|
|
|
{ |
227
|
|
|
$keyword = ' '; |
228
|
|
|
|
229
|
|
|
if($caseInsensitive) |
230
|
|
|
{ |
231
|
|
|
$keyword = ' '.Mailcode_Commands_Keywords::TYPE_INSENSITIVE; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
$condition = sprintf( |
235
|
|
|
'%s%s"%s"', |
236
|
|
|
$this->filterVariableName($variable), |
237
|
|
|
$keyword, |
238
|
|
|
$this->filterLiteral($search) |
239
|
|
|
); |
240
|
|
|
|
241
|
|
|
return $this->buildIf($ifType, $condition, $subType); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
public function filterLiteral(string $term) : string |
245
|
|
|
{ |
246
|
|
|
return str_replace('"', '\"', $term); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
public function filterVariableName(string $name) : string |
250
|
|
|
{ |
251
|
|
|
$name = preg_replace('/\s/', '', $name); |
252
|
|
|
|
253
|
|
|
return '$'.ltrim($name, '$'); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
public function checkCommand(Mailcode_Commands_Command $command) : void |
257
|
|
|
{ |
258
|
|
|
if($command->isValid()) |
259
|
|
|
{ |
260
|
|
|
return; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
throw new Mailcode_Factory_Exception( |
264
|
|
|
'Invalid command created.', |
265
|
|
|
'Validation message: '.$command->getValidationResult()->getErrorMessage(), |
266
|
|
|
Mailcode_Factory::ERROR_INVALID_COMMAND_CREATED, |
267
|
|
|
null, |
268
|
|
|
$command |
269
|
|
|
); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Configures the command's URL encoding or decoding, depending |
274
|
|
|
* on the selected mode. |
275
|
|
|
* |
276
|
|
|
* @param Mailcode_Commands_Command $cmd |
277
|
|
|
* @param string $urlEncoding |
278
|
|
|
* @throws Mailcode_Exception |
279
|
|
|
* |
280
|
|
|
* @see Mailcode_Factory::URL_ENCODING_NONE |
281
|
|
|
* @see Mailcode_Factory::URL_ENCODING_ENCODE |
282
|
|
|
* @see Mailcode_Factory::URL_ENCODING_DECODE |
283
|
|
|
*/ |
284
|
|
|
public function setEncoding(Mailcode_Commands_Command $cmd, string $urlEncoding) : void |
285
|
|
|
{ |
286
|
|
|
// First off, reset the encoding |
287
|
|
|
$cmd->setURLEncoding(false); |
288
|
|
|
$cmd->setURLDecoding(false); |
289
|
|
|
|
290
|
|
|
if ($urlEncoding === Mailcode_Factory::URL_ENCODING_ENCODE) { |
291
|
|
|
$cmd->setURLEncoding(); |
292
|
|
|
return; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
if ($urlEncoding === Mailcode_Factory::URL_ENCODING_DECODE) { |
296
|
|
|
$cmd->setURLDecoding(); |
297
|
|
|
return; |
298
|
|
|
} |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Quotes a string literal: adds the quotes, and escapes any quotes already present in it. |
303
|
|
|
* |
304
|
|
|
* @param string $string |
305
|
|
|
* @return string |
306
|
|
|
*/ |
307
|
|
|
public function quoteString(string $string) : string |
308
|
|
|
{ |
309
|
|
|
if(substr($string, 0, 1) === '"' && substr($string, -1, 1) === '"') { |
310
|
|
|
return $string; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
return '"'.str_replace('"', '\"', $string).'"'; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
public function exceptionUnexpectedType(string $type, Mailcode_Commands_Command $command) : Mailcode_Factory_Exception |
317
|
|
|
{ |
318
|
|
|
return new Mailcode_Factory_Exception( |
319
|
|
|
'Invalid command class type created.', |
320
|
|
|
sprintf('Excepted type [%s], but created class [%s].', $type, get_class($command)), |
321
|
|
|
Mailcode_Factory::ERROR_UNEXPECTED_COMMAND_TYPE, |
322
|
|
|
null, |
323
|
|
|
$command |
324
|
|
|
); |
325
|
|
|
} |
326
|
|
|
} |
327
|
|
|
|