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