Passed
Push — master ( 77b992...62880d )
by Sebastian
02:14
created

Mailcode_Variables_Variable::getFullName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
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 VALIDATION_ERROR_PATH_NUMERIC = 48201;
29
    
30
    const VALIDATION_ERROR_NAME_NUMERIC = 48202;
31
    
32
   /**
33
    * @var string
34
    */
35
    protected $path;
36
    
37
   /**
38
    * @var string
39
    */
40
    protected $name;
41
    
42
   /**
43
    * @var string
44
    */
45
    protected $matchedText;
46
    
47
   /**
48
    * @var string
49
    */
50
    protected $hash = '';
51
    
52
   /**
53
    * @var OperationResult
54
    */
55
    protected $validationResult = null;
56
    
57
    public function __construct(string $path, string $name, string $matchedText)
58
    {
59
        $this->path = $path;
60
        $this->name = $name;
61
        $this->matchedText = $matchedText;
62
    }
63
    
64
    public function getFullName() : string
65
    {
66
        return '$'.$this->path.'.'.$this->name;
67
    }
68
    
69
    public function getName() : string
70
    {
71
        return $this->name;
72
    }
73
    
74
    public function getPath() : string
75
    {
76
        return $this->path;
77
    }
78
    
79
    public function getMatchedText() : string
80
    {
81
        return $this->matchedText;
82
    }
83
    
84
    public function getHash() : string
85
    {
86
        if(empty($this->hash))
87
        {
88
            $this->hash = md5($this->matchedText);
89
        }
90
        
91
        return $this->hash;
92
    }
93
    
94
    public function isValid() : bool
95
    {
96
        $this->validate();
97
        
98
        return $this->validationResult->isValid();
99
    }
100
    
101
    public function getValidationResult() : OperationResult
102
    {
103
        $this->validate();
104
        
105
        return $this->validationResult;
106
    }
107
    
108
    protected function validate() : void
109
    {
110
        if(isset($this->validationResult))
111
        {
112
            return;
113
        }
114
        
115
        $this->validationResult = new OperationResult($this);
116
        
117
        if(is_numeric(substr($this->path, 0, 1))) 
118
        {
119
            $this->validationResult->makeError(
120
                t(
121
                    'The path %1$s of variable %2$s must begin with a letter.', 
122
                    $this->path, 
123
                    $this->getFullName()
124
                ),
125
                self::VALIDATION_ERROR_PATH_NUMERIC
126
            );
127
        }
128
        
129
        if(is_numeric(substr($this->name, 0, 1)))
130
        {
131
            $this->validationResult->makeError(
132
                t(
133
                    'The name %1$s of variable %2$s must begin with a letter.',
134
                    $this->name,
135
                    $this->getFullName()
136
                ),
137
                self::VALIDATION_ERROR_NAME_NUMERIC
138
            );
139
        }
140
    }
141
}
142