1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File containing the {@see Mailcode_Translator_Syntax_ApacheVelocity_Base_AbstractIf} class. |
4
|
|
|
* |
5
|
|
|
* @package Mailcode |
6
|
|
|
* @subpackage Translator |
7
|
|
|
* @see Mailcode_Translator_Syntax_ApacheVelocity_Base_AbstractIf |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Mailcode; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Abstract base class for the IF/ELSEIF command translation classes. |
16
|
|
|
* |
17
|
|
|
* @package Mailcode |
18
|
|
|
* @subpackage Translator |
19
|
|
|
* @author Sebastian Mordziol <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
abstract class Mailcode_Translator_Syntax_ApacheVelocity_Base_AbstractIf extends Mailcode_Translator_Syntax_ApacheVelocity |
22
|
|
|
{ |
23
|
|
|
const ERROR_CANNOT_GET_KEYWORD_SIGN = 60801; |
24
|
|
|
const ERROR_INVALID_KEYWORD_COMMAND_TYPE = 60802; |
25
|
|
|
|
26
|
|
|
abstract protected function getCommandTemplate() : string; |
27
|
|
|
|
28
|
|
|
protected function getIfType(Mailcode_Commands_IfBase $command) : string |
29
|
|
|
{ |
30
|
|
|
$parts = explode('_', get_class($command)); |
31
|
|
|
|
32
|
|
|
return array_pop($parts); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected function translateBody(Mailcode_Commands_IfBase $command) : string |
36
|
|
|
{ |
37
|
|
|
// The command's getID() method will return "If" for all flavors |
38
|
|
|
// of the command. We use a custom method to determine the actual |
39
|
|
|
// IF type. |
40
|
|
|
$method = 'translate'.$this->getIfType($command); |
41
|
|
|
|
42
|
|
|
if(method_exists($this, $method)) |
43
|
|
|
{ |
44
|
|
|
return strval($this->$method($command)); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return ''; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function _translate(Mailcode_Commands_IfBase $command): string |
51
|
|
|
{ |
52
|
|
|
$body = $this->translateBody($command); |
53
|
|
|
|
54
|
|
|
$keywords = $command->getLogicKeywords()->getKeywords(); |
55
|
|
|
|
56
|
|
|
foreach($keywords as $keyword) |
57
|
|
|
{ |
58
|
|
|
$keyCommand = $keyword->getCommand(); |
59
|
|
|
|
60
|
|
|
if($keyCommand instanceof Mailcode_Commands_IfBase) |
61
|
|
|
{ |
62
|
|
|
$body .= ' '.$this->getSign($keyword).' '.$this->translateBody($keyCommand); |
63
|
|
|
} |
64
|
|
|
else |
65
|
|
|
{ |
66
|
|
|
throw new Mailcode_Exception( |
67
|
|
|
'Keyword command type does not match expected base class.', |
68
|
|
|
sprintf( |
69
|
|
|
'Expected instance of [%s], got [%s].', |
70
|
|
|
Mailcode_Commands_IfBase::class, |
71
|
|
|
get_class($keyCommand) |
72
|
|
|
), |
73
|
|
|
self::ERROR_INVALID_KEYWORD_COMMAND_TYPE |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return sprintf($this->getCommandTemplate(), $body); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
protected function getSign(Mailcode_Commands_LogicKeywords_Keyword $keyword) : string |
82
|
|
|
{ |
83
|
|
|
switch($keyword->getName()) |
84
|
|
|
{ |
85
|
|
|
case 'and': |
86
|
|
|
return '&&'; |
87
|
|
|
|
88
|
|
|
case 'or': |
89
|
|
|
return '||'; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
throw new Mailcode_Exception( |
93
|
|
|
'Unknown keyword name', |
94
|
|
|
sprintf( |
95
|
|
|
'The keyword name [%s] is not known and cannot be translated to a velocity sign.', |
96
|
|
|
$keyword->getName() |
97
|
|
|
), |
98
|
|
|
self::ERROR_CANNOT_GET_KEYWORD_SIGN |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
protected function _translateEmpty(Mailcode_Variables_Variable $variable, bool $notEmpty) : string |
103
|
|
|
{ |
104
|
|
|
$sign = ''; |
105
|
|
|
|
106
|
|
|
if($notEmpty) |
107
|
|
|
{ |
108
|
|
|
$sign = '!'; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return sprintf( |
112
|
|
|
'%s$StringUtils.isEmpty(%s)', |
113
|
|
|
$sign, |
114
|
|
|
$variable->getFullName() |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
protected function _translateGeneric(Mailcode_Commands_IfBase $command) : string |
119
|
|
|
{ |
120
|
|
|
$params = $command->getParams(); |
121
|
|
|
|
122
|
|
|
if(!$params) |
|
|
|
|
123
|
|
|
{ |
124
|
|
|
return ''; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $params->getNormalized(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
protected function _translateVariable(Mailcode_Variables_Variable $variable, string $comparator, string $value) : string |
131
|
|
|
{ |
132
|
|
|
return sprintf( |
133
|
|
|
'%s %s %s', |
134
|
|
|
$variable->getFullName(), |
135
|
|
|
$comparator, |
136
|
|
|
$value |
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* |
142
|
|
|
* @param Mailcode_Variables_Variable $variable |
143
|
|
|
* @param bool $caseSensitive |
144
|
|
|
* @param Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral[] $searchTerms |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
|
|
protected function _translateContains(Mailcode_Variables_Variable $variable, bool $caseSensitive, array $searchTerms) : string |
148
|
|
|
{ |
149
|
|
|
$parts = array(); |
150
|
|
|
$varName = $variable->getFullName(); |
151
|
|
|
|
152
|
|
|
$opts = 's'; |
153
|
|
|
if($caseSensitive) |
154
|
|
|
{ |
155
|
|
|
$opts = 'is'; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
foreach($searchTerms as $token) |
159
|
|
|
{ |
160
|
|
|
$parts[] = sprintf( |
161
|
|
|
'%s.matches("(?%s)%s")', |
162
|
|
|
$varName, |
163
|
|
|
$opts, |
164
|
|
|
$this->filterRegexString(trim($token->getNormalized(), '"')) |
165
|
|
|
); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return implode(' || ', $parts); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
protected function _translateSearch(string $mode, Mailcode_Variables_Variable $variable, bool $caseSensitive, string $searchTerm) : string |
172
|
|
|
{ |
173
|
|
|
$method = $mode.'With'; |
174
|
|
|
if($caseSensitive) |
175
|
|
|
{ |
176
|
|
|
$method = $mode.'WithIgnoreCase'; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return sprintf( |
180
|
|
|
'$StringUtils.%s(%s, "%s")', |
181
|
|
|
$method, |
182
|
|
|
$variable->getFullName(), |
183
|
|
|
trim($searchTerm, '"') |
184
|
|
|
); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|