|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* File containing the {@see Mailcode_Translator_Syntax_ApacheVelocity_ShowDate} class. |
|
4
|
|
|
* |
|
5
|
|
|
* @package Mailcode |
|
6
|
|
|
* @subpackage Translator |
|
7
|
|
|
* @see Mailcode_Translator_Syntax_ApacheVelocity_ShowDate |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Mailcode; |
|
13
|
|
|
|
|
14
|
|
|
use AppUtils\ConvertHelper; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Translates the "ShowDate" command to Apache Velocity. |
|
18
|
|
|
* |
|
19
|
|
|
* @package Mailcode |
|
20
|
|
|
* @subpackage Translator |
|
21
|
|
|
* @author Sebastian Mordziol <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
class Mailcode_Translator_Syntax_ApacheVelocity_ShowDate extends Mailcode_Translator_Syntax_ApacheVelocity implements Mailcode_Translator_Command_ShowDate |
|
24
|
|
|
{ |
|
25
|
|
|
public const ERROR_UNKNOWN_DATE_FORMAT_CHARACTER = 55501; |
|
26
|
|
|
public const ERROR_UNHANDLED_TIME_ZONE_TOKEN_TYPE = 55502; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* The date format used in the date variable. This is used to convert |
|
30
|
|
|
* the native date to the format specified in the variable command. |
|
31
|
|
|
*/ |
|
32
|
|
|
public const DEFAULT_INTERNAL_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX"; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string[]string |
|
36
|
|
|
*/ |
|
37
|
|
|
private $charTable = array( |
|
38
|
|
|
'd' => 'dd', |
|
39
|
|
|
'j' => 'd', |
|
40
|
|
|
'm' => 'MM', |
|
41
|
|
|
'n' => 'M', |
|
42
|
|
|
'Y' => 'yyyy', |
|
43
|
|
|
'y' => 'yy', |
|
44
|
|
|
'H' => 'H', |
|
45
|
|
|
'i' => 'm', |
|
46
|
|
|
's' => 's', |
|
47
|
|
|
'.' => '.', |
|
48
|
|
|
':' => ':', |
|
49
|
|
|
'-' => '-', |
|
50
|
|
|
'/' => '/', |
|
51
|
|
|
' ' => ' ' |
|
52
|
|
|
); |
|
53
|
|
|
|
|
54
|
|
|
public function getInternalFormat(Mailcode_Commands_Command_ShowDate $command): string |
|
55
|
|
|
{ |
|
56
|
|
|
$internalFormat = $command->getTranslationParam('internal_format'); |
|
57
|
|
|
|
|
58
|
|
|
if (is_string($internalFormat) && !empty($internalFormat)) { |
|
59
|
|
|
return $internalFormat; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return self::DEFAULT_INTERNAL_FORMAT; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function translate(Mailcode_Commands_Command_ShowDate $command): string |
|
66
|
|
|
{ |
|
67
|
|
|
$statement = sprintf( |
|
68
|
|
|
'time.input("%s", $%s).output("%s")%s', |
|
69
|
|
|
$this->getInternalFormat($command), |
|
70
|
|
|
undollarize($command->getVariableName()), |
|
71
|
|
|
$this->resolveJavaFormat($command->getFormatString()), |
|
72
|
|
|
$this->resolveTimeZoneFormat($command) |
|
73
|
|
|
); |
|
74
|
|
|
|
|
75
|
|
|
return $this->renderVariableEncodings($command, $statement); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
private function resolveTimeZoneFormat(Mailcode_Commands_Command_ShowDate $command) : string |
|
79
|
|
|
{ |
|
80
|
|
|
$token = $command->getTimezoneToken(); |
|
81
|
|
|
|
|
82
|
|
|
if ($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral) { |
|
83
|
|
|
return sprintf('.zone("%s")', $token->getText()); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
if ($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_Variable) { |
|
87
|
|
|
return sprintf('.zone(%s)', $token->getVariable()->getFullName()); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
throw new Mailcode_Translator_Exception( |
|
91
|
|
|
'Unknown time zone type.', |
|
92
|
|
|
sprintf( |
|
93
|
|
|
'The time zone token type is unhandled: [%s].', |
|
94
|
|
|
get_class($token) |
|
95
|
|
|
), |
|
96
|
|
|
self::ERROR_UNHANDLED_TIME_ZONE_TOKEN_TYPE |
|
97
|
|
|
); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
private function resolveJavaFormat(string $formatString): string |
|
101
|
|
|
{ |
|
102
|
|
|
$chars = ConvertHelper::string2array($formatString); |
|
103
|
|
|
$result = array(); |
|
104
|
|
|
|
|
105
|
|
|
foreach ($chars as $char) { |
|
106
|
|
|
if (!isset($this->charTable[$char])) { |
|
107
|
|
|
throw new Mailcode_Translator_Exception( |
|
108
|
|
|
'Unknown date format string character', |
|
109
|
|
|
sprintf( |
|
110
|
|
|
'No translation for character %s available.', |
|
111
|
|
|
ConvertHelper::hidden2visible($char) |
|
112
|
|
|
), |
|
113
|
|
|
self::ERROR_UNKNOWN_DATE_FORMAT_CHARACTER |
|
114
|
|
|
); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
$result[] = $this->charTable[$char]; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return implode('', $result); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|