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