Passed
Push — master ( 6ab608...f87868 )
by Sebastian
03:03
created

Mailcode_Factory::showSnippet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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