-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Validation.php
996 lines (848 loc) · 30 KB
/
Validation.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
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Validation;
use Closure;
use CodeIgniter\Database\BaseConnection;
use CodeIgniter\HTTP\Exceptions\HTTPException;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\Method;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\Validation\Exceptions\ValidationException;
use CodeIgniter\View\RendererInterface;
use Config\Validation as ValidationConfig;
use InvalidArgumentException;
use LogicException;
use TypeError;
/**
* Validator
*
* @see \CodeIgniter\Validation\ValidationTest
*/
class Validation implements ValidationInterface
{
/**
* Files to load with validation functions.
*
* @var array
*/
protected $ruleSetFiles;
/**
* The loaded instances of our validation files.
*
* @var array
*/
protected $ruleSetInstances = [];
/**
* Stores the actual rules that should be run against $data.
*
* @var array<array-key, array{label?: string, rules: list<string>}>
*
* [
* field1 => [
* 'label' => label,
* 'rules' => [
* rule1, rule2, ...
* ],
* ],
* ]
*/
protected $rules = [];
/**
* The data that should be validated,
* where 'key' is the alias, with value.
*
* @var array
*/
protected $data = [];
/**
* The data that was actually validated.
*
* @var array
*/
protected $validated = [];
/**
* Any generated errors during validation.
* 'key' is the alias, 'value' is the message.
*
* @var array
*/
protected $errors = [];
/**
* Stores custom error message to use
* during validation. Where 'key' is the alias.
*
* @var array
*/
protected $customErrors = [];
/**
* Our configuration.
*
* @var ValidationConfig
*/
protected $config;
/**
* The view renderer used to render validation messages.
*
* @var RendererInterface
*/
protected $view;
/**
* Validation constructor.
*
* @param ValidationConfig $config
*/
public function __construct($config, RendererInterface $view)
{
$this->ruleSetFiles = $config->ruleSets;
$this->config = $config;
$this->view = $view;
$this->loadRuleSets();
}
/**
* Runs the validation process, returning true/false determining whether
* validation was successful or not.
*
* @param array|null $data The array of data to validate.
* @param string|null $group The predefined group of rules to apply.
* @param array|BaseConnection|non-empty-string|null $dbGroup The database group to use.
*/
public function run(?array $data = null, ?string $group = null, $dbGroup = null): bool
{
if ($data === null) {
$data = $this->data;
} else {
// Store data to validate.
$this->data = $data;
}
// `DBGroup` is a reserved name. For is_unique and is_not_unique
$data['DBGroup'] = $dbGroup;
$this->loadRuleGroup($group);
// If no rules exist, we return false to ensure
// the developer didn't forget to set the rules.
if ($this->rules === []) {
return false;
}
// Replace any placeholders (e.g. {id}) in the rules with
// the value found in $data, if any.
$this->rules = $this->fillPlaceholders($this->rules, $data);
// Need this for searching arrays in validation.
helper('array');
// Run through each rule. If we have any field set for
// this rule, then we need to run them through!
foreach ($this->rules as $field => $setup) {
// An array key might be int.
$field = (string) $field;
$rules = $setup['rules'];
if (is_string($rules)) {
$rules = $this->splitRules($rules);
}
if (str_contains($field, '*')) {
$flattenedArray = array_flatten_with_dots($data);
$values = array_filter(
$flattenedArray,
static fn ($key) => preg_match(self::getRegex($field), $key),
ARRAY_FILTER_USE_KEY
);
// if keys not found
$values = $values ?: [$field => null];
} else {
$values = dot_array_search($field, $data);
}
if ($values === []) {
// We'll process the values right away if an empty array
$this->processRules($field, $setup['label'] ?? $field, $values, $rules, $data, $field);
continue;
}
if (str_contains($field, '*')) {
// Process multiple fields
foreach ($values as $dotField => $value) {
$this->processRules($dotField, $setup['label'] ?? $field, $value, $rules, $data, $field);
}
} else {
// Process single field
$this->processRules($field, $setup['label'] ?? $field, $values, $rules, $data, $field);
}
}
if ($this->getErrors() === []) {
// Store data that was actually validated.
$this->validated = DotArrayFilter::run(
array_keys($this->rules),
$this->data
);
return true;
}
return false;
}
/**
* Returns regex pattern for key with dot array syntax.
*/
private static function getRegex(string $field): string
{
return '/\A'
. str_replace(
['\.\*', '\*\.'],
['\.[^.]+', '[^.]+\.'],
preg_quote($field, '/')
)
. '\z/';
}
/**
* Runs the validation process, returning true or false determining whether
* validation was successful or not.
*
* @param array|bool|float|int|object|string|null $value The data to validate.
* @param array|string $rules The validation rules.
* @param list<string> $errors The custom error message.
* @param string|null $dbGroup The database group to use.
*/
public function check($value, $rules, array $errors = [], $dbGroup = null): bool
{
$this->reset();
return $this->setRule(
'check',
null,
$rules,
$errors
)->run(
['check' => $value],
null,
$dbGroup
);
}
/**
* Returns the actual validated data.
*/
public function getValidated(): array
{
return $this->validated;
}
/**
* Runs all of $rules against $field, until one fails, or
* all of them have been processed. If one fails, it adds
* the error to $this->errors and moves on to the next,
* so that we can collect all of the first errors.
*
* @param array|string $value
* @param array $rules
* @param array $data The array of data to validate, with `DBGroup`.
* @param string|null $originalField The original asterisk field name like "foo.*.bar".
*/
protected function processRules(
string $field,
?string $label,
$value,
$rules = null, // @TODO remove `= null`
?array $data = null, // @TODO remove `= null`
?string $originalField = null
): bool {
if ($data === null) {
throw new InvalidArgumentException('You must supply the parameter: data.');
}
$rules = $this->processIfExist($field, $rules, $data);
if ($rules === true) {
return true;
}
$rules = $this->processPermitEmpty($value, $rules, $data);
if ($rules === true) {
return true;
}
foreach ($rules as $i => $rule) {
$isCallable = is_callable($rule);
$stringCallable = $isCallable && is_string($rule);
$arrayCallable = $isCallable && is_array($rule);
$passed = false;
/** @var string|null $param */
$param = null;
if (! $isCallable && preg_match('/(.*?)\[(.*)\]/', $rule, $match)) {
$rule = $match[1];
$param = $match[2];
}
// Placeholder for custom errors from the rules.
$error = null;
// If it's a callable, call and get out of here.
if ($this->isClosure($rule)) {
$passed = $rule($value, $data, $error, $field);
} elseif ($isCallable) {
$passed = $stringCallable ? $rule($value) : $rule($value, $data, $error, $field);
} else {
$found = false;
// Check in our rulesets
foreach ($this->ruleSetInstances as $set) {
if (! method_exists($set, $rule)) {
continue;
}
$found = true;
if ($rule === 'field_exists') {
$passed = $set->{$rule}($value, $param, $data, $error, $originalField);
} else {
$passed = ($param === null)
? $set->{$rule}($value, $error)
: $set->{$rule}($value, $param, $data, $error, $field);
}
break;
}
// If the rule wasn't found anywhere, we
// should throw an exception so the developer can find it.
if (! $found) {
throw ValidationException::forRuleNotFound($rule);
}
}
// Set the error message if we didn't survive.
if ($passed === false) {
// if the $value is an array, convert it to as string representation
if (is_array($value)) {
$value = $this->isStringList($value)
? '[' . implode(', ', $value) . ']'
: json_encode($value);
} elseif (is_object($value)) {
$value = json_encode($value);
}
$fieldForErrors = ($rule === 'field_exists') ? $originalField : $field;
// @phpstan-ignore-next-line $error may be set by rule methods.
$this->errors[$fieldForErrors] = $error ?? $this->getErrorMessage(
($this->isClosure($rule) || $arrayCallable) ? (string) $i : $rule,
$field,
$label,
$param,
(string) $value,
$originalField
);
return false;
}
}
return true;
}
/**
* @param array $data The array of data to validate, with `DBGroup`.
*
* @return array|true The modified rules or true if we return early
*/
private function processIfExist(string $field, array $rules, array $data)
{
if (in_array('if_exist', $rules, true)) {
$flattenedData = array_flatten_with_dots($data);
$ifExistField = $field;
if (str_contains($field, '.*')) {
// We'll change the dot notation into a PCRE pattern that can be used later
$ifExistField = str_replace('\.\*', '\.(?:[^\.]+)', preg_quote($field, '/'));
$dataIsExisting = false;
$pattern = sprintf('/%s/u', $ifExistField);
foreach (array_keys($flattenedData) as $item) {
if (preg_match($pattern, $item) === 1) {
$dataIsExisting = true;
break;
}
}
} elseif (str_contains($field, '.')) {
$dataIsExisting = array_key_exists($ifExistField, $flattenedData);
} else {
$dataIsExisting = array_key_exists($ifExistField, $data);
}
if (! $dataIsExisting) {
// we return early if `if_exist` is not satisfied. we have nothing to do here.
return true;
}
// Otherwise remove the if_exist rule and continue the process
$rules = array_filter($rules, static fn ($rule) => $rule instanceof Closure || $rule !== 'if_exist');
}
return $rules;
}
/**
* @param array|string $value
* @param array $data The array of data to validate, with `DBGroup`.
*
* @return array|true The modified rules or true if we return early
*/
private function processPermitEmpty($value, array $rules, array $data)
{
if (in_array('permit_empty', $rules, true)) {
if (
! in_array('required', $rules, true)
&& (is_array($value) ? $value === [] : trim((string) $value) === '')
) {
$passed = true;
foreach ($rules as $rule) {
if (! $this->isClosure($rule) && preg_match('/(.*?)\[(.*)\]/', $rule, $match)) {
$rule = $match[1];
$param = $match[2];
if (! in_array($rule, ['required_with', 'required_without'], true)) {
continue;
}
// Check in our rulesets
foreach ($this->ruleSetInstances as $set) {
if (! method_exists($set, $rule)) {
continue;
}
$passed = $passed && $set->{$rule}($value, $param, $data);
break;
}
}
}
if ($passed) {
return true;
}
}
$rules = array_filter($rules, static fn ($rule) => $rule instanceof Closure || $rule !== 'permit_empty');
}
return $rules;
}
/**
* @param Closure(bool|float|int|list<mixed>|object|string|null, bool|float|int|list<mixed>|object|string|null, string|null, string|null): (bool|string) $rule
*/
private function isClosure($rule): bool
{
return $rule instanceof Closure;
}
/**
* Is the array a string list `list<string>`?
*/
private function isStringList(array $array): bool
{
$expectedKey = 0;
foreach ($array as $key => $val) {
// Note: also covers PHP array key conversion, e.g. '5' and 5.1 both become 5
if (! is_int($key)) {
return false;
}
if ($key !== $expectedKey) {
return false;
}
$expectedKey++;
if (! is_string($val)) {
return false;
}
}
return true;
}
/**
* Takes a Request object and grabs the input data to use from its
* array values.
*/
public function withRequest(RequestInterface $request): ValidationInterface
{
/** @var IncomingRequest $request */
if (str_contains($request->getHeaderLine('Content-Type'), 'application/json')) {
$this->data = $request->getJSON(true);
if (! is_array($this->data)) {
throw HTTPException::forUnsupportedJSONFormat();
}
return $this;
}
if (in_array($request->getMethod(), [Method::PUT, Method::PATCH, Method::DELETE], true)
&& ! str_contains($request->getHeaderLine('Content-Type'), 'multipart/form-data')
) {
$this->data = $request->getRawInput();
} else {
$this->data = $request->getVar() ?? [];
}
return $this;
}
/**
* Sets (or adds) an individual rule and custom error messages for a single
* field.
*
* The custom error message should be just the messages that apply to
* this field, like so:
* [
* 'rule1' => 'message1',
* 'rule2' => 'message2',
* ]
*
* @param array|string $rules The validation rules.
* @param array $errors The custom error message.
*
* @return $this
*
* @throws TypeError
*/
public function setRule(string $field, ?string $label, $rules, array $errors = [])
{
if (! is_array($rules) && ! is_string($rules)) {
throw new TypeError('$rules must be of type string|array');
}
$ruleSet = [
$field => [
'label' => $label,
'rules' => $rules,
],
];
if ($errors !== []) {
$ruleSet[$field]['errors'] = $errors;
}
$this->setRules(array_merge($this->getRules(), $ruleSet), $this->customErrors);
return $this;
}
/**
* Stores the rules that should be used to validate the items.
*
* Rules should be an array formatted like:
* [
* 'field' => 'rule1|rule2'
* ]
*
* The $errors array should be formatted like:
* [
* 'field' => [
* 'rule1' => 'message1',
* 'rule2' => 'message2',
* ],
* ]
*
* @param array $errors An array of custom error messages
*/
public function setRules(array $rules, array $errors = []): ValidationInterface
{
$this->customErrors = $errors;
foreach ($rules as $field => &$rule) {
if (is_array($rule)) {
if (array_key_exists('errors', $rule)) {
$this->customErrors[$field] = $rule['errors'];
unset($rule['errors']);
}
// if $rule is already a rule collection, just move it to "rules"
// transforming [foo => [required, foobar]] to [foo => [rules => [required, foobar]]]
if (! array_key_exists('rules', $rule)) {
$rule = ['rules' => $rule];
}
}
if (isset($rule['rules']) && is_string($rule['rules'])) {
$rule['rules'] = $this->splitRules($rule['rules']);
}
if (is_string($rule)) {
$rule = ['rules' => $this->splitRules($rule)];
}
}
$this->rules = $rules;
return $this;
}
/**
* Returns all of the rules currently defined.
*/
public function getRules(): array
{
return $this->rules;
}
/**
* Checks to see if the rule for key $field has been set or not.
*/
public function hasRule(string $field): bool
{
return array_key_exists($field, $this->rules);
}
/**
* Get rule group.
*
* @param string $group Group.
*
* @return list<string> Rule group.
*
* @throws ValidationException If group not found.
*/
public function getRuleGroup(string $group): array
{
if (! isset($this->config->{$group})) {
throw ValidationException::forGroupNotFound($group);
}
if (! is_array($this->config->{$group})) {
throw ValidationException::forGroupNotArray($group);
}
return $this->config->{$group};
}
/**
* Set rule group.
*
* @param string $group Group.
*
* @return void
*
* @throws ValidationException If group not found.
*/
public function setRuleGroup(string $group)
{
$rules = $this->getRuleGroup($group);
$this->setRules($rules);
$errorName = $group . '_errors';
if (isset($this->config->{$errorName})) {
$this->customErrors = $this->config->{$errorName};
}
}
/**
* Returns the rendered HTML of the errors as defined in $template.
*
* You can also use validation_list_errors() in Form helper.
*/
public function listErrors(string $template = 'list'): string
{
if (! array_key_exists($template, $this->config->templates)) {
throw ValidationException::forInvalidTemplate($template);
}
return $this->view
->setVar('errors', $this->getErrors())
->render($this->config->templates[$template]);
}
/**
* Displays a single error in formatted HTML as defined in the $template view.
*
* You can also use validation_show_error() in Form helper.
*/
public function showError(string $field, string $template = 'single'): string
{
if (! array_key_exists($field, $this->getErrors())) {
return '';
}
if (! array_key_exists($template, $this->config->templates)) {
throw ValidationException::forInvalidTemplate($template);
}
return $this->view
->setVar('error', $this->getError($field))
->render($this->config->templates[$template]);
}
/**
* Loads all of the rulesets classes that have been defined in the
* Config\Validation and stores them locally so we can use them.
*
* @return void
*/
protected function loadRuleSets()
{
if ($this->ruleSetFiles === [] || $this->ruleSetFiles === null) {
throw ValidationException::forNoRuleSets();
}
foreach ($this->ruleSetFiles as $file) {
$this->ruleSetInstances[] = new $file();
}
}
/**
* Loads custom rule groups (if set) into the current rules.
*
* Rules can be pre-defined in Config\Validation and can
* be any name, but must all still be an array of the
* same format used with setRules(). Additionally, check
* for {group}_errors for an array of custom error messages.
*
* @param non-empty-string|null $group
*
* @return array<int, array> [rules, customErrors]
*
* @throws ValidationException
*/
public function loadRuleGroup(?string $group = null)
{
if ($group === null || $group === '') {
return [];
}
if (! isset($this->config->{$group})) {
throw ValidationException::forGroupNotFound($group);
}
if (! is_array($this->config->{$group})) {
throw ValidationException::forGroupNotArray($group);
}
$this->setRules($this->config->{$group});
// If {group}_errors exists in the config file,
// then override our custom errors with them.
$errorName = $group . '_errors';
if (isset($this->config->{$errorName})) {
$this->customErrors = $this->config->{$errorName};
}
return [$this->rules, $this->customErrors];
}
/**
* Replace any placeholders within the rules with the values that
* match the 'key' of any properties being set. For example, if
* we had the following $data array:
*
* [ 'id' => 13 ]
*
* and the following rule:
*
* 'is_unique[users,email,id,{id}]'
*
* The value of {id} would be replaced with the actual id in the form data:
*
* 'is_unique[users,email,id,13]'
*/
protected function fillPlaceholders(array $rules, array $data): array
{
foreach ($rules as &$rule) {
$ruleSet = $rule['rules'];
foreach ($ruleSet as &$row) {
if (is_string($row)) {
$placeholderFields = $this->retrievePlaceholders($row, $data);
foreach ($placeholderFields as $field) {
$validator ??= service('validation', null, false);
assert($validator instanceof Validation);
$placeholderRules = $rules[$field]['rules'] ?? null;
// Check if the validation rule for the placeholder exists
if ($placeholderRules === null) {
throw new LogicException(
'No validation rules for the placeholder: "' . $field
. '". You must set the validation rules for the field.'
. ' See <https://codeigniter4.github.io/userguide/libraries/validation.html#validation-placeholders>.'
);
}
// Check if the rule does not have placeholders
foreach ($placeholderRules as $placeholderRule) {
if ($this->retrievePlaceholders($placeholderRule, $data) !== []) {
throw new LogicException(
'The placeholder field cannot use placeholder: ' . $field
);
}
}
// Validate the placeholder field
$dbGroup = $data['DBGroup'] ?? null;
if (! $validator->check($data[$field], $placeholderRules, [], $dbGroup)) {
// if fails, do nothing
continue;
}
// Replace the placeholder in the rule
$ruleSet = str_replace('{' . $field . '}', (string) $data[$field], $ruleSet);
}
}
}
$rule['rules'] = $ruleSet;
}
return $rules;
}
/**
* Retrieves valid placeholder fields.
*/
private function retrievePlaceholders(string $rule, array $data): array
{
preg_match_all('/{(.+?)}/', $rule, $matches);
return array_intersect($matches[1], array_keys($data));
}
/**
* Checks to see if an error exists for the given field.
*/
public function hasError(string $field): bool
{
return (bool) preg_grep(self::getRegex($field), array_keys($this->getErrors()));
}
/**
* Returns the error(s) for a specified $field (or empty string if not
* set).
*/
public function getError(?string $field = null): string
{
if ($field === null && count($this->rules) === 1) {
$field = array_key_first($this->rules);
}
$errors = array_filter(
$this->getErrors(),
static fn ($key) => preg_match(self::getRegex($field), $key),
ARRAY_FILTER_USE_KEY
);
return $errors === [] ? '' : implode("\n", $errors);
}
/**
* Returns the array of errors that were encountered during
* a run() call. The array should be in the following format:
*
* [
* 'field1' => 'error message',
* 'field2' => 'error message',
* ]
*
* @return array<string, string>
*
* @codeCoverageIgnore
*/
public function getErrors(): array
{
return $this->errors;
}
/**
* Sets the error for a specific field. Used by custom validation methods.
*/
public function setError(string $field, string $error): ValidationInterface
{
$this->errors[$field] = $error;
return $this;
}
/**
* Attempts to find the appropriate error message
*
* @param non-empty-string|null $label
* @param string|null $value The value that caused the validation to fail.
*/
protected function getErrorMessage(
string $rule,
string $field,
?string $label = null,
?string $param = null,
?string $value = null,
?string $originalField = null
): string {
$param ??= '';
$args = [
'field' => ($label === null || $label === '') ? $field : lang($label),
'param' => (! isset($this->rules[$param]['label'])) ? $param : lang($this->rules[$param]['label']),
'value' => $value ?? '',
];
// Check if custom message has been defined by user
if (isset($this->customErrors[$field][$rule])) {
return lang($this->customErrors[$field][$rule], $args);
}
if (null !== $originalField && isset($this->customErrors[$originalField][$rule])) {
return lang($this->customErrors[$originalField][$rule], $args);
}
// Try to grab a localized version of the message...
// lang() will return the rule name back if not found,
// so there will always be a string being returned.
return lang('Validation.' . $rule, $args);
}
/**
* Split rules string by pipe operator.
*/
protected function splitRules(string $rules): array
{
if (! str_contains($rules, '|')) {
return [$rules];
}
$string = $rules;
$rules = [];
$length = strlen($string);
$cursor = 0;
while ($cursor < $length) {
$pos = strpos($string, '|', $cursor);
if ($pos === false) {
// we're in the last rule
$pos = $length;
}
$rule = substr($string, $cursor, $pos - $cursor);
while (
(substr_count($rule, '[') - substr_count($rule, '\['))
!== (substr_count($rule, ']') - substr_count($rule, '\]'))
) {
// the pipe is inside the brackets causing the closing bracket to
// not be included. so, we adjust the rule to include that portion.
$pos = strpos($string, '|', $cursor + strlen($rule) + 1) ?: $length;
$rule = substr($string, $cursor, $pos - $cursor);
}
$rules[] = $rule;
$cursor += strlen($rule) + 1; // +1 to exclude the pipe
}
return array_unique($rules);
}
/**
* Resets the class to a blank slate. Should be called whenever
* you need to process more than one array.
*/
public function reset(): ValidationInterface
{
$this->data = [];
$this->validated = [];
$this->rules = [];
$this->errors = [];
$this->customErrors = [];
return $this;
}
}