|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Codervio\Envmanager\Validator; |
|
4
|
|
|
|
|
5
|
|
|
use Codervio\Envmanager\Parser\ValueParser; |
|
6
|
|
|
use Codervio\Envmanager\Exceptions\ValidationException; |
|
7
|
|
|
use Codervio\Envmanager\Exceptions\VariableException; |
|
8
|
|
|
use Exception; |
|
9
|
|
|
|
|
10
|
|
|
class VariableValidator |
|
11
|
|
|
{ |
|
12
|
|
|
protected $variable; |
|
13
|
|
|
|
|
14
|
|
|
protected $valueParser; |
|
15
|
|
|
|
|
16
|
|
|
private $result; |
|
17
|
|
|
|
|
18
|
|
|
private $strict = false; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct($variable) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->variable = $variable; |
|
23
|
|
|
|
|
24
|
|
|
$this->valueParser = new ValueParser; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function setResult($result) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->result = $result; |
|
30
|
|
|
|
|
31
|
|
|
$this->checkVariable($this->variable, $this->result); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
private function checkVariable($variable, $result) |
|
35
|
|
|
{ |
|
36
|
|
|
if (!array_key_exists($variable, $result)) { |
|
37
|
|
|
throw new VariableException(sprintf('No variable "%s" set', $variable)); |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function setStrict($strict) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->strict = $strict; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function isBoolean() |
|
47
|
|
|
{ |
|
48
|
|
|
if (!isset($this->result[$this->variable])) { |
|
49
|
|
|
throw new Exception('No variable'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
if (is_bool($this->valueParser->parseBool($this->result[$this->variable], $this->strict))) { |
|
53
|
|
|
return true; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return false; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function isEmpty() |
|
60
|
|
|
{ |
|
61
|
|
|
if (empty($this->valueParser->parseEmpty($this->result[$this->variable]))) { |
|
62
|
|
|
return true; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return false; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function isNumber() |
|
69
|
|
|
{ |
|
70
|
|
|
if (is_int($this->valueParser->parseNumeric($this->result[$this->variable]))) { |
|
71
|
|
|
return true; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return false; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function isFloat() |
|
78
|
|
|
{ |
|
79
|
|
|
if (is_float($this->valueParser->parseFloat($this->result[$this->variable]))) { |
|
80
|
|
|
return true; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return false; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function checkValues($value) |
|
87
|
|
|
{ |
|
88
|
|
|
if (!isset($this->result[$this->variable])) { |
|
89
|
|
|
throw new ValidationException('No variable'); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
if (is_array($value)) { |
|
93
|
|
|
|
|
94
|
|
|
$arraydiffs = array_diff($value, array($this->result[$this->variable])); |
|
95
|
|
|
|
|
96
|
|
|
if (is_array($arraydiffs) && !empty($arraydiffs)) { |
|
97
|
|
|
throw new ValidationException(sprintf('Missing value: "%s" in "%s" environment variable', join(', ', $arraydiffs), $this->variable)); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return true; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
if ($this->result[$this->variable] === $value) { |
|
104
|
|
|
return true; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return false; |
|
108
|
|
|
} |
|
109
|
|
|
} |