Passed
Push — master ( a60ac2...b0c01a )
by
unknown
09:12
created

IfTranslation::translateContains()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * @package Mailcode
4
 * @subpackage Translator
5
 */
6
7
declare(strict_types=1);
8
9
namespace Mailcode\Translator\Syntax\HubL;
10
11
use Mailcode\Mailcode_Commands_Command_If;
12
use Mailcode\Mailcode_Commands_Command_If_BeginsWith;
13
use Mailcode\Mailcode_Commands_Command_If_BiggerThan;
14
use Mailcode\Mailcode_Commands_Command_If_Command;
15
use Mailcode\Mailcode_Commands_Command_If_Contains;
16
use Mailcode\Mailcode_Commands_Command_If_Empty;
17
use Mailcode\Mailcode_Commands_Command_If_EndsWith;
18
use Mailcode\Mailcode_Commands_Command_If_EqualsNumber;
19
use Mailcode\Mailcode_Commands_Command_If_ListBeginsWith;
20
use Mailcode\Mailcode_Commands_Command_If_ListContains;
21
use Mailcode\Mailcode_Commands_Command_If_ListEndsWith;
22
use Mailcode\Mailcode_Commands_Command_If_ListEquals;
23
use Mailcode\Mailcode_Commands_Command_If_ListNotContains;
24
use Mailcode\Mailcode_Commands_Command_If_NotContains;
25
use Mailcode\Mailcode_Commands_Command_If_NotEmpty;
26
use Mailcode\Mailcode_Commands_Command_If_SmallerThan;
27
use Mailcode\Mailcode_Commands_Command_If_Variable;
28
use Mailcode\Mailcode_Translator_Command_If;
29
use Mailcode\Translator\Syntax\HubL;
30
use Mailcode\Translator\Syntax\HubL\Base\AbstractIfBase;
31
32
/**
33
 * Translates the {@see Mailcode_Commands_Command_If} command to HubL.
34
 *
35
 * @package Mailcode
36
 * @subpackage Translator
37
 * @author Sebastian Mordziol <[email protected]>
38
 */
39
class IfTranslation extends AbstractIfBase implements Mailcode_Translator_Command_If
40
{
41
    protected function getCommandTemplate() : string
42
    {
43
        return '{%% if %s %%}';
44
    }
45
46
    public function translate(Mailcode_Commands_Command_If $command): string
47
    {
48
        return $this->_translate($command);
49
    }
50
51
    protected function translateCommand(Mailcode_Commands_Command_If_Command $command) : string
52
    {
53
        return $this->_translateGeneric($command);
54
    }
55
56
    protected function translateBeginsWith(Mailcode_Commands_Command_If_BeginsWith $command) : string
57
    {
58
        return $this->_translateSearch(
59
            'starts',
60
            $command->getVariable(),
61
            $command->isCaseInsensitive(),
62
            $command->getSearchTerm()
63
        );
64
    }
65
66
    protected function translateEndsWith(Mailcode_Commands_Command_If_EndsWith $command) : string
67
    {
68
        return $this->_translateSearch(
69
            'ends',
70
            $command->getVariable(),
71
            $command->isCaseInsensitive(),
72
            $command->getSearchTerm()
73
        );
74
    }
75
76
    protected function translateBiggerThan(Mailcode_Commands_Command_If_BiggerThan $command) : string
77
    {
78
        return $this->_translateNumberComparison(
79
            $command->getVariable(),
80
            $command->getNumber(),
81
            '>'
82
        );
83
    }
84
85
    protected function translateSmallerThan(Mailcode_Commands_Command_If_SmallerThan $command) : string
86
    {
87
        return $this->_translateNumberComparison(
88
            $command->getVariable(),
89
            $command->getNumber(),
90
            '<'
91
        );
92
    }
93
94
    protected function translateEqualsNumber(Mailcode_Commands_Command_If_EqualsNumber $command) : string
95
    {
96
        return $this->_translateNumberComparison(
97
            $command->getVariable(),
98
            $command->getNumber(),
99
            '=='
100
        );
101
    }
102
103
    protected function translateVariable(Mailcode_Commands_Command_If_Variable $command) : string
104
    {
105
        return $this->_translateVariable(
106
            $command->getVariable(),
107
            $command->getSign(),
108
            $command->getValue(),
109
            $command->isCaseInsensitive()
110
        );
111
    }
112
113
    protected function translateContains(Mailcode_Commands_Command_If_Contains $command) : string
114
    {
115
        return $this->_translateContains(
116
            $command->getVariable(),
117
            $command->isCaseInsensitive(),
118
            false,
119
            $command->getSearchTerms(),
120
            $command->getType()
121
        );
122
    }
123
124
    protected function translateNotContains(Mailcode_Commands_Command_If_NotContains $command) : string
125
    {
126
        return $this->_translateContains(
127
            $command->getVariable(),
128
            $command->isCaseInsensitive(),
129
            false,
130
            $command->getSearchTerms(),
131
            $command->getType()
132
        );
133
    }
134
135
    protected function translateListContains(Mailcode_Commands_Command_If_ListContains $command) : string
136
    {
137
        return $this->_translateContains(
138
            $command->getVariable(),
139
            $command->isCaseInsensitive(),
140
            $command->isRegexEnabled(),
141
            $command->getSearchTerms(),
142
            $command->getType()
143
        );
144
    }
145
146
    protected function translateListEquals(Mailcode_Commands_Command_If_ListEquals $command) : string
147
    {
148
        return $this->_translateContains(
149
            $command->getVariable(),
150
            $command->isCaseInsensitive(),
151
            false,
152
            $command->getSearchTerms(),
153
            $command->getType()
154
        );
155
    }
156
157
    protected function translateListNotContains(Mailcode_Commands_Command_If_ListNotContains $command) : string
158
    {
159
        return $this->_translateContains(
160
            $command->getVariable(),
161
            $command->isCaseInsensitive(),
162
            $command->isRegexEnabled(),
163
            $command->getSearchTerms(),
164
            $command->getType()
165
        );
166
    }
167
168
    protected function translateListBeginsWith(Mailcode_Commands_Command_If_ListBeginsWith $command) : string
169
    {
170
        return $this->_translateContains(
171
            $command->getVariable(),
172
            $command->isCaseInsensitive(),
173
            $command->isRegexEnabled(),
174
            $command->getSearchTerms(),
175
            $command->getType()
176
        );
177
    }
178
179
    protected function translateListEndsWith(Mailcode_Commands_Command_If_ListEndsWith $command) : string
180
    {
181
        return $this->_translateContains(
182
            $command->getVariable(),
183
            $command->isCaseInsensitive(),
184
            $command->isRegexEnabled(),
185
            $command->getSearchTerms(),
186
            $command->getType()
187
        );
188
    }
189
190
    protected function translateEmpty(Mailcode_Commands_Command_If_Empty $command) : string
191
    {
192
        return $this->_translateEmpty($command->getVariable(), false);
193
    }
194
195
    protected function translateNotEmpty(Mailcode_Commands_Command_If_NotEmpty $command) : string
196
    {
197
        return $this->_translateEmpty($command->getVariable(), true);
198
    }
199
}
200