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