1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File containing the {@see Mailcode_Translator_Syntax_ApacheVelocity} class. |
4
|
|
|
* |
5
|
|
|
* @package Mailcode |
6
|
|
|
* @subpackage Translator |
7
|
|
|
* @see Mailcode_Translator_Syntax_ApacheVelocity |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Mailcode; |
13
|
|
|
|
14
|
|
|
use Mailcode\Interfaces\Commands\EncodableInterface; |
15
|
|
|
use Mailcode\Interfaces\Commands\Validation\URLEncodingInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Abstract base class for apache velocity command translation classes. |
19
|
|
|
* |
20
|
|
|
* @package Mailcode |
21
|
|
|
* @subpackage Translator |
22
|
|
|
* @author Sebastian Mordziol <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
abstract class Mailcode_Translator_Syntax_ApacheVelocity extends Mailcode_Translator_Command |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var string[] |
28
|
|
|
*/ |
29
|
|
|
private array $regexSpecialChars = array( |
30
|
|
|
'?', |
31
|
|
|
'.', |
32
|
|
|
'[', |
33
|
|
|
']', |
34
|
|
|
'|', |
35
|
|
|
'{', |
36
|
|
|
'}', |
37
|
|
|
'$', |
38
|
|
|
'*', |
39
|
|
|
'^', |
40
|
|
|
'+', |
41
|
|
|
'<', |
42
|
|
|
'>', |
43
|
|
|
'(', |
44
|
|
|
')' |
45
|
|
|
); |
46
|
|
|
|
47
|
|
|
public function getLabel(): string |
48
|
|
|
{ |
49
|
|
|
return 'Apache Velocity'; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Filters the string for use in an Apache Velocity (Java) |
54
|
|
|
* regex string: escapes all special characters. |
55
|
|
|
* |
56
|
|
|
* Velocity does its own escaping, so no need to escape special |
57
|
|
|
* characters as if they were a javascript string. |
58
|
|
|
* |
59
|
|
|
* @param string $string |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
public function filterRegexString(string $string) : string |
63
|
|
|
{ |
64
|
|
|
// Special case: previously escaped quotes. |
65
|
|
|
// To avoid modifying them, we strip them out. |
66
|
|
|
$string = str_replace('\\"', 'ESCQUOTE', $string); |
67
|
|
|
|
68
|
|
|
// Any other existing backslashes in the string |
69
|
|
|
// have to be double-escaped, giving four |
70
|
|
|
// backslashes in the java regex. |
71
|
|
|
$string = str_replace('\\', '\\\\', $string); |
72
|
|
|
|
73
|
|
|
// All other special characters have to be escaped |
74
|
|
|
// with two backslashes. |
75
|
|
|
foreach($this->regexSpecialChars as $char) |
76
|
|
|
{ |
77
|
|
|
$string = str_replace($char, '\\'.$char, $string); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// Restore the escaped quotes, which stay escaped |
81
|
|
|
// with a single backslash. |
82
|
|
|
$string = str_replace('ESCQUOTE', '\\"', $string); |
83
|
|
|
|
84
|
|
|
return $string; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
protected function renderVariableEncodings(Mailcode_Commands_Command $command, string $varName) : string |
88
|
|
|
{ |
89
|
|
|
if(!$command instanceof EncodableInterface || !$command->hasActiveEncodings()) |
90
|
|
|
{ |
91
|
|
|
return sprintf( |
92
|
|
|
'${%s}', |
93
|
|
|
$varName |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $this->renderEncodings($command, '$'.$varName); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function renderNumberFormat(string $varName, Mailcode_Number_Info $numberInfo, bool $absolute) : string |
101
|
|
|
{ |
102
|
|
|
$varName = '$'.ltrim($varName, '$'); |
103
|
|
|
|
104
|
|
|
if($absolute) |
105
|
|
|
{ |
106
|
|
|
$varName = sprintf('${price.abs(%s)}', $varName); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return sprintf( |
110
|
|
|
"price.format(%s, %s, '%s', '%s')", |
111
|
|
|
$varName, |
112
|
|
|
$numberInfo->getDecimals(), |
113
|
|
|
$numberInfo->getDecimalsSeparator(), |
114
|
|
|
$numberInfo->getThousandsSeparator() |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function renderStringToNumber(string $varName) : string |
119
|
|
|
{ |
120
|
|
|
$varName = ltrim($varName, '$'); |
121
|
|
|
|
122
|
|
|
return sprintf( |
123
|
|
|
'$price.toNumber(%s)', |
124
|
|
|
'$'.$varName |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function renderQuotedValue(string $value) : string |
129
|
|
|
{ |
130
|
|
|
return sprintf( |
131
|
|
|
'"%s"', |
132
|
|
|
str_replace('"', '\"', $value) |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function getSyntaxName() : string |
137
|
|
|
{ |
138
|
|
|
return 'ApacheVelocity'; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @var array<string,string> |
143
|
|
|
*/ |
144
|
|
|
private array $encodingTemplates = array( |
145
|
|
|
Mailcode_Commands_Keywords::TYPE_URLENCODE => '${esc.url(%s)}', |
146
|
|
|
Mailcode_Commands_Keywords::TYPE_URLDECODE => '${esc.unurl(%s)}', |
147
|
|
|
Mailcode_Commands_Keywords::TYPE_IDN_ENCODE => '${esc.idn(%s)}', |
148
|
|
|
Mailcode_Commands_Keywords::TYPE_IDN_DECODE => '${esc.unidn(%s)}' |
149
|
|
|
); |
150
|
|
|
|
151
|
|
|
protected function renderEncodings(EncodableInterface $command, string $statement) : string |
152
|
|
|
{ |
153
|
|
|
$encodings = $command->getActiveEncodings(); |
154
|
|
|
$result = $statement; |
155
|
|
|
|
156
|
|
|
foreach($encodings as $encoding) |
157
|
|
|
{ |
158
|
|
|
$result = $this->renderEncoding($encoding, $result); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $result; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
protected function renderEncoding(string $keyword, $result) : string |
165
|
|
|
{ |
166
|
|
|
$template = $this->encodingTemplates[$keyword] ?? '%s'; |
167
|
|
|
|
168
|
|
|
return sprintf($template, $result); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|