1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Lexuss1979\Validol; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Lexuss1979\Validol\Exceptions\ValueObjectInvalidTypeException; |
8
|
|
|
|
9
|
|
|
class ValueObject |
10
|
|
|
{ |
11
|
|
|
const STRING = 'string'; |
12
|
|
|
const INT = 'int'; |
13
|
|
|
const FLOAT = 'float'; |
14
|
|
|
const BOOL = 'bool'; |
15
|
|
|
|
16
|
|
|
private $value; |
17
|
|
|
/** |
18
|
|
|
* @var null |
19
|
|
|
*/ |
20
|
|
|
private $type; |
21
|
|
|
private $name; |
22
|
|
|
private $alias; |
23
|
|
|
|
24
|
54 |
|
public function __construct($name, $value, $type = null) |
25
|
|
|
{ |
26
|
54 |
|
$this->name = $name; |
27
|
54 |
|
$this->alias = $name; |
28
|
54 |
|
$this->value = $value; |
29
|
54 |
|
if (!in_array($type, $this->types())) throw new ValueObjectInvalidTypeException('Wrong type: ' . $type); |
30
|
53 |
|
$this->type = $type; |
31
|
53 |
|
} |
32
|
|
|
|
33
|
54 |
|
protected function types() |
34
|
|
|
{ |
35
|
54 |
|
return [null, self::BOOL, self::FLOAT, self::INT, self::STRING]; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public static function get($value, $type = null) |
39
|
|
|
{ |
40
|
|
|
return new static($value, $type); |
41
|
|
|
} |
42
|
|
|
|
43
|
41 |
|
public function setAlias($alias) |
44
|
|
|
{ |
45
|
41 |
|
$this->alias = $alias; |
46
|
41 |
|
} |
47
|
|
|
|
48
|
1 |
|
public function __invoke() |
49
|
|
|
{ |
50
|
1 |
|
return $this->value; |
51
|
|
|
} |
52
|
|
|
|
53
|
27 |
|
public function name() |
54
|
|
|
{ |
55
|
27 |
|
return $this->name; |
56
|
|
|
} |
57
|
|
|
|
58
|
24 |
|
public function alias() |
59
|
|
|
{ |
60
|
24 |
|
return $this->alias; |
61
|
|
|
} |
62
|
|
|
|
63
|
42 |
|
public function value() |
64
|
|
|
{ |
65
|
42 |
|
return $this->value; |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
public function type() |
69
|
|
|
{ |
70
|
1 |
|
return $this->type; |
71
|
|
|
} |
72
|
|
|
|
73
|
4 |
|
public function isInt() |
74
|
|
|
{ |
75
|
4 |
|
return $this->type === self::INT; |
76
|
|
|
} |
77
|
|
|
|
78
|
2 |
|
public function isString() |
79
|
|
|
{ |
80
|
2 |
|
return $this->type === self::STRING; |
81
|
|
|
} |
82
|
|
|
|
83
|
2 |
|
public function isBool() |
84
|
|
|
{ |
85
|
2 |
|
return $this->type === self::BOOL; |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
public function isFloat() |
89
|
|
|
{ |
90
|
1 |
|
return $this->type === self::FLOAT; |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
public function isUndefinedType() |
94
|
|
|
{ |
95
|
1 |
|
return is_null($this->type); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param Rule $rule |
100
|
|
|
* @return bool |
101
|
|
|
*/ |
102
|
44 |
|
public function satisfy(Rule $rule) |
103
|
|
|
{ |
104
|
44 |
|
return $rule->process($this); |
105
|
|
|
} |
106
|
|
|
|
107
|
41 |
|
public function isNull() |
108
|
|
|
{ |
109
|
41 |
|
return false; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
protected function setName($name) |
113
|
|
|
{ |
114
|
|
|
if (preg_match("/^(\S*)\s*as\s*(\S*)$/", $name, $matches)) { |
115
|
|
|
$this->name = $matches[1]; |
116
|
|
|
$this->alias = $matches[2]; |
117
|
|
|
} else { |
118
|
|
|
$this->name = $name; |
119
|
|
|
$this->alias = $name; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
} |