Passed
Push — master ( 62880d...2f714f )
by Sebastian
07:50
created

Mailcode_Variables_Variable::getValidationResult()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 2
b 0
f 0
nc 2
nop 0
dl 0
loc 12
rs 10
1
<?php
2
/**
3
 * File containing the {@see Mailcode_Variables_Variable} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Variables
7
 * @see Mailcode_Variables_Variable
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
use AppUtils\OperationResult;
15
16
/**
17
 * Simple container for a single variable name occurrence: used
18
 * to store information on variable names used in commands, with
19
 * the possibility to retrieve the actual matched text among other
20
 * things.
21
 *
22
 * @package Mailcode
23
 * @subpackage Variables
24
 * @author Sebastian Mordziol <[email protected]>
25
 */
26
class Mailcode_Variables_Variable
27
{
28
    const ERROR_MISSING_VALIDATION_METHOD = 48601;
29
    
30
    const VALIDATION_ERROR_PATH_NUMERIC = 48201;
31
    const VALIDATION_ERROR_NAME_NUMERIC = 48202;
32
    const VALIDATION_ERROR_PATH_UNDERSCORE = 48203;
33
    const VALIDATION_ERROR_NAME_UNDERSCORE = 48204;
34
    
35
   /**
36
    * @var string
37
    */
38
    protected $path;
39
    
40
   /**
41
    * @var string
42
    */
43
    protected $name;
44
    
45
   /**
46
    * @var string
47
    */
48
    protected $matchedText;
49
    
50
   /**
51
    * @var string
52
    */
53
    protected $hash = '';
54
    
55
   /**
56
    * @var OperationResult
57
    */
58
    protected $validationResult = null;
59
    
60
    public function __construct(string $path, string $name, string $matchedText)
61
    {
62
        $this->path = $path;
63
        $this->name = $name;
64
        $this->matchedText = $matchedText;
65
    }
66
    
67
    public function getFullName() : string
68
    {
69
        return '$'.$this->path.'.'.$this->name;
70
    }
71
    
72
    public function getName() : string
73
    {
74
        return $this->name;
75
    }
76
    
77
    public function getPath() : string
78
    {
79
        return $this->path;
80
    }
81
    
82
    public function getMatchedText() : string
83
    {
84
        return $this->matchedText;
85
    }
86
    
87
    public function getHash() : string
88
    {
89
        if(empty($this->hash))
90
        {
91
            $this->hash = md5($this->matchedText);
92
        }
93
        
94
        return $this->hash;
95
    }
96
    
97
    public function isValid() : bool
98
    {
99
        return $this->getValidationResult()->isValid();
100
    }
101
    
102
    public function getValidationResult() : OperationResult
103
    {
104
        if(isset($this->validationResult))
105
        {
106
            return $this->validationResult;
107
        }
108
        
109
        $this->validationResult = new OperationResult($this);
110
        
111
        $this->validate();
112
        
113
        return $this->validationResult;
114
    }
115
116
    protected $validations = array(
117
        'number_path',
118
        'number_name',
119
        'underscore_path',
120
        'underscore_name'
121
    );
122
    
123
    protected function validate() : void
124
    {
125
        foreach($this->validations as $validation)
126
        {
127
            $method = 'validate_'.$validation;
128
            
129
            if(!method_exists($this, $method))
130
            {
131
                throw new Mailcode_Exception(
132
                    'Unknown validation method',
133
                    sprintf(
134
                        'The method [%s] is missing in class [%s].',
135
                        $method,
136
                        get_class($this)
137
                    ),
138
                    self::ERROR_MISSING_VALIDATION_METHOD
139
                );
140
            }
141
            
142
            $this->$method();
143
            
144
            if(!$this->validationResult->isValid())
145
            {
146
                return;
147
            }
148
        }
149
    }
150
    
151
    protected function validate_number_path()
152
    {
153
        $this->validateNumber($this->path, self::VALIDATION_ERROR_PATH_NUMERIC);
154
    }
155
    
156
    protected function validate_number_name()
157
    {
158
        $this->validateNumber($this->name, self::VALIDATION_ERROR_NAME_NUMERIC);
159
    }
160
    
161
    protected function validate_underscore_path()
162
    {
163
        $this->validateUnderscore($this->path, self::VALIDATION_ERROR_PATH_UNDERSCORE);
164
    }
165
    
166
    protected function validate_underscore_name()
167
    {
168
        $this->validateUnderscore($this->name, self::VALIDATION_ERROR_NAME_UNDERSCORE);
169
    }
170
    
171
    protected function validateNumber(string $value, int $errorCode)
172
    {
173
        if(!is_numeric(substr($value, 0, 1)))
174
        {
175
            return;
176
        }
177
        
178
        $this->validationResult->makeError(
179
            t(
180
                'The %1$s in variable %2$s must begin with a letter.',
181
                $value,
182
                $this->getFullName()
183
            ),
184
            $errorCode
185
        );
186
    }
187
    
188
    protected function validateUnderscore(string $value, int $errorCode)
189
    {
190
        $length = strlen($value);
191
        
192
        // trimming underscores does not change the length: no underscores at start or end of string.
193
        if(strlen(trim($value, '_')) == $length)
194
        {
195
            return;
196
        }
197
        
198
        $this->validationResult->makeError(
199
            t(
200
                'The %1$s in variable %2$s may not start or end with an underscore.',
201
                $value,
202
                $this->getFullName()
203
            ),
204
            $errorCode
205
        );
206
    }
207
}
208