1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File containing the {@see Mailcode_Traits_Commands_Validation_Operand} trait. |
4
|
|
|
* |
5
|
|
|
* @package Mailcode |
6
|
|
|
* @subpackage Validation |
7
|
|
|
* @see Mailcode_Traits_Commands_Validation_Operand |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Mailcode; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Command validation drop-in: checks for the presence |
16
|
|
|
* of an operand token (any operand sign). Will accept |
17
|
|
|
* the first operand token it finds. |
18
|
|
|
* |
19
|
|
|
* @package Mailcode |
20
|
|
|
* @subpackage Validation |
21
|
|
|
* @author Sebastian Mordziol <[email protected]> |
22
|
|
|
* |
23
|
|
|
* @property \AppUtils\OperationResult $validationResult |
24
|
|
|
* @property Mailcode_Parser_Statement_Validator $validator |
25
|
|
|
*/ |
26
|
|
|
trait Mailcode_Traits_Commands_Validation_Operand |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var Mailcode_Parser_Statement_Tokenizer_Token_Operand|NULL |
30
|
|
|
*/ |
31
|
|
|
protected $operandToken; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return string[] |
35
|
|
|
*/ |
36
|
|
|
abstract protected function getAllowedOperands() : array; |
37
|
|
|
|
38
|
|
|
protected function validateSyntax_operand() : void |
39
|
|
|
{ |
40
|
|
|
$var = $this->validator->createOperand(); |
41
|
|
|
|
42
|
|
|
if($var->isValid()) |
43
|
|
|
{ |
44
|
|
|
if(in_array($var->getSign(), $this->getAllowedOperands())) |
45
|
|
|
{ |
46
|
|
|
$this->operandToken = $var->getToken(); |
47
|
|
|
} |
48
|
|
|
else |
49
|
|
|
{ |
50
|
|
|
$this->validationResult->makeError( |
51
|
|
|
t('Invalid operand %1$s.', $var->getSign()).' '. |
52
|
|
|
t('The following operands may be used in this command:').' '. |
53
|
|
|
'<code>'.implode('</code>, <code>', $this->getAllowedOperands()).'</code>', |
54
|
|
|
Mailcode_Commands_CommonConstants::VALIDATION_INVALID_OPERAND |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
else |
59
|
|
|
{ |
60
|
|
|
$this->validationResult->makeError( |
61
|
|
|
t('No operand has been specified.'), |
62
|
|
|
Mailcode_Commands_CommonConstants::VALIDATION_OPERAND_MISSING |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function getOperand() : Mailcode_Parser_Statement_Tokenizer_Token_Operand |
68
|
|
|
{ |
69
|
|
|
if($this->operandToken instanceof Mailcode_Parser_Statement_Tokenizer_Token_Operand) |
70
|
|
|
{ |
71
|
|
|
return $this->operandToken; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
throw new Mailcode_Exception( |
75
|
|
|
'No operand available', |
76
|
|
|
null, |
77
|
|
|
Mailcode_Commands_CommonConstants::ERROR_NO_OPERAND_AVAILABLE |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Retrieves the operand sign. |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
public function getSign() : string |
87
|
|
|
{ |
88
|
|
|
return $this->getOperand()->getSign(); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|