|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* File containing the {@see Mailcode_Commands_Command_SetVariable} class. |
|
4
|
|
|
* |
|
5
|
|
|
* @package Mailcode |
|
6
|
|
|
* @subpackage Commands |
|
7
|
|
|
* @see Mailcode_Commands_Command_SetVariable |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Mailcode; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Mailcode command: set a variable value. |
|
16
|
|
|
* |
|
17
|
|
|
* @package Mailcode |
|
18
|
|
|
* @subpackage Commands |
|
19
|
|
|
* @author Sebastian Mordziol <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
class Mailcode_Commands_Command_SetVariable extends Mailcode_Commands_Command implements Mailcode_Commands_Command_Type_Standalone |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var Mailcode_Parser_Statement_Tokenizer_Token_Variable|NULL |
|
25
|
|
|
*/ |
|
26
|
|
|
private $variableToken; |
|
27
|
|
|
|
|
28
|
|
|
public function getName() : string |
|
29
|
|
|
{ |
|
30
|
|
|
return 'setvar'; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function getLabel() : string |
|
34
|
|
|
{ |
|
35
|
|
|
return t('Set variable value'); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function supportsType(): bool |
|
39
|
|
|
{ |
|
40
|
|
|
return false; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function getDefaultType() : string |
|
44
|
|
|
{ |
|
45
|
|
|
return ''; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function requiresParameters(): bool |
|
49
|
|
|
{ |
|
50
|
|
|
return true; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function supportsLogicKeywords() : bool |
|
54
|
|
|
{ |
|
55
|
|
|
return false; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
protected function getValidations() : array |
|
59
|
|
|
{ |
|
60
|
|
|
return array( |
|
61
|
|
|
'variable', |
|
62
|
|
|
'operand', |
|
63
|
|
|
'assignment' |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function generatesContent() : bool |
|
68
|
|
|
{ |
|
69
|
|
|
return false; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
protected function validateSyntax_variable() : void |
|
73
|
|
|
{ |
|
74
|
|
|
$val = $this->validator->createVariable()->setIndex(0); |
|
75
|
|
|
|
|
76
|
|
|
if($val->isValid()) |
|
77
|
|
|
{ |
|
78
|
|
|
$this->variableToken = $val->getToken(); |
|
79
|
|
|
} |
|
80
|
|
|
else |
|
81
|
|
|
{ |
|
82
|
|
|
$this->validationResult->makeError( |
|
83
|
|
|
'The first parameter must be a variable name.', |
|
84
|
|
|
Mailcode_Commands_CommonConstants::VALIDATION_VARIABLE_MISSING |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
protected function validateSyntax_operand() : void |
|
90
|
|
|
{ |
|
91
|
|
|
$val = $this->validator->createOperand('=')->setIndex(1); |
|
92
|
|
|
|
|
93
|
|
|
if(!$val->isValid()) |
|
94
|
|
|
{ |
|
95
|
|
|
$this->validationResult->makeError( |
|
96
|
|
|
t('The second parameter must be an equals sign (%1$s).', '<code>=</code>'), |
|
97
|
|
|
Mailcode_Commands_CommonConstants::VALIDATION_INVALID_OPERAND |
|
98
|
|
|
); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
protected function validateSyntax_assignment() : void |
|
103
|
|
|
{ |
|
104
|
|
|
$tokens = $this->getAssignmentTokens(); |
|
105
|
|
|
|
|
106
|
|
|
if(empty($tokens)) |
|
107
|
|
|
{ |
|
108
|
|
|
$this->validationResult->makeError( |
|
109
|
|
|
t('No value assigned to the variable.'), |
|
110
|
|
|
Mailcode_Commands_CommonConstants::VALIDATION_VALUE_MISSING |
|
111
|
|
|
); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function getVariable() : Mailcode_Variables_Variable |
|
116
|
|
|
{ |
|
117
|
|
|
if($this->variableToken instanceof Mailcode_Parser_Statement_Tokenizer_Token_Variable) |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->variableToken->getVariable(); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
throw new Mailcode_Exception( |
|
123
|
|
|
'No variable found.', |
|
124
|
|
|
'Statement does not start with a variable: ['.$this->paramsString.']', |
|
125
|
|
|
Mailcode_Commands_CommonConstants::ERROR_NO_VARIABLE_AVAILABLE |
|
126
|
|
|
); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @return Mailcode_Parser_Statement_Tokenizer_Token[] |
|
131
|
|
|
*/ |
|
132
|
|
|
public function getAssignmentTokens() : array |
|
133
|
|
|
{ |
|
134
|
|
|
$params = $this->params->getInfo()->getTokens(); |
|
135
|
|
|
|
|
136
|
|
|
array_shift($params); // variable |
|
137
|
|
|
array_shift($params); // equals sign |
|
138
|
|
|
|
|
139
|
|
|
return $params; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
public function getAssignmentString() : string |
|
143
|
|
|
{ |
|
144
|
|
|
$tokens = $this->getAssignmentTokens(); |
|
145
|
|
|
|
|
146
|
|
|
$items = array(); |
|
147
|
|
|
|
|
148
|
|
|
foreach($tokens as $token) |
|
149
|
|
|
{ |
|
150
|
|
|
$items[] = $token->getNormalized(); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
return implode(' ', $items); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|