|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* File containing the {@see Mailcode_Traits_Commands_Validation_Variable} trait. |
|
4
|
|
|
* |
|
5
|
|
|
* @package Mailcode |
|
6
|
|
|
* @subpackage Validation |
|
7
|
|
|
* @see Mailcode_Traits_Commands_Validation_Variable |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Mailcode; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Command validation drop-in: checks for the presence |
|
16
|
|
|
* of a variable name. Will accept the first variable |
|
17
|
|
|
* it finds. |
|
18
|
|
|
* |
|
19
|
|
|
* @package Mailcode |
|
20
|
|
|
* @subpackage Validation |
|
21
|
|
|
* @author Sebastian Mordziol <[email protected]> |
|
22
|
|
|
* |
|
23
|
|
|
* @property Mailcode_Parser_Statement_Validator $validator |
|
24
|
|
|
* |
|
25
|
|
|
* @see Mailcode_Interfaces_Commands_Validation_Variable |
|
26
|
|
|
*/ |
|
27
|
|
|
trait Mailcode_Traits_Commands_Validation_Variable |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var Mailcode_Parser_Statement_Tokenizer_Token_Variable|NULL |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $variableToken; |
|
33
|
|
|
|
|
34
|
|
|
protected function validateSyntax_variable(): void |
|
35
|
|
|
{ |
|
36
|
|
|
$var = $this->validator->createVariable(); |
|
37
|
|
|
|
|
38
|
|
|
if ($var->isValid()) { |
|
39
|
|
|
$this->variableToken = $var->getToken(); |
|
40
|
|
|
} else { |
|
41
|
|
|
$this->validationResult->makeError( |
|
42
|
|
|
t('No variable has been specified.'), |
|
43
|
|
|
Mailcode_Commands_CommonConstants::VALIDATION_VARIABLE_MISSING |
|
44
|
|
|
); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
protected function validateSyntax_variable_optional(): void |
|
49
|
|
|
{ |
|
50
|
|
|
$var = $this->validator->createVariable(); |
|
51
|
|
|
|
|
52
|
|
|
if ($var->isValid()) { |
|
53
|
|
|
$this->variableToken = $var->getToken(); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function getVariableToken(): Mailcode_Parser_Statement_Tokenizer_Token_Variable |
|
58
|
|
|
{ |
|
59
|
|
|
if (isset($this->variableToken)) { |
|
60
|
|
|
return $this->variableToken; |
|
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
throw new Mailcode_Exception( |
|
64
|
|
|
'No variable token available', |
|
65
|
|
|
null, |
|
66
|
|
|
Mailcode_Commands_CommonConstants::ERROR_NO_VARIABLE_AVAILABLE |
|
67
|
|
|
); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Retrieves the variable being compared. |
|
72
|
|
|
* |
|
73
|
|
|
* @return Mailcode_Variables_Variable |
|
74
|
|
|
* @throws Mailcode_Exception |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getVariable(): Mailcode_Variables_Variable |
|
77
|
|
|
{ |
|
78
|
|
|
if (isset($this->variableToken)) { |
|
79
|
|
|
return $this->variableToken->getVariable(); |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
throw new Mailcode_Exception( |
|
83
|
|
|
'No variable available', |
|
84
|
|
|
null, |
|
85
|
|
|
Mailcode_Commands_CommonConstants::ERROR_NO_VARIABLE_AVAILABLE |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function getVariableName(): string |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->getVariable()->getFullName(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Checks whether the command is nested in a loop (FOR) command. |
|
96
|
|
|
* |
|
97
|
|
|
* @return bool |
|
98
|
|
|
*/ |
|
99
|
|
|
public function isInLoop(): bool |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->getLoopCommand() !== null; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Retrieves the command's parent loop command, if any. |
|
106
|
|
|
* |
|
107
|
|
|
* @return Mailcode_Commands_Command_For|NULL |
|
108
|
|
|
*/ |
|
109
|
|
|
public function getLoopCommand(): ?Mailcode_Commands_Command_For |
|
110
|
|
|
{ |
|
111
|
|
|
return $this->findLoopRecursive($this); |
|
|
|
|
|
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Recursively tries to find a loop command in the command's |
|
116
|
|
|
* parent commands. Goes up the whole ancestry if need be. |
|
117
|
|
|
* |
|
118
|
|
|
* @param Mailcode_Commands_Command $subject |
|
119
|
|
|
* @return Mailcode_Commands_Command_For|null |
|
120
|
|
|
*/ |
|
121
|
|
|
protected function findLoopRecursive(Mailcode_Commands_Command $subject): ?Mailcode_Commands_Command_For |
|
122
|
|
|
{ |
|
123
|
|
|
$parent = $subject->getParent(); |
|
124
|
|
|
|
|
125
|
|
|
if ($parent === null) { |
|
126
|
|
|
return null; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
if ($parent instanceof Mailcode_Commands_Command_For) { |
|
130
|
|
|
return $parent; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
return $this->findLoopRecursive($parent); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function hasVariable(): bool |
|
137
|
|
|
{ |
|
138
|
|
|
return isset($this->variableToken); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|