-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathRule.php
More file actions
237 lines (208 loc) · 4.71 KB
/
Rule.php
File metadata and controls
237 lines (208 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
namespace Rakit\Validation;
use Rakit\Validation\MissingRequiredParameterException;
abstract class Rule
{
/** @var string */
protected $key;
/** @var \Rakit\Validation\Attribute|null */
protected $attribute;
/** @var \Rakit\Validation\Validation|null */
protected $validation;
/** @var bool */
protected $implicit = false;
/** @var array */
protected $params = [];
/** @var array */
protected $paramsTexts = [];
/** @var array */
protected $fillableParams = [];
/** @var string */
protected $message = "The :attribute is invalid";
abstract public function check($value): bool;
/**
* Set Validation class instance
*
* @param \Rakit\Validation\Validation $validation
* @return void
*/
public function setValidation(Validation $validation)
{
$this->validation = $validation;
}
public function getValidation()
{
return $this->validation;
}
/**
* Set key
*
* @param string $key
* @return void
*/
public function setKey(string $key)
{
$this->key = $key;
}
/**
* Get key
*
* @return string
*/
public function getKey()
{
return $this->key ?: get_class($this);
}
/**
* Set attribute
*
* @param \Rakit\Validation\Attribute $attribute
* @return void
*/
public function setAttribute(Attribute $attribute)
{
$this->attribute = $attribute;
}
/**
* Get attribute
*
* @return \Rakit\Validation\Attribute|null
*/
public function getAttribute()
{
return $this->attribute;
}
/**
* Get parameters
*
* @return array
*/
public function getParameters(): array
{
return $this->params;
}
/**
* Set params
*
* @param array $params
* @return \Rakit\Validation\Rule
*/
public function setParameters(array $params): Rule
{
$this->params = array_merge($this->params, $params);
return $this;
}
/**
* Set parameters
*
* @param string $key
* @param mixed $value
* @return \Rakit\Validation\Rule
*/
public function setParameter(string $key, $value): Rule
{
$this->params[$key] = $value;
return $this;
}
/**
* Fill $params to $this->params
*
* @param array $params
* @return \Rakit\Validation\Rule
*/
public function fillParameters(array $params): Rule
{
foreach ($this->fillableParams as $key) {
if (empty($params)) {
break;
}
$this->params[$key] = array_shift($params);
}
return $this;
}
/**
* Get parameter from given $key, return null if it not exists
*
* @param string $key
* @return mixed
*/
public function parameter(string $key)
{
return isset($this->params[$key])? $this->params[$key] : null;
}
/**
* Set parameter text that can be displayed in error message using ':param_key'
*
* @param string $key
* @param string $text
* @return void
*/
public function setParameterText(string $key, string $text)
{
$this->paramsTexts[$key] = $text;
}
/**
* Get $paramsTexts
*
* @return array
*/
public function getParametersTexts(): array
{
return $this->paramsTexts;
}
/**
* Check whether this rule is implicit
*
* @return boolean
*/
public function isImplicit(): bool
{
return $this->implicit;
}
/**
* Just alias of setMessage
*
* @param string $message
* @return \Rakit\Validation\Rule
*/
public function message(string $message): Rule
{
return $this->setMessage($message);
}
/**
* Set message
*
* @param string $message
* @return \Rakit\Validation\Rule
*/
public function setMessage(string $message): Rule
{
$this->message = $message;
return $this;
}
/**
* Get message
*
* @return string
*/
public function getMessage(): string
{
return $this->message;
}
/**
* Check given $params must be exists
*
* @param array $params
* @return void
* @throws \Rakit\Validation\MissingRequiredParameterException
*/
protected function requireParameters(array $params)
{
foreach ($params as $param) {
if (!isset($this->params[$param])) {
$rule = $this->getKey();
throw new MissingRequiredParameterException("Missing required parameter '{$param}' on rule '{$rule}'");
}
}
}
}