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