Passed
Push — master ( 1b5df6...213867 )
by Sebastian
13:22
created

Mailcode_Factory::_buildIfContains()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
c 0
b 0
f 0
nc 2
nop 4
dl 0
loc 17
rs 9.9666
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 = Mailcode::create()->getCommands()->createCommand(
43
            'ShowVariable',
44
            '',
45
            $variableName,
46
            '{showvar:'.$variableName.'}'
47
        );
48
        
49
        self::_checkCommand($cmd);
50
        
51
        if($cmd instanceof Mailcode_Commands_Command_ShowVariable)
52
        {
53
            return $cmd;
54
        }
55
        
56
        throw self::_exceptionUnexpectedType('ShowVariable', $cmd);
57
    }
58
59
   /**
60
    * Creates a ShowSnippet command.
61
    *
62
    * @param string $snippetName A snippet name, with or without the $ sign prepended.
63
    * @return Mailcode_Commands_Command_ShowSnippet
64
    */
65
    public static function showSnippet(string $snippetName) : Mailcode_Commands_Command_ShowSnippet
66
    {
67
        $snippetName = self::_filterVariableName($snippetName);
68
        
69
        $cmd = Mailcode::create()->getCommands()->createCommand(
70
            'ShowSnippet',
71
            '',
72
            $snippetName,
73
            '{showsnippet:'.$snippetName.'}'
74
        );
75
        
76
        self::_checkCommand($cmd);
77
        
78
        if($cmd instanceof Mailcode_Commands_Command_ShowSnippet)
79
        {
80
            return $cmd;
81
        }
82
        
83
        throw self::_exceptionUnexpectedType('ShowSnippet', $cmd);
84
    }
85
    
86
   /**
87
    * Creates a SetVariable command.
88
    * 
89
    * @param string $variableName A variable name, with or without the $ sign prepended.
90
    * @param string $value
91
    * @param bool $quoteValue Whether to treat the value as a string literal, and add quotes to it.
92
    * @return Mailcode_Commands_Command_SetVariable
93
    * @throws Mailcode_Factory_Exception
94
    * 
95
    * @see Mailcode_Factory::ERROR_INVALID_COMMAND_CREATED
96
    */
97
    public static function setVar(string $variableName, string $value, bool $quoteValue=true) : Mailcode_Commands_Command_SetVariable
98
    {
99
        $variableName = self::_filterVariableName($variableName);
100
        
101
        if($quoteValue)
102
        {
103
            $value = self::_quoteString($value);
104
        }
105
        
106
        $params = $variableName.' = '.$value;
107
        
108
        $cmd = Mailcode::create()->getCommands()->createCommand(
109
            'SetVariable',
110
            '', // type
111
            $params,
112
            '{setvar: '.$params.'}'
113
        );
114
        
115
        self::_checkCommand($cmd);
116
        
117
        if($cmd instanceof Mailcode_Commands_Command_SetVariable)
118
        {
119
            return $cmd;
120
        }
121
        
122
        throw self::_exceptionUnexpectedType('SetVariable', $cmd);
123
    }
124
    
125
   /**
126
    * Like setVar(), but treats the value as a string literal
127
    * and automatically adds quotes to it.
128
    * 
129
    * @param string $variableName
130
    * @param string $value
131
    * @return Mailcode_Commands_Command_SetVariable
132
    */
133
    public static function setVarString(string $variableName, string $value) : Mailcode_Commands_Command_SetVariable
134
    {
135
        return self::setVar($variableName, $value, true);
136
    }
137
    
138
    public static function comment(string $comments) : Mailcode_Commands_Command_Comment
139
    {
140
        $cmd = Mailcode::create()->getCommands()->createCommand(
141
            'Comment',
142
            '', // type
143
            $comments, // params
144
            sprintf(
145
                '{comment: %s}',
146
                $comments
147
            )
148
        );
149
        
150
        self::_checkCommand($cmd);
151
        
152
        if($cmd instanceof Mailcode_Commands_Command_Comment)
153
        {
154
            return $cmd;
155
        }
156
        
157
        throw self::_exceptionUnexpectedType('Comment', $cmd);
158
    }
159
    
160
    public static function else() : Mailcode_Commands_Command_Else
161
    {
162
        $cmd = Mailcode::create()->getCommands()->createCommand(
163
            'Else', 
164
            '', 
165
            '', 
166
            '{else}'
167
        );
168
        
169
        self::_checkCommand($cmd);
170
        
171
        if($cmd instanceof Mailcode_Commands_Command_Else)
172
        {
173
            return $cmd;
174
        }
175
        
176
        throw self::_exceptionUnexpectedType('Else', $cmd);
177
    }
178
    
179
    public static function end() : Mailcode_Commands_Command_End
180
    {
181
        $cmd = Mailcode::create()->getCommands()->createCommand(
182
            'End',
183
            '',
184
            '',
185
            '{end}'
186
        );
187
        
188
        self::_checkCommand($cmd);
189
        
190
        if($cmd instanceof Mailcode_Commands_Command_End)
191
        {
192
            return $cmd;
193
        }
194
        
195
        throw self::_exceptionUnexpectedType('End', $cmd);
196
    }
197
    
198
    protected static function _buildIf(string $ifType, string $params, string $type='') : Mailcode_Commands_IfBase
199
    {
200
        $stringType = $type;
201
        
202
        if(!empty($type))
203
        {
204
            $stringType = ' '.$type;
205
        }
206
        
207
        $command = Mailcode::create()->getCommands()->createCommand(
208
            $ifType, 
209
            $type, 
210
            $params, 
211
            sprintf(
212
                '{%s%s: %s}',
213
                strtolower($ifType),
214
                $stringType,
215
                $params
216
            )
217
        );
218
        
219
        self::_checkCommand($command);
220
        
221
        if($command instanceof Mailcode_Commands_IfBase)
222
        {
223
            return $command;
224
        }
225
        
226
        throw self::_exceptionUnexpectedType('IfBase', $command);
227
    }
228
  
229
    protected static function _buildIfVar(string $ifType, string $variable, string $operand, string $value, bool $quoteValue=false) : Mailcode_Commands_IfBase
230
    {
231
        if($quoteValue)
232
        {
233
            $value = self::_quoteString($value);
234
        }
235
        
236
        $condition = sprintf(
237
            "%s %s %s",
238
            self::_filterVariableName($variable),
239
            $operand,
240
            $value
241
        );
242
        
243
        return self::_buildIf($ifType, $condition, 'variable');
244
    }
245
    
246
    public static function if(string $condition, string $type='') : Mailcode_Commands_Command_If
247
    {
248
        $command = self::_buildIf('If', $condition, $type);
249
        
250
        if($command instanceof Mailcode_Commands_Command_If)
251
        {
252
            return $command;
253
        }
254
       
255
        throw self::_exceptionUnexpectedType('If', $command);
256
    }
257
    
258
    public static function ifVar(string $variable, string $operand, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_If_Variable
259
    {
260
        $command = self::_buildIfVar('If', $variable, $operand, $value, $quoteValue);
261
        
262
        if($command instanceof Mailcode_Commands_Command_If_Variable)
263
        {
264
            return $command;
265
        }
266
        
267
        throw self::_exceptionUnexpectedType('IfVar', $command);
268
    }
269
270
    public static function ifVarString(string $variable, string $operand, string $value) : Mailcode_Commands_Command_If_Variable
271
    {
272
        $command = self::_buildIfVar('If', $variable, $operand, $value, true);
273
        
274
        if($command instanceof Mailcode_Commands_Command_If_Variable)
275
        {
276
            return $command;
277
        }
278
        
279
        throw self::_exceptionUnexpectedType('IfVarString', $command);
280
    }
281
    
282
    public static function ifVarEquals(string $variable, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_If_Variable
283
    {
284
        $command = self::_buildIfVar('If', $variable, '==', $value, $quoteValue);
285
        
286
        if($command instanceof Mailcode_Commands_Command_If_Variable)
287
        {
288
            return $command;
289
        }
290
        
291
        throw self::_exceptionUnexpectedType('IfVarEquals', $command);
292
    }
293
294
    public static function ifVarEqualsString(string $variable, string $value) : Mailcode_Commands_Command_If
295
    {
296
        $command = self::_buildIfVar('If', $variable, '==', $value, true);
297
        
298
        if($command instanceof Mailcode_Commands_Command_If_Variable)
299
        {
300
            return $command;
301
        }
302
        
303
        throw self::_exceptionUnexpectedType('IfarEqualsString', $command);
304
    }
305
    
306
    public static function ifVarNotEquals(string $variable, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_If_Variable
307
    {
308
        $command = self::_buildIfVar('If', $variable, '!=', $value, $quoteValue);
309
        
310
        if($command instanceof Mailcode_Commands_Command_If_Variable)
311
        {
312
            return $command;
313
        }
314
        
315
        throw self::_exceptionUnexpectedType('IfVarNotEquals', $command);
316
    }
317
318
    public static function ifVarNotEqualsString(string $variable, string $value) : Mailcode_Commands_Command_If_Variable
319
    {
320
        $command = self::_buildIfVar('If', $variable, '!=', $value, true);
321
        
322
        if($command instanceof Mailcode_Commands_Command_If_Variable)
323
        {
324
            return $command;
325
        }
326
        
327
        throw self::_exceptionUnexpectedType('IfVarNotEqualsString', $command);
328
    }
329
    
330
    public static function elseIf(string $condition, string $type='') : Mailcode_Commands_Command_ElseIf
331
    {
332
        $command = self::_buildIf('ElseIf', $condition, $type);
333
        
334
        if($command instanceof Mailcode_Commands_Command_ElseIf)
335
        {
336
            return $command;
337
        }
338
        
339
        throw self::_exceptionUnexpectedType('ElseIf', $command);
340
    }
341
    
342
    public static function elseIfVar(string $variable, string $operand, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_ElseIf_Variable
343
    {
344
        $command = self::_buildIfVar('ElseIf', $variable, $operand, $value, $quoteValue);
345
        
346
        if($command instanceof Mailcode_Commands_Command_ElseIf_Variable)
347
        {
348
            return $command;
349
        }
350
        
351
        throw self::_exceptionUnexpectedType('ElseIfVariable', $command);
352
    }
353
354
    public static function elseIfVarString(string $variable, string $operand, string $value) : Mailcode_Commands_Command_ElseIf_Variable
355
    {
356
        $command = self::_buildIfVar('ElseIf', $variable, $operand, $value, true);
357
        
358
        if($command instanceof Mailcode_Commands_Command_ElseIf_Variable)
359
        {
360
            return $command;
361
        }
362
        
363
        throw self::_exceptionUnexpectedType('ElseIfVarString', $command);
364
    }
365
    
366
    public static function elseIfVarEquals(string $variable, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_ElseIf_Variable
367
    {
368
        $command = self::_buildIfVar('ElseIf', $variable, '==', $value, $quoteValue);
369
        
370
        if($command instanceof Mailcode_Commands_Command_ElseIf_Variable)
371
        {
372
            return $command;
373
        }
374
        
375
        throw self::_exceptionUnexpectedType('ElseIfVarEquals', $command);
376
    }
377
378
    public static function elseIfVarEqualsString(string $variable, string $value) : Mailcode_Commands_Command_ElseIf_Variable
379
    {
380
        $command = self::_buildIfVar('ElseIf', $variable, '==', $value, true);
381
        
382
        if($command instanceof Mailcode_Commands_Command_ElseIf_Variable)
383
        {
384
            return $command;
385
        }
386
        
387
        throw self::_exceptionUnexpectedType('ElseIfVarEqualsString', $command);
388
    }
389
    
390
    public static function elseIfVarNotEquals(string $variable, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_ElseIf_Variable
391
    {
392
        $command = self::_buildIfVar('ElseIf', $variable, '!=', $value, $quoteValue);
393
        
394
        if($command instanceof Mailcode_Commands_Command_ElseIf_Variable)
395
        {
396
            return $command;
397
        }
398
        
399
        throw self::_exceptionUnexpectedType('ElseIfVarNotEquals', $command);
400
    }
401
402
    public static function elseIfVarNotEqualsString(string $variable, string $value) : Mailcode_Commands_Command_ElseIf_Variable
403
    {
404
        $command = self::_buildIfVar('ElseIf', $variable, '!=', $value, true);
405
        
406
        if($command instanceof Mailcode_Commands_Command_ElseIf_Variable)
407
        {
408
            return $command;
409
        }
410
        
411
        throw self::_exceptionUnexpectedType('ElseIfVarNotEqualsString', $command);
412
    }
413
    
414
    public static function ifContains(string $variable, string $search, bool $caseInsensitive=false) : Mailcode_Commands_Command_If_Contains
415
    {
416
        $command = self::_buildIfContains('If', $variable, $search, $caseInsensitive);
417
        
418
        if($command instanceof Mailcode_Commands_Command_If_Contains)
419
        {
420
            return $command;
421
        }
422
        
423
        throw self::_exceptionUnexpectedType('ElseIfContains', $command);
424
    }
425
    
426
    public static function elseIfContains(string $variable, string $search, bool $caseInsensitive=false) : Mailcode_Commands_Command_ElseIf_Contains
427
    {
428
        $command = self::_buildIfContains('ElseIf', $variable, $search, $caseInsensitive);
429
        
430
        if($command instanceof Mailcode_Commands_Command_ElseIf_Contains)
431
        {
432
            return $command;
433
        }
434
        
435
        throw self::_exceptionUnexpectedType('ElseIfContains', $command);
436
    }
437
    
438
    protected static function _buildIfContains(string $ifType, string $variable, string $search, bool $caseInsensitive=false) : Mailcode_Commands_IfBase
439
    {
440
        $keyword = ' ';
441
        
442
        if($caseInsensitive)
443
        {
444
            $keyword = ' insensitive: ';
445
        }
446
        
447
        $condition = sprintf(
448
            '%s%s"%s"',
449
            self::_filterVariableName($variable),
450
            $keyword,
451
            $search
452
        );
453
        
454
        return self::_buildIf($ifType, $condition, 'contains');
455
    }
456
    
457
    protected static function _filterVariableName(string $name) : string
458
    {
459
        $name = preg_replace('/\s/', '', $name);
460
        
461
        return '$'.ltrim($name, '$');
462
    }
463
    
464
   /**
465
    * Quotes a string literal: adds the quotes, and escapes any quotes already present in it.
466
    * 
467
    * @param string $string
468
    * @return string
469
    */
470
    protected static function _quoteString(string $string) : string
471
    {
472
        return '"'.str_replace('"', '\"', $string).'"';
473
    }
474
    
475
    protected static function _checkCommand(Mailcode_Commands_Command $command) : void
476
    {
477
        if($command->isValid())
478
        {
479
            return;
480
        }
481
        
482
        throw new Mailcode_Factory_Exception(
483
            'Invalid command created.',
484
            'Validation message: '.$command->getValidationResult()->getErrorMessage(),
485
            self::ERROR_INVALID_COMMAND_CREATED,
486
            null,
487
            $command
488
        );
489
    }
490
    
491
    protected static function _exceptionUnexpectedType(string $type, Mailcode_Commands_Command $command) : Mailcode_Factory_Exception
492
    {
493
        return new Mailcode_Factory_Exception(
494
            'Invalid command class type created.',
495
            sprintf('Excepted type [%s], but created class [%s].', $type, get_class($command)),
496
            self::ERROR_UNEXPECTED_COMMAND_TYPE,
497
            null,
498
            $command
499
        );
500
    }
501
    
502
   /**
503
    * Creates a renderer instance, which can be used to easily
504
    * create and convert commands to strings.
505
    * 
506
    * @return Mailcode_Renderer
507
    */
508
    public static function createRenderer() : Mailcode_Renderer
509
    {
510
        return new Mailcode_Renderer();
511
    }
512
    
513
   /**
514
    * Creates a printer instance, which works like the renderer,
515
    * but outputs the generated strings to standard output.
516
    * 
517
    * @return Mailcode_Printer
518
    */
519
    public static function createPrinter() : Mailcode_Printer
520
    {
521
        return new Mailcode_Printer();
522
    }
523
}
524