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