|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* File containing the {@see Mailcode_Commands_Command_If_Contains} class. |
|
4
|
|
|
* |
|
5
|
|
|
* @package Mailcode |
|
6
|
|
|
* @subpackage Commands |
|
7
|
|
|
* @see Mailcode_Commands_Command_If_Contains |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Mailcode; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Mailcode command: opening IF statement. |
|
16
|
|
|
* |
|
17
|
|
|
* @package Mailcode |
|
18
|
|
|
* @subpackage Commands |
|
19
|
|
|
* @author Sebastian Mordziol <[email protected]> |
|
20
|
|
|
* |
|
21
|
|
|
* @property Mailcode_Parser_Statement $params |
|
22
|
|
|
* @property \AppUtils\OperationResult $validationResult |
|
23
|
|
|
*/ |
|
24
|
|
|
trait Mailcode_Traits_Commands_IfContains |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var Mailcode_Parser_Statement_Tokenizer_Token_Variable|NULL |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $variable; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral|NULL |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $literal; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var boolean |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $caseInsensitive = false; |
|
40
|
|
|
|
|
41
|
|
|
protected function getValidations() : array |
|
42
|
|
|
{ |
|
43
|
|
|
return array( |
|
44
|
|
|
'variable', |
|
45
|
|
|
'literal', |
|
46
|
|
|
); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
protected function validateSyntax_variable() : void |
|
50
|
|
|
{ |
|
51
|
|
|
$info = $this->params->getInfo(); |
|
52
|
|
|
|
|
53
|
|
|
$variable = $info->getVariableByIndex(0); |
|
54
|
|
|
|
|
55
|
|
|
if($variable) |
|
56
|
|
|
{ |
|
57
|
|
|
$this->variable = $variable; |
|
58
|
|
|
return; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$this->validationResult->makeError( |
|
62
|
|
|
t('No variable specified in the command.'), |
|
63
|
|
|
Mailcode_Commands_IfBase::VALIDATION_VARIABLE_MISSING |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
protected function validateSyntax_literal() : void |
|
68
|
|
|
{ |
|
69
|
|
|
$info = $this->params->getInfo(); |
|
70
|
|
|
|
|
71
|
|
|
// first variant: variable "Search term" |
|
72
|
|
|
$string = $info->getStringLiteralByIndex(1); |
|
73
|
|
|
|
|
74
|
|
|
if($string) |
|
75
|
|
|
{ |
|
76
|
|
|
$this->literal = $string; |
|
77
|
|
|
return; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$this->validateSyntax_keyword(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
protected function validateSyntax_keyword() : void |
|
84
|
|
|
{ |
|
85
|
|
|
$info = $this->params->getInfo(); |
|
86
|
|
|
|
|
87
|
|
|
// second variant: variable insensitive: "Search term" |
|
88
|
|
|
$keyword = $info->getKeywordByIndex(1); |
|
89
|
|
|
|
|
90
|
|
|
if(!$keyword) |
|
91
|
|
|
{ |
|
92
|
|
|
$this->validationResult->makeError( |
|
93
|
|
|
t('Expected a search term or the %1$s keyword after the variable name.', 'insensitive:'), |
|
94
|
|
|
Mailcode_Commands_IfBase::VALIDATION_EXPECTED_KEYWORD |
|
95
|
|
|
); |
|
96
|
|
|
|
|
97
|
|
|
return; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
if($keyword->getKeyword() !== 'insensitive:') |
|
101
|
|
|
{ |
|
102
|
|
|
$this->validationResult->makeError( |
|
103
|
|
|
t('Invalid keyword %1$s.', $keyword->getKeyword()).' '. |
|
104
|
|
|
t('Only the keyword %1$s may be used here.', 'insensitive:'), |
|
105
|
|
|
Mailcode_Commands_IfBase::VALIDATION_INVALID_KEYWORD |
|
106
|
|
|
); |
|
107
|
|
|
|
|
108
|
|
|
return; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$this->caseInsensitive = true; |
|
112
|
|
|
|
|
113
|
|
|
$this->validateSyntax_literal_after_keyword(); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
protected function validateSyntax_literal_after_keyword() : void |
|
117
|
|
|
{ |
|
118
|
|
|
$info = $this->params->getInfo(); |
|
119
|
|
|
|
|
120
|
|
|
$string = $info->getStringLiteralByIndex(2); |
|
121
|
|
|
|
|
122
|
|
|
if($string) |
|
123
|
|
|
{ |
|
124
|
|
|
$this->literal = $string; |
|
125
|
|
|
return; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
$this->validationResult->makeError( |
|
129
|
|
|
t('Expected a search term after the %1$s keyword.', 'insensitive:'), |
|
130
|
|
|
Mailcode_Commands_IfBase::VALIDATION_CONTAINS_MISSING_SEARCH_TERM |
|
131
|
|
|
); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Retrieves the variable being compared. |
|
136
|
|
|
* |
|
137
|
|
|
* @return Mailcode_Variables_Variable |
|
138
|
|
|
*/ |
|
139
|
|
|
public function getVariable() : Mailcode_Variables_Variable |
|
140
|
|
|
{ |
|
141
|
|
|
if(isset($this->variable)) |
|
142
|
|
|
{ |
|
143
|
|
|
return $this->variable->getVariable(); |
|
|
|
|
|
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
throw new Mailcode_Exception( |
|
147
|
|
|
'No variable available', |
|
148
|
|
|
null, |
|
149
|
|
|
Mailcode_Commands_IfBase::ERROR_NO_VARIABLE_AVAILABLE |
|
150
|
|
|
); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
public function isCaseInsensitive() : bool |
|
154
|
|
|
{ |
|
155
|
|
|
return $this->caseInsensitive; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
public function getSearchTerm() : string |
|
159
|
|
|
{ |
|
160
|
|
|
if(!isset($this->literal)) |
|
161
|
|
|
{ |
|
162
|
|
|
throw new Mailcode_Exception( |
|
163
|
|
|
'No string literal available', |
|
164
|
|
|
null, |
|
165
|
|
|
Mailcode_Commands_IfBase::ERROR_NO_STRING_LITERAL_AVAILABLE |
|
166
|
|
|
); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
return $this->literal->getNormalized(); |
|
|
|
|
|
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.