1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File containing the {@see Mailcode_Commands_Command_ShowDate} class. |
4
|
|
|
* |
5
|
|
|
* @package Mailcode |
6
|
|
|
* @subpackage Commands |
7
|
|
|
* @see Mailcode_Commands_Command_ShowDate |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Mailcode; |
13
|
|
|
|
14
|
|
|
use Mailcode\Interfaces\Commands\Validation\TimezoneInterface; |
15
|
|
|
use Mailcode\Traits\Commands\Validation\TimezoneTrait; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Mailcode command: show a date variable value. |
19
|
|
|
* |
20
|
|
|
* @package Mailcode |
21
|
|
|
* @subpackage Commands |
22
|
|
|
* @author Sebastian Mordziol <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class Mailcode_Commands_Command_ShowDate |
25
|
|
|
extends Mailcode_Commands_ShowBase |
26
|
|
|
implements |
27
|
|
|
TimezoneInterface |
28
|
|
|
{ |
29
|
|
|
use TimezoneTrait; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var Mailcode_Variables_Variable|string|NULL |
33
|
|
|
*/ |
34
|
|
|
private static $defaultTimeZone = null; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The date format string. |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private string $formatString; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var Mailcode_Date_FormatInfo |
44
|
|
|
*/ |
45
|
|
|
private Mailcode_Date_FormatInfo $formatInfo; |
46
|
|
|
|
47
|
|
|
public function getName(): string |
48
|
|
|
{ |
49
|
|
|
return 'showdate'; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function getLabel(): string |
53
|
|
|
{ |
54
|
|
|
return t('Show date variable'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
protected function getValidations(): array |
58
|
|
|
{ |
59
|
|
|
return array( |
60
|
|
|
Mailcode_Interfaces_Commands_Validation_Variable::VALIDATION_NAME_VARIABLE, |
61
|
|
|
'check_format', |
62
|
|
|
TimezoneInterface::VALIDATION_TIMEZONE_NAME |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function init(): void |
67
|
|
|
{ |
68
|
|
|
$this->formatInfo = Mailcode_Factory::createDateInfo(); |
69
|
|
|
$this->formatString = $this->formatInfo->getDefaultFormat(); |
70
|
|
|
|
71
|
|
|
parent::init(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
protected function validateSyntax_check_format(): void |
75
|
|
|
{ |
76
|
|
|
$tokens = $this->requireParams() |
77
|
|
|
->getInfo() |
78
|
|
|
->getStringLiterals(); |
79
|
|
|
|
80
|
|
|
// no format specified? Use the default one. |
81
|
|
|
if (empty($tokens)) { |
82
|
|
|
return; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$format = $tokens[0]->getText(); |
86
|
|
|
|
87
|
|
|
$this->parseFormatString($format); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
private function parseFormatString(string $format): void |
91
|
|
|
{ |
92
|
|
|
$result = $this->formatInfo->validateFormat($format); |
93
|
|
|
|
94
|
|
|
if ($result->isValid()) { |
95
|
|
|
$this->formatString = $format; |
96
|
|
|
return; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$this->validationResult->makeError( |
100
|
|
|
$result->getErrorMessage(), |
101
|
|
|
$result->getCode() |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Retrieves the format string used to format the date. |
107
|
|
|
* |
108
|
|
|
* @return string A PHP compatible date format string. |
109
|
|
|
*/ |
110
|
|
|
public function getFormatString(): string |
111
|
|
|
{ |
112
|
|
|
return $this->formatString; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param string|Mailcode_Variables_Variable|NULL $zone A timezone identifier, e.g. <code>Europe/Paris</code> or a variable containing the zone identifier, or NULL to use the PHP default. |
117
|
|
|
* @return void |
118
|
|
|
*/ |
119
|
|
|
public static function setDefaultTimezone($zone) : void |
120
|
|
|
{ |
121
|
|
|
self::$defaultTimeZone = $zone; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Gets the default time zone used for dates. If not set via |
126
|
|
|
* {@see self::setDefaultTimezone()}, this defaults to PHP's |
127
|
|
|
* default time zone (typically <code>UTC</code> if not changed). |
128
|
|
|
* |
129
|
|
|
* @return string|Mailcode_Variables_Variable |
130
|
|
|
*/ |
131
|
|
|
public static function getDefaultTimezone() |
132
|
|
|
{ |
133
|
|
|
return self::$defaultTimeZone ?? date_default_timezone_get(); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|