Test Failed
Push — master ( f091df...afe914 )
by Sebastian
03:05
created

validateOperand()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 14
rs 10
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
        $tokens = $this->params->getInfo()->createPruner()->limitToOperands()->getTokens();
92
93
        foreach($tokens as $token)
94
        {
95
            if($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_Operand)
96
            {
97
                $this->validateOperand($token);
98
            }
99
        }
100
    }
101
    
102
    protected function validateOperand(Mailcode_Parser_Statement_Tokenizer_Token_Operand $token) : void 
103
    {
104
        $allowed = Mailcode_Parser_Statement_Tokenizer_Token_Operand::getArithmeticSigns();
105
        $allowed[] = '=';
106
        
107
        $sign = $token->getSign();
108
        
109
        // ensure that the operand we have in the command is one of the
110
        // allowed ones.
111
        if(!in_array($sign, $allowed))
112
        {
113
            $this->validationResult->makeError(
114
                t('The %1$s sign is not allowed in this command.', '<code>'.$sign.'</code>'),
115
                Mailcode_Commands_CommonConstants::VALIDATION_INVALID_OPERAND
116
            );
117
        }
118
    }
119
    
120
    protected function validateSyntax_assignment() : void
121
    {
122
        $tokens = $this->getAssignmentTokens();
123
        
124
        if(empty($tokens))
125
        {
126
            $this->validationResult->makeError(
127
                t('No value assigned to the variable.'),
128
                Mailcode_Commands_CommonConstants::VALIDATION_VALUE_MISSING
129
            );
130
        }
131
    }
132
    
133
    public function getVariable() : Mailcode_Variables_Variable
134
    {
135
        if($this->variableToken instanceof Mailcode_Parser_Statement_Tokenizer_Token_Variable)
136
        {
137
            return $this->variableToken->getVariable();
138
        }
139
        
140
        throw new Mailcode_Exception(
141
            'No variable found.',
142
            'Statement does not start with a variable: ['.$this->paramsString.']',
143
            Mailcode_Commands_CommonConstants::ERROR_NO_VARIABLE_AVAILABLE
144
        );
145
    }
146
    
147
   /**
148
    * @return Mailcode_Parser_Statement_Tokenizer_Token[]
149
    */
150
    public function getAssignmentTokens() : array
151
    {
152
        $params = $this->params->getInfo()->getTokens();
153
        
154
        array_shift($params); // variable
155
        
156
        $eq = array_shift($params); // equals sign
157
        
158
        // in case the equals sign was omitted.
159
        if(!$eq instanceof Mailcode_Parser_Statement_Tokenizer_Token_Operand)
160
        {
161
            array_unshift($params, $eq);
162
        }
163
        
164
        return $params;
165
    }
166
    
167
    public function getAssignmentString() : string
168
    {
169
        $tokens = $this->getAssignmentTokens();
170
        
171
        $items = array();
172
        
173
        foreach($tokens as $token)
174
        {
175
            $items[] = $token->getNormalized();
176
        }
177
        
178
        return implode(' ', $items);
179
    }
180
}
181