|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Quantum PHP Framework |
|
5
|
|
|
* |
|
6
|
|
|
* An open source software development framework for PHP |
|
7
|
|
|
* |
|
8
|
|
|
* @package Quantum |
|
9
|
|
|
* @author Arman Ag. <[email protected]> |
|
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
|
11
|
|
|
* @link http://quantum.softberg.org/ |
|
12
|
|
|
* @since 2.4.0 |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Quantum\Libraries\Validation\Rules; |
|
16
|
|
|
|
|
17
|
|
|
use Quantum\Libraries\Captcha\CaptchaManager; |
|
18
|
|
|
use Quantum\Factory\ModelFactory; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Trait General |
|
22
|
|
|
* @package Quantum\Libraries\Validation |
|
23
|
|
|
*/ |
|
24
|
|
|
trait General |
|
25
|
|
|
{ |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Adds validation Error |
|
29
|
|
|
* @param string $field |
|
30
|
|
|
* @param string $rule |
|
31
|
|
|
* @param mixed|null $param |
|
32
|
|
|
*/ |
|
33
|
|
|
abstract protected function addError(string $field, string $rule, $param = null); |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Checks Field Required |
|
37
|
|
|
* @param string $field |
|
38
|
|
|
* @param string $value |
|
39
|
|
|
* @param null|mixed $param |
|
40
|
|
|
*/ |
|
41
|
|
|
protected function required(string $field, string $value, $param = null) |
|
42
|
|
|
{ |
|
43
|
|
|
if ($value == false || empty($value)) { |
|
|
|
|
|
|
44
|
|
|
$this->addError($field, 'required', $param); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Checks Email |
|
50
|
|
|
* @param string $field |
|
51
|
|
|
* @param string $value |
|
52
|
|
|
* @param null|mixed $param |
|
53
|
|
|
*/ |
|
54
|
|
|
protected function email(string $field, string $value, $param = null) |
|
55
|
|
|
{ |
|
56
|
|
|
if (!empty($value)) { |
|
57
|
|
|
if (!filter_var($value, FILTER_VALIDATE_EMAIL)) { |
|
58
|
|
|
$this->addError($field, 'email', $param); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Check Captcha |
|
65
|
|
|
* @param string $field |
|
66
|
|
|
* @param string $value |
|
67
|
|
|
* @param null|mixed $param |
|
68
|
|
|
*/ |
|
69
|
|
|
protected function captcha(string $field, string $value, $param = null) |
|
70
|
|
|
{ |
|
71
|
|
|
if (!empty($value)) { |
|
72
|
|
|
$captcha = CaptchaManager::getCaptcha(); |
|
73
|
|
|
if (!$captcha->verifyResponse($value)){ |
|
74
|
|
|
$this->addError($field, 'captcha', $param); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Checks for a valid credit card number |
|
81
|
|
|
* @param string $field |
|
82
|
|
|
* @param string $value |
|
83
|
|
|
* @param null|mixed $param |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function creditCard(string $field, string $value, $param = null) |
|
86
|
|
|
{ |
|
87
|
|
|
if (!empty($value)) { |
|
88
|
|
|
$number = preg_replace('/\D/', '', $value); |
|
89
|
|
|
|
|
90
|
|
|
if (function_exists('mb_strlen')) { |
|
91
|
|
|
$number_length = mb_strlen($number); |
|
92
|
|
|
} else { |
|
93
|
|
|
$number_length = strlen($number); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
if ($number_length == 0) { |
|
97
|
|
|
$this->addError($field, 'creditCard', $param); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$parity = $number_length % 2; |
|
101
|
|
|
|
|
102
|
|
|
$total = 0; |
|
103
|
|
|
|
|
104
|
|
|
for ($i = 0; $i < $number_length; ++$i) { |
|
105
|
|
|
$digit = $number[$i]; |
|
106
|
|
|
|
|
107
|
|
|
if ($i % 2 == $parity) { |
|
108
|
|
|
$digit *= 2; |
|
109
|
|
|
|
|
110
|
|
|
if ($digit > 9) { |
|
111
|
|
|
$digit -= 9; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$total += $digit; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
if ($total % 10 != 0) { |
|
119
|
|
|
$this->addError($field, 'creditCard', $param); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Checks for a valid IBAN |
|
126
|
|
|
* @param string $field |
|
127
|
|
|
* @param string $value |
|
128
|
|
|
* @param null|mixed $param |
|
129
|
|
|
*/ |
|
130
|
|
|
protected function iban(string $field, string $value, $param = null) |
|
131
|
|
|
{ |
|
132
|
|
|
if (!empty($value)) { |
|
133
|
|
|
static $character = [ |
|
134
|
|
|
'A' => 10, 'C' => 12, 'D' => 13, 'E' => 14, 'F' => 15, 'G' => 16, |
|
135
|
|
|
'H' => 17, 'I' => 18, 'J' => 19, 'K' => 20, 'L' => 21, 'M' => 22, |
|
136
|
|
|
'N' => 23, 'O' => 24, 'P' => 25, 'Q' => 26, 'R' => 27, 'S' => 28, |
|
137
|
|
|
'T' => 29, 'U' => 30, 'V' => 31, 'W' => 32, 'X' => 33, 'Y' => 34, |
|
138
|
|
|
'Z' => 35, 'B' => 11, |
|
139
|
|
|
]; |
|
140
|
|
|
|
|
141
|
|
|
if (!preg_match("/\A[A-Z]{2}\d{2} ?[A-Z\d]{4}( ?\d{4}){1,} ?\d{1,4}\z/", $value)) { |
|
142
|
|
|
$this->addError($field, 'iban', $param); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
$iban = str_replace(' ', '', $value); |
|
146
|
|
|
$iban = substr($iban, 4) . substr($iban, 0, 4); |
|
147
|
|
|
$iban = strtr($iban, $character); |
|
148
|
|
|
|
|
149
|
|
|
if (bcmod($iban, 97) != 1) { |
|
150
|
|
|
$this->addError($field, 'iban', $param); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
|
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Checks for a valid format human name |
|
159
|
|
|
* @param string $field |
|
160
|
|
|
* @param string $value |
|
161
|
|
|
* @param null|mixed $param |
|
162
|
|
|
*/ |
|
163
|
|
|
protected function name(string $field, string $value, $param = null) |
|
164
|
|
|
{ |
|
165
|
|
|
if (!empty($value)) { |
|
166
|
|
|
if (preg_match("/^([a-z \p{L} '-])+$/i", $value) === 0) { |
|
167
|
|
|
$this->addError($field, 'name', $param); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Checks that the provided string is a likely street address. |
|
174
|
|
|
* @param string $field |
|
175
|
|
|
* @param string $value |
|
176
|
|
|
* @param null|mixed $param |
|
177
|
|
|
*/ |
|
178
|
|
|
protected function streetAddress(string $field, string $value, $param = null) |
|
179
|
|
|
{ |
|
180
|
|
|
if (!empty($value)) { |
|
181
|
|
|
$hasLetter = preg_match('/[a-zA-Z]/', $value); |
|
182
|
|
|
$hasDigit = preg_match('/\d/', $value); |
|
183
|
|
|
$hasSpace = preg_match('/\s/', $value); |
|
184
|
|
|
|
|
185
|
|
|
$passes = $hasLetter && $hasDigit && $hasSpace; |
|
186
|
|
|
|
|
187
|
|
|
if (!$passes) { |
|
188
|
|
|
$this->addError($field, 'streetAddress', $param); |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Validates the phone number // 555-555-5555 , 5555425555, 555 555 5555, 1(519) 555-4444, 1 (519) 555-4422, +1-555-555-5555 |
|
195
|
|
|
* @param string $field |
|
196
|
|
|
* @param string $value |
|
197
|
|
|
* @param null|mixed $param |
|
198
|
|
|
*/ |
|
199
|
|
|
protected function phoneNumber(string $field, string $value, $param = null) |
|
200
|
|
|
{ |
|
201
|
|
|
if (!empty($value)) { |
|
202
|
|
|
$regex = '/^(\+*\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}[\s-]?\d{4}$/i'; |
|
203
|
|
|
|
|
204
|
|
|
if (!preg_match($regex, $value)) { |
|
205
|
|
|
$this->addError($field, 'phoneNumber', $param); |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Determines if the provided input is a valid date |
|
212
|
|
|
* @param string $field |
|
213
|
|
|
* @param string $value |
|
214
|
|
|
* @param string|null $param format |
|
215
|
|
|
*/ |
|
216
|
|
|
protected function date(string $field, string $value, string $param = null) |
|
217
|
|
|
{ |
|
218
|
|
|
if (!empty($value)) { |
|
219
|
|
|
if (!$param) { |
|
220
|
|
|
$cdate1 = date('Y-m-d', strtotime($value)); |
|
221
|
|
|
$cdate2 = date('Y-m-d H:i:s', strtotime($value)); |
|
222
|
|
|
|
|
223
|
|
|
if ($cdate1 != $value && $cdate2 != $value) { |
|
224
|
|
|
$this->addError($field, 'date', $param); |
|
225
|
|
|
} |
|
226
|
|
|
} else { |
|
227
|
|
|
$date = \DateTime::createFromFormat($param, $value); |
|
228
|
|
|
|
|
229
|
|
|
if ($date === false || $value != date($param, $date->getTimestamp())) { |
|
230
|
|
|
$this->addError($field, 'date', $param); |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
} |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Ensures the value starts with a certain character / set of character |
|
238
|
|
|
* @param string $field |
|
239
|
|
|
* @param string $value |
|
240
|
|
|
* @param string $param |
|
241
|
|
|
*/ |
|
242
|
|
|
protected function starts(string $field, string $value, string $param) |
|
243
|
|
|
{ |
|
244
|
|
|
if (!empty($value)) { |
|
245
|
|
|
if (strpos($value, $param) !== 0) { |
|
246
|
|
|
$this->addError($field, 'starts', $param); |
|
247
|
|
|
} |
|
248
|
|
|
} |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* Custom regex validator |
|
253
|
|
|
* @param string $field |
|
254
|
|
|
* @param string $value |
|
255
|
|
|
* @param string $param |
|
256
|
|
|
*/ |
|
257
|
|
|
protected function regex(string $field, string $value, string $param) |
|
258
|
|
|
{ |
|
259
|
|
|
if (!empty($value)) { |
|
260
|
|
|
if (!preg_match($param, $value)) { |
|
261
|
|
|
$this->addError($field, 'regex', $param); |
|
262
|
|
|
} |
|
263
|
|
|
} |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* Validates JSON string |
|
268
|
|
|
* @param string $field |
|
269
|
|
|
* @param string $value |
|
270
|
|
|
* @param null|mixed $param |
|
271
|
|
|
*/ |
|
272
|
|
|
protected function jsonString(string $field, string $value, $param = null) |
|
273
|
|
|
{ |
|
274
|
|
|
if (!empty($value)) { |
|
275
|
|
|
$value = htmlspecialchars_decode($value, ENT_QUOTES); |
|
276
|
|
|
|
|
277
|
|
|
if (!is_object(json_decode($value))) { |
|
278
|
|
|
$this->addError($field, 'jsonString', $param); |
|
279
|
|
|
} |
|
280
|
|
|
} |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* Validates same value for both fields |
|
285
|
|
|
* @param string $field |
|
286
|
|
|
* @param string $value |
|
287
|
|
|
* @param null|mixed $param |
|
288
|
|
|
*/ |
|
289
|
|
|
protected function same(string $field, string $value, $param = null) |
|
290
|
|
|
{ |
|
291
|
|
|
if (!empty($value)) { |
|
292
|
|
|
if ($value != $this->data[$param]) { |
|
293
|
|
|
$this->addError($field, 'same', $param); |
|
294
|
|
|
} |
|
295
|
|
|
} |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
/** |
|
299
|
|
|
* Validates uniqueness |
|
300
|
|
|
* @param string $field |
|
301
|
|
|
* @param mixed $value |
|
302
|
|
|
* @param string $param |
|
303
|
|
|
* @throws \Quantum\Exceptions\DiException |
|
304
|
|
|
*/ |
|
305
|
|
|
protected function unique(string $field, $value, string $param) |
|
306
|
|
|
{ |
|
307
|
|
|
if (!empty($value)) { |
|
308
|
|
|
$model = ModelFactory::get(ucfirst($param)); |
|
309
|
|
|
|
|
310
|
|
|
$row = $model->findOneBy($field, $value); |
|
311
|
|
|
|
|
312
|
|
|
if ($row->count()) { |
|
313
|
|
|
$this->addError($field, 'unique', null); |
|
314
|
|
|
} |
|
315
|
|
|
} |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
} |