1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File containing the trait {@see \Mailcode\Traits\Commands\Validation\TimezoneTrait}. |
4
|
|
|
* |
5
|
|
|
* @package Mailcode |
6
|
|
|
* @subpackage Validation |
7
|
|
|
* @see \Mailcode\Traits\Commands\Validation\TimezoneTrait |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Mailcode\Traits\Commands\Validation; |
13
|
|
|
|
14
|
|
|
use Mailcode\Interfaces\Commands\Validation\TimezoneInterface; |
15
|
|
|
use Mailcode\Mailcode_Commands_Command_ShowDate; |
16
|
|
|
use Mailcode\Mailcode_Commands_Keywords; |
17
|
|
|
use Mailcode\Mailcode_Parser_Statement_Tokenizer_Token; |
18
|
|
|
use Mailcode\Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral; |
19
|
|
|
use Mailcode\Mailcode_Parser_Statement_Tokenizer_Token_Variable; |
20
|
|
|
use Mailcode\Mailcode_Variables_Variable; |
21
|
|
|
use function Mailcode\t; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @package Mailcode |
25
|
|
|
* @subpackage Validation |
26
|
|
|
* @author Olaf Böcker <[email protected]> |
27
|
|
|
* |
28
|
|
|
* @see TimezoneInterface |
29
|
|
|
*/ |
30
|
|
|
trait TimezoneTrait |
31
|
|
|
{ |
32
|
|
|
private ?Mailcode_Parser_Statement_Tokenizer_Token $timezoneToken = null; |
33
|
|
|
|
34
|
|
|
protected function validateSyntax_check_timezone(): void |
35
|
|
|
{ |
36
|
|
|
$this->timezoneToken = $this->requireParams() |
|
|
|
|
37
|
|
|
->getInfo() |
38
|
|
|
->getTokenByParamName(TimezoneInterface::PARAMETER_NAME); |
39
|
|
|
|
40
|
|
|
if ($this->timezoneToken === null) { |
41
|
|
|
return; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if (!$this->timezoneToken instanceof Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral && |
45
|
|
|
!$this->timezoneToken instanceof Mailcode_Parser_Statement_Tokenizer_Token_Variable) { |
46
|
|
|
$this->validationResult->makeError( |
47
|
|
|
t('Invalid timezone:') . ' ' . t('Expected a string or variable.'), |
48
|
|
|
TimezoneInterface::VALIDATION_TIMEZONE_CODE_WRONG_TYPE |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Gets the time zone to use for the command. If none has |
55
|
|
|
* been specified in the original command, the default |
56
|
|
|
* time zone is used as defined via {@see Mailcode_Commands_Command_ShowDate::setDefaultTimezone()}. |
57
|
|
|
* |
58
|
|
|
* @return Mailcode_Parser_Statement_Tokenizer_Token |
59
|
|
|
*/ |
60
|
|
|
public function getTimezoneToken(): Mailcode_Parser_Statement_Tokenizer_Token |
61
|
|
|
{ |
62
|
|
|
if(!isset($this->timezoneToken)) { |
63
|
|
|
$this->timezoneToken = $this->createTimeZoneToken(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $this->timezoneToken; |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Creates the default time zone token on demand. |
71
|
|
|
* |
72
|
|
|
* @return Mailcode_Parser_Statement_Tokenizer_Token |
73
|
|
|
*/ |
74
|
|
|
private function createTimeZoneToken() : Mailcode_Parser_Statement_Tokenizer_Token |
75
|
|
|
{ |
76
|
|
|
$default = Mailcode_Commands_Command_ShowDate::getDefaultTimezone(); |
77
|
|
|
$info = $this->requireParams()->getInfo(); |
78
|
|
|
|
79
|
|
|
if($default instanceof Mailcode_Variables_Variable) { |
80
|
|
|
$token = $info->addVariable($default); |
81
|
|
|
} else { |
82
|
|
|
$token = $info->addStringLiteral($default); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$info->setParamName($token, TimezoneInterface::PARAMETER_NAME); |
86
|
|
|
|
87
|
|
|
return $token; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param Mailcode_Variables_Variable|string|NULL $timezone |
92
|
|
|
* @return $this |
93
|
|
|
*/ |
94
|
|
|
public function setTimezone($timezone) : self |
95
|
|
|
{ |
96
|
|
|
$info = $this->requireParams()->getInfo(); |
97
|
|
|
$token = null; |
98
|
|
|
|
99
|
|
|
$existing = $info->getTokenByParamName(TimezoneInterface::PARAMETER_NAME); |
100
|
|
|
if($existing) { |
101
|
|
|
$info->removeToken($existing); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if($timezone instanceof Mailcode_Variables_Variable) { |
105
|
|
|
$token = $info->addVariable($timezone); |
106
|
|
|
} else if(is_string($timezone)) { |
107
|
|
|
$token = $info->addStringLiteral($timezone); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if($token !== null) { |
111
|
|
|
$info->setParamName($token, TimezoneInterface::PARAMETER_NAME); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$this->timezoneToken = $token; |
115
|
|
|
|
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|