|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* File containing the {@see \Mailcode\Mailcode_Translator_Syntax_ApacheVelocity_ShowNumber} class. |
|
4
|
|
|
* |
|
5
|
|
|
* @package Mailcode |
|
6
|
|
|
* @subpackage Translator |
|
7
|
|
|
* @see \Mailcode\Mailcode_Translator_Syntax_ApacheVelocity_ShowNumber |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Mailcode; |
|
13
|
|
|
|
|
14
|
|
|
use AppUtils\ConvertHelper; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Translates the "ShowNumber" command to Apache Velocity. |
|
18
|
|
|
* |
|
19
|
|
|
* @package Mailcode |
|
20
|
|
|
* @subpackage Translator |
|
21
|
|
|
* @author Sebastian Mordziol <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
class Mailcode_Translator_Syntax_ApacheVelocity_ShowNumber extends Mailcode_Translator_Syntax_ApacheVelocity implements Mailcode_Translator_Command_ShowNumber |
|
24
|
|
|
{ |
|
25
|
|
|
public function translate(Mailcode_Commands_Command_ShowNumber $command): string |
|
26
|
|
|
{ |
|
27
|
|
|
$template = <<<'EOD' |
|
28
|
|
|
number.format('%s', $number.toNumber('#.####', $%s.replace(',', '.'), 'en_US'), '%s') |
|
29
|
|
|
EOD; |
|
30
|
|
|
|
|
31
|
|
|
$varName = ltrim($command->getVariableName(), '$'); |
|
32
|
|
|
$formatInfo = $command->getFormatInfo(); |
|
33
|
|
|
$javaFormat = $this->translateFormat($formatInfo); |
|
34
|
|
|
$locale = $this->getLocale($formatInfo); |
|
35
|
|
|
|
|
36
|
|
|
$statement = sprintf( |
|
37
|
|
|
$template, |
|
38
|
|
|
$javaFormat, |
|
39
|
|
|
$varName, |
|
40
|
|
|
$locale |
|
41
|
|
|
); |
|
42
|
|
|
|
|
43
|
|
|
if($command->isURLEncoded()) |
|
44
|
|
|
{ |
|
45
|
|
|
return sprintf( |
|
46
|
|
|
'${esc.url($%s)}', |
|
47
|
|
|
$statement |
|
48
|
|
|
); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return sprintf( |
|
52
|
|
|
'${%s}', |
|
53
|
|
|
$statement |
|
54
|
|
|
); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* en_US: 100,000.00 |
|
59
|
|
|
* de_DE: 100.000,00 |
|
60
|
|
|
* fr_FR: 100 000,00 |
|
61
|
|
|
* |
|
62
|
|
|
* @var array<string,string> |
|
63
|
|
|
*/ |
|
64
|
|
|
protected $typeLocales = array( |
|
65
|
|
|
// No separators at all |
|
66
|
|
|
'__' => 'en_US', |
|
67
|
|
|
|
|
68
|
|
|
// International variants |
|
69
|
|
|
',.' => 'en_US', |
|
70
|
|
|
'.,' => 'de_DE', |
|
71
|
|
|
' ,' => 'fr_FR', |
|
72
|
|
|
|
|
73
|
|
|
// No thousands separator |
|
74
|
|
|
'_.' => 'en_US', |
|
75
|
|
|
'_,' => 'de_DE', |
|
76
|
|
|
|
|
77
|
|
|
// Do decimals separator |
|
78
|
|
|
',_' => 'en_US', |
|
79
|
|
|
'._' => 'de_DE', |
|
80
|
|
|
' _' => 'fr_FR' |
|
81
|
|
|
); |
|
82
|
|
|
|
|
83
|
|
|
private function getLocale(Mailcode_Number_Info $format) : string |
|
84
|
|
|
{ |
|
85
|
|
|
$th = $format->getThousandsSeparator(); |
|
86
|
|
|
$dc = $format->getDecimalsSeparator(); |
|
87
|
|
|
|
|
88
|
|
|
if ($th === '') { |
|
89
|
|
|
$th = '_'; |
|
90
|
|
|
} |
|
91
|
|
|
if ($dc === '') { |
|
92
|
|
|
$dc = '_'; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$type = $th . $dc; |
|
96
|
|
|
|
|
97
|
|
|
if (isset($this->typeLocales[$type])) { |
|
98
|
|
|
return $this->typeLocales[$type]; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return 'en_US'; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
private function translateFormat(Mailcode_Number_Info $format) : string |
|
105
|
|
|
{ |
|
106
|
|
|
$result = '#'; |
|
107
|
|
|
|
|
108
|
|
|
if($format->hasThousandsSeparator()) |
|
109
|
|
|
{ |
|
110
|
|
|
$result = '#,###'; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
if($format->hasDecimals()) |
|
114
|
|
|
{ |
|
115
|
|
|
$result .= '.'.str_repeat('#', $format->getDecimals()); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return $result; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|