|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace FigTree\Validation; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
use FigTree\Validation\Contracts\RuleInterface; |
|
7
|
|
|
|
|
8
|
|
|
class Rule implements RuleInterface |
|
9
|
|
|
{ |
|
10
|
|
|
public function __construct(protected int $filterType, protected int $flags = 0, protected array $options = [], protected ?Closure $callback = null) |
|
11
|
|
|
{ |
|
12
|
|
|
// |
|
13
|
|
|
} |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Get the Rule filter type. |
|
17
|
|
|
* |
|
18
|
|
|
* @return integer |
|
19
|
|
|
*/ |
|
20
|
|
|
public function getFilterType(): int |
|
21
|
|
|
{ |
|
22
|
|
|
return $this->filterType; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Get the Rule flags. |
|
27
|
|
|
* |
|
28
|
|
|
* @return integer |
|
29
|
|
|
*/ |
|
30
|
|
|
public function getFlags(): int |
|
31
|
|
|
{ |
|
32
|
|
|
return $this->flags; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Check if a Rule flag is set. |
|
37
|
|
|
* |
|
38
|
|
|
* @param integer $flag |
|
39
|
|
|
* |
|
40
|
|
|
* @return boolean |
|
41
|
|
|
*/ |
|
42
|
|
|
public function hasFlag(int $flag): bool |
|
43
|
|
|
{ |
|
44
|
|
|
return ($flag > 0 && ($this->flags & $flag) == $flag); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Add a Rule flag. |
|
49
|
|
|
* |
|
50
|
|
|
* @param integer $flag |
|
51
|
|
|
* |
|
52
|
|
|
* @return $this |
|
53
|
|
|
*/ |
|
54
|
|
|
public function addFlag(int $flag): RuleInterface |
|
55
|
|
|
{ |
|
56
|
|
|
$this->flags |= $flag; |
|
57
|
|
|
|
|
58
|
|
|
return $this; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Remove a Rule flag. |
|
63
|
|
|
* |
|
64
|
|
|
* @param integer $flag |
|
65
|
|
|
* |
|
66
|
|
|
* @return $this |
|
67
|
|
|
*/ |
|
68
|
|
|
public function removeFlag(int $flag): RuleInterface |
|
69
|
|
|
{ |
|
70
|
|
|
$this->flags &= $flag; |
|
71
|
|
|
|
|
72
|
|
|
return $this; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Get the Rule options. |
|
77
|
|
|
* |
|
78
|
|
|
* @return array |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getOptions(): array |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->options; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Check if a given Rule option exists. |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $name |
|
89
|
|
|
* |
|
90
|
|
|
* @return boolean |
|
91
|
|
|
*/ |
|
92
|
|
|
public function hasOption(string $name): bool |
|
93
|
|
|
{ |
|
94
|
|
|
return key_exists($name, $this->options); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Get the value of a given Rule option. |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $name |
|
101
|
|
|
* @param mixed $default |
|
102
|
|
|
* |
|
103
|
|
|
* @return mixed |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getOption(string $name, $default = null) |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->options[$name] ?? $default; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Set the value of a given Rule option. |
|
112
|
|
|
* |
|
113
|
|
|
* @param string $name |
|
114
|
|
|
* @param mixed $value |
|
115
|
|
|
* |
|
116
|
|
|
* @return $this |
|
117
|
|
|
*/ |
|
118
|
|
|
public function setOption(string $name, $value): RuleInterface |
|
119
|
|
|
{ |
|
120
|
|
|
$this->options[$name] = $value; |
|
121
|
|
|
|
|
122
|
|
|
return $this; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Remove the given option from the Rule. |
|
127
|
|
|
* |
|
128
|
|
|
* @param string $name |
|
129
|
|
|
* |
|
130
|
|
|
* @return $this |
|
131
|
|
|
*/ |
|
132
|
|
|
public function removeOption(string $name): RuleInterface |
|
133
|
|
|
{ |
|
134
|
|
|
unset($this->options[$name]); |
|
135
|
|
|
|
|
136
|
|
|
return $this; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Get the Rule callback. |
|
141
|
|
|
* |
|
142
|
|
|
* @return \Closure|null |
|
143
|
|
|
*/ |
|
144
|
|
|
public function getCallback(): ?Closure |
|
145
|
|
|
{ |
|
146
|
|
|
return $this->callback; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Get the Rule callback. |
|
151
|
|
|
* |
|
152
|
|
|
* @param \Closure|null $callback |
|
153
|
|
|
* |
|
154
|
|
|
* @return $this |
|
155
|
|
|
*/ |
|
156
|
|
|
public function setCallback(?Closure $callback): RuleInterface |
|
157
|
|
|
{ |
|
158
|
|
|
$this->callback = $callback; |
|
159
|
|
|
|
|
160
|
|
|
return $this; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Cast the Rule into an array. |
|
165
|
|
|
* |
|
166
|
|
|
* @return array |
|
167
|
|
|
*/ |
|
168
|
|
|
public function toArray(): array |
|
169
|
|
|
{ |
|
170
|
|
|
$options = $this->getOptions(); |
|
171
|
|
|
$callback = $this->getCallback(); |
|
172
|
|
|
|
|
173
|
|
|
$array = [ |
|
174
|
|
|
'filter' => $this->getFilterType(), |
|
175
|
|
|
'flags' => $this->getFlags(), |
|
176
|
|
|
]; |
|
177
|
|
|
|
|
178
|
|
|
if (!empty($callback)) { |
|
179
|
|
|
$array['options'] = $callback; |
|
180
|
|
|
} elseif (!empty($options)) { |
|
181
|
|
|
$array['options'] = $options; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
return $array; |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|