-
Notifications
You must be signed in to change notification settings - Fork 0
/
Validator.php
executable file
·2541 lines (2223 loc) · 68.4 KB
/
Validator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Qubus\Validation
*
* @link https://github.com/QubusPHP/validation
* @copyright 2020
* @author Joshua Parker <joshua@joshuaparker.dev>
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
declare(strict_types=1);
namespace Qubus\Validation;
use BadMethodCallException;
use Closure;
use DateTime;
use DateTimeZone;
use Qubus\Exception\Data\TypeException;
use Qubus\Exception\Exception;
use Qubus\Validation\Interfaces\PresenceVerifier;
use Qubus\Validation\Translators\StringTranslator;
use RuntimeException;
use function array_combine;
use function array_key_exists;
use function array_keys;
use function array_map;
use function array_merge;
use function array_slice;
use function array_values;
use function call_user_func;
use function call_user_func_array;
use function checkdate;
use function checkdnsrr;
use function count;
use function date_parse;
use function date_parse_from_format;
use function explode;
use function filter_var;
use function func_get_args;
use function function_exists;
use function implode;
use function in_array;
use function is_array;
use function is_numeric;
use function is_string;
use function is_uploaded_file;
use function mb_strlen;
use function method_exists;
use function pathinfo;
use function preg_match;
use function Qubus\Support\Helpers\array_dot;
use function Qubus\Support\Helpers\is_false__;
use function Qubus\Support\Helpers\is_null__;
use function Qubus\Support\Helpers\return_array;
use function Qubus\Support\Helpers\snake_case;
use function Qubus\Support\Helpers\studly_case;
use function str_getcsv;
use function str_replace;
use function strlen;
use function strpos;
use function strtolower;
use function strtotime;
use function strtoupper;
use function substr;
use function trim;
use function ucfirst;
use const FILTER_FLAG_IPV4;
use const FILTER_FLAG_IPV6;
use const FILTER_VALIDATE_EMAIL;
use const FILTER_VALIDATE_INT;
use const FILTER_VALIDATE_IP;
use const FILTER_VALIDATE_URL;
use const PATHINFO_EXTENSION;
class Validator implements Validatable
{
/**
* The StringTranslator implementation.
*/
protected StringTranslator $translator;
/**
* The Presence Verifier implementation.
*/
protected ?PresenceVerifier $presenceVerifier;
/**
* The failed validation rules.
*
* @var array $failedRules
*/
protected array $failedRules = [];
/**
* The messages.
*/
protected ?MessageBag $messages = null;
/**
* The data under validation.
*
* @var array $data
*/
protected array $data;
/**
* The files under validation.
*
* @var array $files
*/
protected array $files = [];
/**
* The rules to be applied to the data.
*
* @var array $rules
*/
protected array $rules;
/**
* All the registered "after" callbacks.
*
* @var array $after
*/
protected array $after = [];
/**
* The array of custom error messages.
*
* @var array $customMessages
*/
protected array $customMessages = [];
/**
* The array of fallback error messages.
*
* @var array $fallbackMessages
*/
protected array $fallbackMessages = [];
/**
* The array of custom attribute names.
*
* @var array $customAttributes
*/
protected array $customAttributes = [];
/**
* The array of custom displayable values.
*
* @var array $customValues
*/
protected array $customValues = [];
/**
* All the custom validator extensions.
*
* @var array $extensions
*/
protected array $extensions = [];
/**
* All the custom replacer extensions.
*
* @var array $replacers
*/
protected array $replacers = [];
/**
* The size related validation rules.
*
* @var array $sizeRules
*/
protected array $sizeRules = ['Size', 'Between', 'Min', 'Max'];
/**
* The numeric related validation rules.
*
* @var array $numericRules
*/
protected array $numericRules = ['Numeric', 'Integer'];
/**
* The validation rules that imply the field is required.
*
* @var array $implicitRules
*/
protected array $implicitRules = [
'Required',
'Filled',
'RequiredWith',
'RequiredWithAll',
'RequiredWithout',
'RequiredWithoutAll',
'RequiredIf',
'Accepted',
];
/**
* Create a new Validator instance.
*
* @param StringTranslator $translator
* @param array $data
* @param array $rules
* @param array $messages
* @param array $customAttributes
*/
public function __construct(
StringTranslator $translator,
array $data,
array $rules,
array $messages = [],
array $customAttributes = []
) {
$this->translator = $translator;
$this->customMessages = $messages;
$this->data = $this->parseData($data);
$this->rules = $this->explodeRules($rules);
$this->customAttributes = $customAttributes;
}
/**
* Parse the data and hydrate the files array.
*
* @param array $data
* @return array
*/
protected function parseData(array $data): array
{
$this->files = [];
foreach ($data as $key => $value) {
// If this value is an instance of the Qubus\Http File class we will
// remove it from the data array and add it to the files array, which
// we use to conveniently separate out these files from other data.
if (in_array($value, $_FILES, true)) {
$this->files[$key] = $value;
unset($data[$key]);
}
}
return $data;
}
/**
* Explode the rules into an array of rules.
*
* @param array|string $rules
* @return array
*/
protected function explodeRules(array|string $rules): array
{
foreach ($rules as $key => &$rule) {
$rule = is_string($rule) ? explode('|', $rule) : $rule;
}
return $rules;
}
/**
* After an after validation callback.
*
* @param callable|string $callback
* @return $this
*/
public function after(callable|string $callback): static
{
$this->after[] = fn () => call_user_func($callback, [], 'validate');
return $this;
}
/**
* Add conditions to a given field based on a Closure.
*
* @param string $attribute
* @param array|string $rules
* @param callable $callback
*/
public function sometimes(string $attribute, array|string $rules, callable $callback): void
{
$payload = array_merge($this->data, $this->files);
if (call_user_func($callback, $payload)) {
foreach ((array) $attribute as $key) {
$this->mergeRules($key, $rules);
}
}
}
/**
* Define a set of rules that apply to each element in an array attribute.
*
* @param string $attribute
* @param array|string $rules
* @throws TypeException
*/
public function each(string $attribute, array|string $rules): void
{
$data = return_array($this->data, $attribute);
if (! is_array($data)) {
if ($this->hasRule($attribute, 'Array')) {
return;
}
throw new TypeException('Attribute for each() must be an array.');
}
foreach ($data as $dataKey => $dataValue) {
foreach ($rules as $ruleValue) {
$this->mergeRules("$attribute.$dataKey", $ruleValue);
}
}
}
/**
* Merge additional rules into a given attribute.
*
* @param string $attribute
* @param array|string $rules
*/
public function mergeRules(string $attribute, array|string $rules): void
{
$current = $this->rules[$attribute] ?? [];
$merge = head($this->explodeRules([$rules]));
$this->rules[$attribute] = array_merge($current, $merge);
}
/**
* Determine if the data passes the validation rules.
*/
public function passes(): bool
{
$this->messages = new MessageBag();
// We'll spin through each rule, validating the attributes attached to that
// rule. Any error messages will be added to the containers with each of
// the other error messages, returning true if we don't have messages.
foreach ($this->rules as $attribute => $rules) {
foreach ($rules as $rule) {
$this->validate($attribute, $rule);
}
}
// Here we will spin through all of the "after" hooks on this validator and
// fire them off. This gives the callbacks a chance to perform all kinds
// of other validation that needs to get wrapped up in this operation.
foreach ($this->after as $after) {
call_user_func($after);
}
return count($this->messages->all()) === 0;
}
/**
* Determine if the data fails the validation rules.
*/
public function fails(): bool
{
return ! $this->passes();
}
/**
* Validate a given attribute against a rule.
*/
protected function validate(string $attribute, string $rule): void
{
[$rule, $parameters] = $this->parseRule($rule);
if ($rule === '') {
return;
}
// We will get the value for the given attribute from the array of data and then
// verify that the attribute is indeed validatable. Unless the rule implies
// that the attribute is required, rules are not run for missing values.
$value = $this->getValue($attribute);
$validatable = $this->isValidatable($rule, $attribute, $value);
$method = "validate{$rule}";
if ($validatable && ! $this->$method($attribute, $value, $parameters, $this)) {
$this->addFailure($attribute, $rule, $parameters);
}
}
/**
* Get the value of a given attribute.
*
* @param string $attribute
* @return array|null
*/
protected function getValue(string $attribute): ?array
{
if (null !== ($value = return_array($this->data, $attribute))) {
return $value;
} elseif (null !== ($value = return_array($this->files, $attribute))) {
return $value;
}
return null;
}
/**
* Determine if the attribute is validatable.
*
* @param string $rule
* @param string $attribute
* @param mixed $value
* @return bool
*/
protected function isValidatable(string $rule, string $attribute, mixed $value): bool
{
return $this->presentOrRuleIsImplicit($rule, $attribute, $value) &&
$this->passesOptionalCheck($attribute);
}
/**
* Determine if the field is present, or the rule implies required.
*
* @param string $rule
* @param string $attribute
* @param mixed $value
* @return bool
*/
protected function presentOrRuleIsImplicit(string $rule, string $attribute, mixed $value): bool
{
return $this->validateRequired($attribute, $value) || $this->isImplicit($rule);
}
/**
* Determine if the attribute passes any optional check.
*/
protected function passesOptionalCheck(string $attribute): bool
{
if ($this->hasRule($attribute, ['Sometimes'])) {
return array_key_exists($attribute, array_dot($this->data))
|| array_key_exists($attribute, $this->files);
}
return true;
}
/**
* Determine if a given rule implies the attribute is required.
*/
protected function isImplicit(string $rule): bool
{
return in_array($rule, $this->implicitRules, true);
}
/**
* Add a failed rule and error message to the collection.
*
* @param string $attribute
* @param string $rule
* @param array $parameters
*/
protected function addFailure(string $attribute, string $rule, array $parameters): void
{
$this->addError($attribute, $rule, $parameters);
$this->failedRules[$attribute][snake_case($rule)] = $parameters;
}
/**
* Add an error message to the validator's collection of messages.
*
* @param string $attribute
* @param string $rule
* @param array $parameters
*/
protected function addError(string $attribute, string $rule, array $parameters): void
{
$message = $this->getMessage($attribute, $rule);
$message = $this->doReplacements($message, $attribute, $rule, $parameters);
$this->messages->add($attribute, $message);
}
/**
* "Validate" optional attributes.
*
* Always returns true, just lets us put sometimes in rules.
*/
protected function validateSometimes(): bool
{
return true;
}
/**
* Validate that a required attribute exists.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
protected function validateRequired(string $attribute, mixed $value): bool
{
if (is_null__($value)) {
return false;
} elseif (is_string($value) && trim($value) === '') {
return false;
} elseif (is_array($value) && count($value) < 1) {
return false;
} elseif (in_array($value, $_FILES, true)) {
return (string) $value['tmp_name'] !== '';
}
return true;
}
/**
* Validate the given attribute is filled if it is present.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
protected function validateFilled(string $attribute, mixed $value): bool
{
if (array_key_exists($attribute, $this->data) || array_key_exists($attribute, $this->files)) {
return $this->validateRequired($attribute, $value);
} else {
return true;
}
}
/**
* Determine if any of the given attributes fail the required test.
*
* @param array $attributes
* @return bool
*/
protected function anyFailingRequired(array $attributes): bool
{
foreach ($attributes as $key) {
if (! $this->validateRequired($key, $this->getValue($key))) {
return true;
}
}
return false;
}
/**
* Determine if all the given attributes fail the required test.
*
* @param array $attributes
* @return bool
*/
protected function allFailingRequired(array $attributes): bool
{
foreach ($attributes as $key) {
if ($this->validateRequired($key, $this->getValue($key))) {
return false;
}
}
return true;
}
/**
* Validate that an attribute exists when any other attribute exists.
*
* @param string $attribute
* @param mixed $value
* @param mixed $parameters
* @return bool
*/
protected function validateRequiredWith(string $attribute, mixed $value, mixed $parameters): bool
{
if (! $this->allFailingRequired($parameters)) {
return $this->validateRequired($attribute, $value);
}
return true;
}
/**
* Validate that an attribute exists when all other attributes exists.
*
* @param string $attribute
* @param mixed $value
* @param mixed $parameters
* @return bool
*/
protected function validateRequiredWithAll(string $attribute, mixed $value, mixed $parameters): bool
{
if (! $this->anyFailingRequired($parameters)) {
return $this->validateRequired($attribute, $value);
}
return true;
}
/**
* Validate that an attribute exists when another attribute does not.
*
* @param string $attribute
* @param mixed $value
* @param mixed $parameters
* @return bool
*/
protected function validateRequiredWithout(string $attribute, mixed $value, array $parameters): bool
{
if ($this->anyFailingRequired($parameters)) {
return $this->validateRequired($attribute, $value);
}
return true;
}
/**
* Validate that an attribute exists when all other attributes do not.
*
* @param string $attribute
* @param mixed $value
* @param mixed $parameters
* @return bool
*/
protected function validateRequiredWithoutAll(string $attribute, mixed $value, array $parameters): bool
{
if ($this->allFailingRequired($parameters)) {
return $this->validateRequired($attribute, $value);
}
return true;
}
/**
* Validate that an attribute exists when another attribute has a given value.
*
* @param string $attribute
* @param mixed $value
* @param mixed $parameters
* @return bool
* @throws TypeException
*/
protected function validateRequiredIf(string $attribute, mixed $value, array $parameters): bool
{
$this->requireParameterCount(2, $parameters, 'required_if');
$data = return_array($this->data, $parameters[0]);
$values = array_slice($parameters, 1);
if (in_array($data, $values)) {
return $this->validateRequired($attribute, $value);
}
return true;
}
/**
* Get the number of attributes in a list that are present.
*
* @param array $attributes
* @return int
*/
protected function getPresentCount(array $attributes): int
{
$count = 0;
foreach ($attributes as $key) {
if (return_array($this->data, $key) || return_array($this->files, $key)) {
++$count;
}
}
return $count;
}
/**
* Validate that an attribute has a matching confirmation.
*
* @param string $attribute
* @param mixed $value
* @return bool
* @throws TypeException
*/
protected function validateConfirmed(string $attribute, mixed $value): bool
{
return $this->validateSame($attribute, $value, [$attribute . '_confirmation']);
}
/**
* Validate that two attributes match.
*
* @param string $attribute
* @param mixed $value
* @param array $parameters
* @return bool
* @throws TypeException
*/
protected function validateSame(string $attribute, mixed $value, array $parameters): bool
{
$this->requireParameterCount(1, $parameters, 'same');
$other = return_array($this->data, $parameters[0]);
return isset($other) && $value === $other;
}
/**
* Validate that an attribute is different from another attribute.
*
* @param string $attribute
* @param mixed $value
* @param array $parameters
* @return bool
* @throws TypeException
*/
protected function validateDifferent(string $attribute, mixed $value, array $parameters): bool
{
$this->requireParameterCount(1, $parameters, 'different');
$other = return_array($this->data, $parameters[0]);
return isset($other) && $value !== $other;
}
/**
* Validate that an attribute was "accepted".
*
* This validation rule implies the attribute is "required".
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
protected function validateAccepted(string $attribute, mixed $value): bool
{
$acceptable = ['yes', 'on', '1', 1, true, 'true'];
return $this->validateRequired($attribute, $value) && in_array($value, $acceptable, true);
}
/**
* Validate that an attribute is a boolean.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
protected function validateBoolean(string $attribute, mixed $value): bool
{
$acceptable = [true, false, 0, 1, '0', '1'];
return in_array($value, $acceptable, true);
}
/**
* Validate that an attribute is an array.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
protected function validateArray(string $attribute, mixed $value): bool
{
return is_array($value);
}
/**
* Validate that an attribute is numeric.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
protected function validateNumeric(string $attribute, mixed $value): bool
{
return is_numeric($value);
}
/**
* Validate that an attribute is an integer.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
protected function validateInteger(string $attribute, mixed $value): bool
{
return filter_var($value, FILTER_VALIDATE_INT) !== false;
}
/**
* Validate that an attribute has a given number of digits.
*
* @param string $attribute
* @param mixed $value
* @param array $parameters
* @return bool
* @throws TypeException
*/
protected function validateDigits(string $attribute, mixed $value, array $parameters): bool
{
$this->requireParameterCount(1, $parameters, 'digits');
return ! preg_match('/[^0-9]/', (string) $value)
&& $this->validateNumeric($attribute, $value)
&& strlen((string) $value) === (int) $parameters[0];
}
/**
* Validate that an attribute is between a given number of digits.
*
* @param string $attribute
* @param mixed $value
* @param array $parameters
* @return bool
* @throws TypeException
*/
protected function validateDigitsBetween(string $attribute, mixed $value, array $parameters): bool
{
$this->requireParameterCount(2, $parameters, 'digits_between');
$length = strlen((string) $value);
return $length >= $parameters[0] && $length <= $parameters[1];
}
/**
* Validate the size of an attribute.
*
* @param string $attribute
* @param mixed $value
* @param array $parameters
* @return bool
* @throws TypeException
*/
protected function validateSize(string $attribute, mixed $value, array $parameters): bool
{
$this->requireParameterCount(1, $parameters, 'size');
return $this->getSize($attribute, $value) === (int) $parameters[0];
}
/**
* Validate the size of an attribute is between a set of values.
*
* @param string $attribute
* @param mixed $value
* @param array $parameters
* @return bool
* @throws TypeException
*/
protected function validateBetween(string $attribute, mixed $value, array $parameters): bool
{
$this->requireParameterCount(2, $parameters, 'between');
$size = $this->getSize($attribute, $value);
return $size >= $parameters[0] && $size <= $parameters[1];
}
/**
* Validate the size of an attribute is greater than a minimum value.
*
* @param string $attribute
* @param mixed $value
* @param array $parameters
* @return bool
* @throws TypeException
*/
protected function validateMin(string $attribute, mixed $value, array $parameters): bool
{
$this->requireParameterCount(1, $parameters, 'min');
return $this->getSize($attribute, $value) >= $parameters[0];
}
/**
* Validate the size of an attribute is less than a maximum value.
*
* @param string $attribute
* @param mixed $value
* @param array $parameters
* @return bool
* @throws TypeException
*/
protected function validateMax(string $attribute, mixed $value, array $parameters): bool
{
$this->requireParameterCount(1, $parameters, 'max');
if (! empty($value['tmp_name']) && is_uploaded_file($value['tmp_name']) && $value['error']) {
return false;
}
return $this->getSize($attribute, $value) <= $parameters[0];
}
/**
* Get the size of an attribute.
*
* @param string $attribute
* @param mixed $value
* @return array|int|float|null
*/
protected function getSize(string $attribute, mixed $value): array|int|null|float
{
$hasNumeric = $this->hasRule($attribute, $this->numericRules);
// This method will determine if the attribute is a number, string, or file and
// return the proper size accordingly. If it is a number, then number itself
// is the size. If it is a file, we take kilobytes, and for a string the
// entire length of the string will be considered the attribute size.
if (is_numeric($value) && $hasNumeric) {
return return_array($this->data, $attribute);
} elseif (is_array($value)) {
return count($value);
} elseif (in_array($value, $_FILES, true)) {
return $value->getSize() / 1024;
} else {
return $this->getStringSize($value);
}
}
/**
* Get the size of a string.
*
* @param string $value
* @return int
*/
protected function getStringSize($value): int
{
if (function_exists('mb_strlen')) {
return mb_strlen((string) $value);
}
return strlen($value);
}
/**
* Validate an attribute is contained within a list of values.
*
* @param string $attribute
* @param mixed $value
* @param array $parameters
* @return bool
*/
protected function validateIn(string $attribute, mixed $value, array $parameters): bool
{
return in_array((string) $value, $parameters, true);
}
/**
* Validate an attribute is not contained within a list of values.
*
* @param string $attribute
* @param mixed $value
* @param array $parameters
* @return bool
*/
protected function validateNotIn(string $attribute, mixed $value, array $parameters): bool
{
return ! $this->validateIn($attribute, $value, $parameters);
}
/**
* Validate the uniqueness of an attribute value on a given database table.
*
* If a database column is not specified, the attribute will be used.
*
* @param string $attribute
* @param mixed $value
* @param array $parameters
* @return bool
* @throws TypeException
*/
protected function validateUnique(string $attribute, mixed $value, array $parameters): bool
{
$this->requireParameterCount(1, $parameters, 'unique');
$table = $parameters[0];
// The second parameter position holds the name of the column that needs to
// be verified as unique. If this parameter isn't specified we will just
// assume that this column to be verified shares the attribute's name.
$column = $parameters[1] ?? $attribute;
[$idColumn, $id] = [null, null];
if (isset($parameters[2])) {
[$idColumn, $id] = $this->getUniqueIds($parameters);
if (strtolower($id) === 'null') {
$id = null;