1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace System\Validation; |
4
|
|
|
|
5
|
|
|
use System\Application; |
6
|
|
|
use System\Validation\Date; |
7
|
|
|
use System\Validation\Characters; |
8
|
|
|
use System\Validation\AllowedCharacters; |
9
|
|
|
|
10
|
|
|
class Validator |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Application Object |
14
|
|
|
* |
15
|
|
|
* @var \System\Application |
16
|
|
|
*/ |
17
|
|
|
private $app; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Input name |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $input; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Input value |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $value; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Errors container |
35
|
|
|
* |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
private $errors = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Constructor |
42
|
|
|
* |
43
|
|
|
* @param \System\Application $app |
44
|
|
|
*/ |
45
|
|
|
public function __construct(Application $app) |
46
|
|
|
{ |
47
|
|
|
$this->app = $app; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function input($input, $request = 'post') |
51
|
|
|
{ |
52
|
|
|
$this->input = $input; |
53
|
|
|
|
54
|
|
|
$this->value = $this->app->request->$request($this->input); |
55
|
|
|
|
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get the value for the input name |
61
|
|
|
* |
62
|
|
|
* @return mixed |
63
|
|
|
*/ |
64
|
|
|
private function value() |
65
|
|
|
{ |
66
|
|
|
return mb_strtolower($this->value); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Determine if the input is not empty |
71
|
|
|
* |
72
|
|
|
* @param bool $call |
73
|
|
|
* @param string $msg |
74
|
|
|
* @return $this |
75
|
|
|
*/ |
76
|
|
|
public function require($call = true, $msg = null) |
77
|
|
|
{ |
78
|
|
|
if ($call === false) return $this; |
79
|
|
|
|
80
|
|
|
$value = $this->value(); |
81
|
|
|
|
82
|
|
|
if ($value === '' || $value === null) { |
83
|
|
|
$msg = $msg ?: 'this field is required'; |
84
|
|
|
|
85
|
|
|
$this->addError($this->input, $msg); |
86
|
|
|
} |
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Call the function by given $type |
92
|
|
|
* |
93
|
|
|
* @param string $type |
94
|
|
|
* @return mix |
|
|
|
|
95
|
|
|
*/ |
96
|
|
|
public function type($type) |
97
|
|
|
{ |
98
|
|
|
return $this->$type(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Determine if the input is valid email |
103
|
|
|
* |
104
|
|
|
* @param bool $call |
105
|
|
|
* @param string $msg |
106
|
|
|
* @return $this |
107
|
|
|
*/ |
108
|
|
|
public function email($call = true, $msg = null) |
109
|
|
|
{ |
110
|
|
|
if ($call === false) return $this; |
111
|
|
|
|
112
|
|
|
$value = $this->value(); |
113
|
|
|
|
114
|
|
|
if (!$value && $value != '0') return $this; |
115
|
|
|
|
116
|
|
|
if (!filter_var($value, FILTER_VALIDATE_EMAIL)) { |
117
|
|
|
$msg = $msg ?: 'e-mail is not valid'; |
118
|
|
|
|
119
|
|
|
$this->addError($this->input, $msg); |
120
|
|
|
} |
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Determine if the input is an image |
126
|
|
|
* |
127
|
|
|
* @param bool $call |
128
|
|
|
* @param string $customErrorMessage |
129
|
|
|
* @return $this |
130
|
|
|
*/ |
131
|
|
|
public function image($call = true, $msg = null) |
132
|
|
|
{ |
133
|
|
|
if ($call === false) return $this; |
134
|
|
|
|
135
|
|
|
$file = $this->app->request->file($this->input); |
136
|
|
|
|
137
|
|
|
if (!$file->exists()) return $this; |
138
|
|
|
|
139
|
|
|
if (!$file->isImage()) { |
140
|
|
|
$msg = $msg ?: 'image is not valid'; |
141
|
|
|
|
142
|
|
|
$this->addError($this->input, $msg); |
143
|
|
|
} |
144
|
|
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Determine if the input has number |
149
|
|
|
* |
150
|
|
|
* @param bool $call |
151
|
|
|
* @param string $msg |
152
|
|
|
* @return $this |
153
|
|
|
*/ |
154
|
|
|
public function number($call = true, $msg = null) |
155
|
|
|
{ |
156
|
|
|
if ($call === false) return $this; |
157
|
|
|
|
158
|
|
|
$value = $this->value(); |
159
|
|
|
|
160
|
|
|
if (!$value && $value != '0') return $this; |
161
|
|
|
|
162
|
|
|
if (!is_numeric($value)) { |
163
|
|
|
$msg = $msg ?: 'this field must be a number'; |
164
|
|
|
|
165
|
|
|
$this->addError($this->input, $msg); |
166
|
|
|
} |
167
|
|
|
return $this; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Determine if the input has float value |
172
|
|
|
* |
173
|
|
|
* @param bool $call |
174
|
|
|
* @param string $msg |
175
|
|
|
* @return $this |
176
|
|
|
*/ |
177
|
|
|
public function float($call = true, $msg = null) |
178
|
|
|
{ |
179
|
|
|
if ($call === false) return $this; |
180
|
|
|
|
181
|
|
|
$value = $this->value(); |
182
|
|
|
|
183
|
|
|
if (!$value && $value != '0') return $this; |
184
|
|
|
|
185
|
|
|
if (!is_float($value)) { |
|
|
|
|
186
|
|
|
$msg = $msg ?: "this field must be a float number"; |
187
|
|
|
|
188
|
|
|
$this->addError($this->input, $msg); |
189
|
|
|
} |
190
|
|
|
return $this; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Determine if the input is a date |
195
|
|
|
* Determine if the input between the range if the $options['start'] |
196
|
|
|
* or the $options ['end'] is exists |
197
|
|
|
* |
198
|
|
|
* @param string $options |
199
|
|
|
* @param string $msg |
200
|
|
|
* @return $this |
201
|
|
|
*/ |
202
|
|
|
public function date($options = [], $msg = null) |
203
|
|
|
{ |
204
|
|
|
if ($options === false) return $this; |
|
|
|
|
205
|
|
|
|
206
|
|
|
$value = $this->value(); |
207
|
|
|
|
208
|
|
|
if (!$value && $value != '0') return $this; |
209
|
|
|
|
210
|
|
|
$options = json_encode($options); |
211
|
|
|
$options = json_decode($options); |
212
|
|
|
|
213
|
|
|
extract($this->dateMethods($options)); |
214
|
|
|
|
215
|
|
|
$date = new Date($value, $options); |
216
|
|
|
|
217
|
|
|
if (!$date->$method()) { |
218
|
|
|
$this->addError($this->input, $msg); |
219
|
|
|
} |
220
|
|
|
return $this; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
private function dateMethods($options) |
224
|
|
|
{ |
225
|
|
|
$method = null; |
226
|
|
|
$msg = null; |
227
|
|
|
if ($options->start && $options->end) { |
228
|
|
|
$method = 'isDateBetween'; |
229
|
|
|
$msg = 'this field must be between ' . $options->start . ' and ' . $options->end; |
230
|
|
|
} elseif ($options->start) { |
231
|
|
|
$method = 'minimum'; |
232
|
|
|
$msg = 'the date can\'t be under ' . $options->start; |
233
|
|
|
} elseif ($options->end) { |
234
|
|
|
$method = 'maximum'; |
235
|
|
|
$msg = 'the date can\'t be above ' . $options->end; |
236
|
|
|
} |
237
|
|
|
return array('method' => $method, 'msg'=> $msg); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Determine if the input has simple text |
242
|
|
|
* |
243
|
|
|
* @param bool $call |
244
|
|
|
* @param string $msg |
245
|
|
|
* @return $this |
246
|
|
|
*/ |
247
|
|
|
public function text($call = true, $msg = null) |
248
|
|
|
{ |
249
|
|
|
if ($call === false) return $this; |
250
|
|
|
|
251
|
|
|
$value = $this->value(); |
252
|
|
|
|
253
|
|
|
if (!$value && $value != '0') return $this; |
254
|
|
|
|
255
|
|
|
if (!is_string($value)) { |
|
|
|
|
256
|
|
|
$msg = $msg ?: 'the field must be a text'; |
257
|
|
|
|
258
|
|
|
$this->addError($this->input, $msg); |
259
|
|
|
} |
260
|
|
|
return $this; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Determine if the input has pure string |
265
|
|
|
* |
266
|
|
|
* @param bool $call |
267
|
|
|
* @param string $msg |
268
|
|
|
* @return $this |
269
|
|
|
*/ |
270
|
|
|
public function noNumbers($call = true, $msg = null) |
271
|
|
|
{ |
272
|
|
|
if ($call === false) return $this; |
273
|
|
|
|
274
|
|
|
$value = $this->value(); |
275
|
|
|
|
276
|
|
|
if (!$value && $value != '0') return $this; |
277
|
|
|
|
278
|
|
|
if (preg_match('~[0-9]~', $value)) { |
279
|
|
|
$msg = $msg ?: 'numbers are not allow'; |
280
|
|
|
|
281
|
|
|
$this->addError($this->input, $msg); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
return $this; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Determine if the input has pure string |
289
|
|
|
* |
290
|
|
|
* @param array $excepts |
291
|
|
|
* @param string $msg |
292
|
|
|
* @return $this |
293
|
|
|
*/ |
294
|
|
|
public function characters($excepts, $msg = null) |
295
|
|
|
{ |
296
|
|
|
if ($excepts === false) return $this; |
|
|
|
|
297
|
|
|
|
298
|
|
|
$value = $this->value(); |
299
|
|
|
|
300
|
|
|
if (!$value && $value != '0') return $this; |
301
|
|
|
|
302
|
|
|
extract($this->charactersvariables($excepts, $value)); |
303
|
|
|
|
304
|
|
|
if ($this->checkForErrorsInCharactersMethods($methods, $msg)) { |
305
|
|
|
return $this; |
306
|
|
|
} |
307
|
|
|
$re = "/^[0-9\\s$chars$langsRegex]*$/u"; |
308
|
|
|
if (!preg_match($re, $value)) { |
309
|
|
|
$msg = $this->charactersMsg($chars, $languages, $msg); |
310
|
|
|
$this->addError($this->input, $msg); |
311
|
|
|
} |
312
|
|
|
return $this; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
private function charactersMsg($chars, $languages, $msg) |
316
|
|
|
{ |
317
|
|
|
$chars = $this->charactersFormatCharsMsg($chars); |
318
|
|
|
$languages = $languages ? "[ $languages ]" : ''; |
319
|
|
|
$msg = $msg ?: "just $chars $languages letters can be used"; |
320
|
|
|
return $msg; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
private function charactersFormatCharsRegex($chars) |
324
|
|
|
{ |
325
|
|
|
if (strlen($chars) > 1) { |
326
|
|
|
$chars = str_split($chars); |
327
|
|
|
$chars = "\\" . implode('|\\', $chars); |
328
|
|
|
} |
329
|
|
|
return $chars; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
private function charactersFormatCharsMsg($chars) |
333
|
|
|
{ |
334
|
|
|
$chars = explode('\\', $chars); |
335
|
|
|
$chars = implode('', $chars); |
336
|
|
|
$chars = $chars ? "[ $chars ] and" : ''; |
337
|
|
|
return $chars; |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
private function charactersvariables($excepts, $value) |
341
|
|
|
{ |
342
|
|
|
$characters = new Characters($excepts); |
343
|
|
|
$chars = $characters->getChars(); |
344
|
|
|
$langsRegex = $characters->getLangsRegex(); |
345
|
|
|
$languages = $characters->getLanguages(); |
346
|
|
|
$times = $characters->getTimes(); |
347
|
|
|
$atFirst = $characters->getAtFirst(); |
348
|
|
|
$atEnd = $characters->getAtEnd(); |
349
|
|
|
$between = $characters->getBetween(); |
350
|
|
|
$methods = $this->charactersMethods([ |
351
|
|
|
"times" => $times, |
352
|
|
|
"atFirst" => $atFirst, |
353
|
|
|
"atEnd" => $atEnd, |
354
|
|
|
"between" => $between, |
355
|
|
|
"chars" => $chars, |
356
|
|
|
"value" => $value, |
357
|
|
|
]); |
358
|
|
|
|
359
|
|
|
return [ |
360
|
|
|
'characters' => $characters, |
361
|
|
|
'chars' => $chars, |
362
|
|
|
'langsRegex' => $langsRegex, |
363
|
|
|
'languages' => $languages, |
364
|
|
|
'times' => $times, |
365
|
|
|
'atFirst' => $atFirst, |
366
|
|
|
'atEnd' => $atEnd, |
367
|
|
|
'between' => $between, |
368
|
|
|
'methods' => $methods, |
369
|
|
|
]; |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
private function charactersMethods($args) |
373
|
|
|
{ |
374
|
|
|
extract($args); |
375
|
|
|
return [ |
376
|
|
|
'charactersTimes' => [ |
377
|
|
|
[$times, $chars, $value], |
378
|
|
|
'charachters are too many', |
379
|
|
|
], |
380
|
|
|
'charactersAtFirst' => [ |
381
|
|
|
[$atFirst, $chars, $value], |
382
|
|
|
'charachters cant be at the first', |
383
|
|
|
], |
384
|
|
|
'charactersAtEnd' => [ |
385
|
|
|
[$atEnd, $chars, $value], |
386
|
|
|
'charachters cant be at the end', |
387
|
|
|
], |
388
|
|
|
'charactersBetween' => [ |
389
|
|
|
[$between, $chars, $value], |
390
|
|
|
'charachters cant be between', |
391
|
|
|
], |
392
|
|
|
]; |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
private function charactersTimes($times, $chars, $value) |
396
|
|
|
{ |
397
|
|
|
if ($times > 0) { |
398
|
|
|
$chars = $this->charactersFormatCharsRegex($chars); |
399
|
|
|
$re = "/($chars)/"; |
400
|
|
|
if (preg_match($re, $value) && preg_match_all($re, $value) > $times) { |
401
|
|
|
return true; |
402
|
|
|
} |
403
|
|
|
return false; |
404
|
|
|
} |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
private function charactersAtFirst($atFirst, $chars, $value) |
408
|
|
|
{ |
409
|
|
|
if ($atFirst === false) { |
410
|
|
|
$chars = $this->charactersFormatCharsRegex($chars); |
411
|
|
|
$re = "/^($chars" . "|\\s+\\$chars)/"; |
412
|
|
|
if (preg_match_all($re, $value)) { |
413
|
|
|
return true; |
414
|
|
|
} |
415
|
|
|
return false; |
416
|
|
|
} |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
private function charactersAtEnd($atEnd, $chars, $value) |
420
|
|
|
{ |
421
|
|
|
if ($atEnd === false) { |
422
|
|
|
$chars = $this->charactersFormatCharsRegex($chars); |
423
|
|
|
$re = "/($chars" . "|\\$chars\\s+)$/"; |
424
|
|
|
if (preg_match_all($re, $value)) { |
425
|
|
|
return true; |
426
|
|
|
} |
427
|
|
|
return false; |
428
|
|
|
} |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
private function charactersBetween($between, $chars, $value) |
432
|
|
|
{ |
433
|
|
|
if ($between === false) { |
434
|
|
|
$chars = $this->charactersFormatCharsRegex($chars); |
435
|
|
|
$re = "/.+(${chars})(.+|\\s)/"; |
436
|
|
|
if (preg_match_all($re, $value)) { |
437
|
|
|
return true; |
438
|
|
|
} |
439
|
|
|
return false; |
440
|
|
|
} |
441
|
|
|
} |
442
|
|
|
|
443
|
|
|
private function checkForErrorsInCharactersMethods($methods, $msg) |
444
|
|
|
{ |
445
|
|
|
foreach ($methods as $method => $options) { |
446
|
|
|
if (call_user_func_array(array($this, $method), $options[0])) { |
447
|
|
|
$msg = $msg ?: $options[1]; |
448
|
|
|
$this->addError($this->input, $msg); |
449
|
|
|
return true; |
450
|
|
|
} |
451
|
|
|
} |
452
|
|
|
return false; |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
/** |
456
|
|
|
* Determine if the input has spaces between the letters or the words |
457
|
|
|
* |
458
|
|
|
* @param bool $call |
459
|
|
|
* @param string $msg |
460
|
|
|
* @return $this |
461
|
|
|
*/ |
462
|
|
|
public function noSpaces($call = true, $msg = null) |
463
|
|
|
{ |
464
|
|
|
if ($call === false) return $this; |
465
|
|
|
|
466
|
|
|
$value = $this->value(); |
467
|
|
|
|
468
|
|
|
if (!$value && $value != '0') return $this; |
469
|
|
|
|
470
|
|
|
if (preg_match('/\s/', $value)) { |
471
|
|
|
$msg = $msg ?: 'spaces are not allow'; |
472
|
|
|
|
473
|
|
|
$this->addError($this->input, $msg); |
474
|
|
|
} |
475
|
|
|
return $this; |
476
|
|
|
} |
477
|
|
|
|
478
|
|
|
/** |
479
|
|
|
* Determine if the given input has the value that are passed |
480
|
|
|
* |
481
|
|
|
* @param array $characters |
482
|
|
|
* @param string $msg |
483
|
|
|
* @return $this |
484
|
|
|
*/ |
485
|
|
|
public function containJust($characters = [], $msg = null) |
486
|
|
|
{ |
487
|
|
|
if ($characters === false) return $this; |
|
|
|
|
488
|
|
|
|
489
|
|
|
$value = $this->value(); |
490
|
|
|
|
491
|
|
|
if (!$value && $value != '0') return $this; |
492
|
|
|
|
493
|
|
|
$allowedCharacters = new AllowedCharacters($this->app, $characters); |
494
|
|
|
$characters = $allowedCharacters->getCharacters(); |
495
|
|
|
|
496
|
|
|
if (!in_array($value, $characters)) { |
497
|
|
|
$msg = $msg ?: 'wrong value'; |
498
|
|
|
$this->addError($this->input, $msg); |
499
|
|
|
} |
500
|
|
|
return $this; |
501
|
|
|
} |
502
|
|
|
|
503
|
|
|
/** |
504
|
|
|
* Determine if the input value should equal length |
505
|
|
|
* |
506
|
|
|
* @param int $length |
507
|
|
|
* @param string $msg |
508
|
|
|
* @return $this |
509
|
|
|
*/ |
510
|
|
|
public function length($length = null, $msg = null) |
511
|
|
|
{ |
512
|
|
|
if ($length === false) return $this; |
|
|
|
|
513
|
|
|
|
514
|
|
|
$value = $this->value(); |
515
|
|
|
|
516
|
|
|
if (!$value && $value != '0') return $this; |
517
|
|
|
|
518
|
|
|
if (strlen($value) !== $length) { |
519
|
|
|
$msg = $msg ?: `this field can be just ${length} charachter`; |
520
|
|
|
|
521
|
|
|
$this->addError($this->input, $msg); |
522
|
|
|
} |
523
|
|
|
return $this; |
524
|
|
|
} |
525
|
|
|
|
526
|
|
|
/** |
527
|
|
|
* Determine if the input value should be at most the given length |
528
|
|
|
* |
529
|
|
|
* @param int $length |
530
|
|
|
* @param string $msg |
531
|
|
|
* @return $this |
532
|
|
|
*/ |
533
|
|
|
public function maxLen($length = null, $msg = null) |
534
|
|
|
{ |
535
|
|
|
if ($length === false) return $this; |
|
|
|
|
536
|
|
|
|
537
|
|
|
$value = $this->value(); |
538
|
|
|
|
539
|
|
|
if (!$value && $value != '0') return $this; |
540
|
|
|
|
541
|
|
|
if (strlen($value) > $length) { |
542
|
|
|
$msg = $msg ?: "this field can be maximum $length charachter"; |
543
|
|
|
|
544
|
|
|
$this->addError($this->input, $msg); |
545
|
|
|
} |
546
|
|
|
return $this; |
547
|
|
|
} |
548
|
|
|
|
549
|
|
|
/** |
550
|
|
|
* Determine if the input value should be at least the given length |
551
|
|
|
* |
552
|
|
|
* @param int $length |
553
|
|
|
* @param string $msg |
554
|
|
|
* @return $this |
555
|
|
|
*/ |
556
|
|
|
public function minLen($length = null, $msg = null) |
557
|
|
|
{ |
558
|
|
|
if ($length === false) return $this; |
|
|
|
|
559
|
|
|
|
560
|
|
|
$value = $this->value(); |
561
|
|
|
|
562
|
|
|
if (!$value && $value != '0') return $this; |
563
|
|
|
|
564
|
|
|
if (strlen($value) < $length) { |
565
|
|
|
$msg = $msg ?: "this field can be minimum $length charachter"; |
566
|
|
|
|
567
|
|
|
$this->addError($this->input, $msg); |
568
|
|
|
} |
569
|
|
|
return $this; |
570
|
|
|
} |
571
|
|
|
|
572
|
|
|
/** |
573
|
|
|
* Determine if the $input matches the given input |
574
|
|
|
* |
575
|
|
|
* @param string $input |
576
|
|
|
* @param string $msg |
577
|
|
|
* @return $this |
578
|
|
|
*/ |
579
|
|
|
public function match($input, $msg = null) |
580
|
|
|
{ |
581
|
|
|
if ($input === false) return $this; |
|
|
|
|
582
|
|
|
|
583
|
|
|
$value = $this->value(); |
584
|
|
|
|
585
|
|
|
$valueConfirm = $this->app->request->post($input); |
586
|
|
|
|
587
|
|
|
if ($value && $valueConfirm) { |
588
|
|
|
if ($value !== $valueConfirm) { |
589
|
|
|
$msg = $msg ?: 'passwords doesn\'t match'; |
590
|
|
|
|
591
|
|
|
$this->addError('match', $msg); |
592
|
|
|
} |
593
|
|
|
} |
594
|
|
|
return $this; |
595
|
|
|
} |
596
|
|
|
|
597
|
|
|
/** |
598
|
|
|
* Determine if the input is unique in database |
599
|
|
|
* |
600
|
|
|
* @param array $data |
601
|
|
|
* @param string $msg |
602
|
|
|
* @return $this |
603
|
|
|
*/ |
604
|
|
|
public function unique($data = [], $msg = null) |
605
|
|
|
{ |
606
|
|
|
if ($data === false) return $this; |
|
|
|
|
607
|
|
|
|
608
|
|
|
$value = $this->value(); |
609
|
|
|
|
610
|
|
|
if (!$data) return $this; |
|
|
|
|
611
|
|
|
|
612
|
|
|
if (is_array($data)) { |
|
|
|
|
613
|
|
|
list($table, $column) = $data; |
614
|
|
|
} else { |
615
|
|
|
$table = $data; |
616
|
|
|
$column = $this->input; |
617
|
|
|
} |
618
|
|
|
|
619
|
|
|
$result = $this->app->db->select($column)->from($table)->where($column . ' = ? ', $value)->fetch(); |
620
|
|
|
|
621
|
|
|
if ($result) { |
622
|
|
|
$msg = $msg ?: sprintf('%s is already exist', ucfirst($this->input)); |
623
|
|
|
|
624
|
|
|
$this->addError($this->input, $msg); |
625
|
|
|
} |
626
|
|
|
return $this; |
627
|
|
|
} |
628
|
|
|
|
629
|
|
|
/** |
630
|
|
|
* Determine if all inputs are valid |
631
|
|
|
* |
632
|
|
|
* @return bool |
633
|
|
|
*/ |
634
|
|
|
public function passes() |
635
|
|
|
{ |
636
|
|
|
return empty($this->errors); |
637
|
|
|
} |
638
|
|
|
|
639
|
|
|
/** |
640
|
|
|
* Determine if there are any invalid inputs |
641
|
|
|
* |
642
|
|
|
* @return bool |
643
|
|
|
*/ |
644
|
|
|
public function fails() |
645
|
|
|
{ |
646
|
|
|
return !empty($this->errors); |
647
|
|
|
} |
648
|
|
|
|
649
|
|
|
/** |
650
|
|
|
* Determine if the given input has previous errors |
651
|
|
|
* |
652
|
|
|
* @param string $input |
653
|
|
|
*/ |
654
|
|
|
private function hasError($input) |
655
|
|
|
{ |
656
|
|
|
return array_key_exists($input, $this->errors); |
657
|
|
|
} |
658
|
|
|
|
659
|
|
|
/** |
660
|
|
|
* Add input error |
661
|
|
|
* |
662
|
|
|
* @param string $inputName |
663
|
|
|
* @param string $msg |
664
|
|
|
* @return void |
665
|
|
|
*/ |
666
|
|
|
public function addError($input, $msg) |
667
|
|
|
{ |
668
|
|
|
if (!$this->hasError($input)) $this->errors[$input] = $msg; |
669
|
|
|
} |
670
|
|
|
|
671
|
|
|
/** |
672
|
|
|
* Get all errors |
673
|
|
|
* |
674
|
|
|
* @return array |
675
|
|
|
*/ |
676
|
|
|
public function getErrors() |
677
|
|
|
{ |
678
|
|
|
return $this->errors; |
679
|
|
|
} |
680
|
|
|
} |
681
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths