Mailcode_Factory_Instantiator   A
last analyzed

Complexity

Total Complexity 39

Size/Duplication

Total Lines 337
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 117
c 5
b 0
f 0
dl 0
loc 337
rs 9.28
wmc 39

23 Methods

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