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
|
|
|
trait Mailcode_Traits_Commands_Validation_Variable |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var Mailcode_Parser_Statement_Tokenizer_Token_Variable|NULL |
29
|
|
|
*/ |
30
|
|
|
protected $variableToken; |
31
|
|
|
|
32
|
|
|
protected function validateSyntax_variable() : void |
33
|
|
|
{ |
34
|
|
|
$var = $this->validator->createVariable(); |
35
|
|
|
|
36
|
|
|
if($var->isValid()) |
37
|
|
|
{ |
38
|
|
|
$this->variableToken = $var->getToken(); |
39
|
|
|
} |
40
|
|
|
else |
41
|
|
|
{ |
42
|
|
|
$this->validationResult->makeError( |
43
|
|
|
t('No variable has been specified.'), |
44
|
|
|
Mailcode_Commands_CommonConstants::VALIDATION_VARIABLE_MISSING |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Retrieves the variable being compared. |
51
|
|
|
* |
52
|
|
|
* @return Mailcode_Variables_Variable |
53
|
|
|
*/ |
54
|
|
|
public function getVariable() : Mailcode_Variables_Variable |
55
|
|
|
{ |
56
|
|
|
if($this->variableToken instanceof Mailcode_Parser_Statement_Tokenizer_Token_Variable) |
57
|
|
|
{ |
58
|
|
|
return $this->variableToken->getVariable(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
throw new Mailcode_Exception( |
62
|
|
|
'No variable available', |
63
|
|
|
null, |
64
|
|
|
Mailcode_Commands_CommonConstants::ERROR_NO_VARIABLE_AVAILABLE |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function getVariableName() : string |
69
|
|
|
{ |
70
|
|
|
return $this->getVariable()->getFullName(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Checks whether the command is nested in a loop (FOR) command. |
75
|
|
|
* |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
|
|
public function isInLoop() : bool |
79
|
|
|
{ |
80
|
|
|
return $this->getLoopCommand() !== null; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Retrieves the command's parent loop command, if any. |
85
|
|
|
* |
86
|
|
|
* @return Mailcode_Commands_Command_For|NULL |
87
|
|
|
*/ |
88
|
|
|
public function getLoopCommand() : ?Mailcode_Commands_Command_For |
89
|
|
|
{ |
90
|
|
|
return $this->findLoopRecursive($this); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Recursively tries to find a loop command in the command's |
95
|
|
|
* parent commands. Goes up the whole ancestry if need be. |
96
|
|
|
* |
97
|
|
|
* @param Mailcode_Commands_Command $subject |
98
|
|
|
* @return Mailcode_Commands_Command_For|null |
99
|
|
|
*/ |
100
|
|
|
protected function findLoopRecursive(Mailcode_Commands_Command $subject) : ?Mailcode_Commands_Command_For |
101
|
|
|
{ |
102
|
|
|
$parent = $subject->getParent(); |
103
|
|
|
|
104
|
|
|
if($parent === null) |
105
|
|
|
{ |
106
|
|
|
return null; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if($parent instanceof Mailcode_Commands_Command_For) |
110
|
|
|
{ |
111
|
|
|
return $parent; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $this->findLoopRecursive($parent); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|