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