1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library\Validation\Rules; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Illuminate\Contracts\Validation\DataAwareRule; |
7
|
|
|
use Illuminate\Contracts\Validation\Rule; |
8
|
|
|
use Illuminate\Contracts\Validation\ValidationRule; |
9
|
|
|
use Illuminate\Contracts\Validation\ValidatorAwareRule; |
10
|
|
|
use Illuminate\Support\Facades\Validator; |
11
|
|
|
use Illuminate\Support\Str; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @method static static itemRules() |
15
|
|
|
*/ |
16
|
|
|
abstract class BackpackCustomRule implements ValidationRule, DataAwareRule, ValidatorAwareRule |
17
|
|
|
{ |
18
|
|
|
use \Backpack\CRUD\app\Library\Validation\Rules\Support\HasFiles; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var \Illuminate\Contracts\Validation\Validator |
22
|
|
|
*/ |
23
|
|
|
protected $validator; |
24
|
|
|
|
25
|
|
|
protected array $data; |
26
|
|
|
|
27
|
|
|
public array $fieldRules = []; |
28
|
|
|
|
29
|
|
|
public bool $implicit = true; |
30
|
|
|
|
31
|
|
|
public static function field(string|array|ValidationRule|Rule $rules = []): self |
32
|
|
|
{ |
33
|
|
|
$instance = new static(); |
34
|
|
|
$instance->fieldRules = self::getRulesAsArray($rules); |
35
|
|
|
|
36
|
|
|
return $instance; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Run the validation rule. |
41
|
|
|
* |
42
|
|
|
* @param string $attribute |
43
|
|
|
* @param mixed $value |
44
|
|
|
* @param Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail |
45
|
|
|
* @return void |
46
|
|
|
*/ |
47
|
|
|
public function validate(string $attribute, mixed $value, Closure $fail): void |
48
|
|
|
{ |
49
|
|
|
$value = $this->ensureValueIsValid($value); |
50
|
|
|
|
51
|
|
|
if ($value === false) { |
52
|
|
|
$fail('Invalid value for the attribute.')->translate(); |
53
|
|
|
|
54
|
|
|
return; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$errors = $this->validateOnSubmit($attribute, $value); |
58
|
|
|
foreach ($errors as $error) { |
59
|
|
|
$fail($error)->translate(); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Set the performing validator. |
65
|
|
|
* |
66
|
|
|
* @param \Illuminate\Contracts\Validation\Validator $validator |
67
|
|
|
* @return $this |
68
|
|
|
*/ |
69
|
|
|
public function setValidator($validator) |
70
|
|
|
{ |
71
|
|
|
$this->validator = $validator; |
72
|
|
|
|
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Set the data under validation. |
78
|
|
|
* |
79
|
|
|
* @param array $data |
80
|
|
|
* @return $this |
81
|
|
|
*/ |
82
|
|
|
public function setData($data) |
83
|
|
|
{ |
84
|
|
|
$this->data = $data; |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function getFieldRules(): array |
90
|
|
|
{ |
91
|
|
|
return tap($this->fieldRules, function ($rule) { |
92
|
|
|
if (is_a($rule, BackpackCustomRule::class, true)) { |
93
|
|
|
$rule = $rule->getFieldRules(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $rule; |
97
|
|
|
}); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
protected static function getRulesAsArray($rules) |
101
|
|
|
{ |
102
|
|
|
if (is_string($rules)) { |
103
|
|
|
$rules = explode('|', $rules); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (! is_array($rules)) { |
107
|
|
|
$rules = [$rules]; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $rules; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// from our POV, value is always valid, each validator may have their own |
114
|
|
|
// validation needs. This is the first step in the validation process. |
115
|
|
|
protected function ensureValueIsValid($value) |
116
|
|
|
{ |
117
|
|
|
return $value; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
private function validateAndGetErrors(string $attribute, mixed $value, array $rules): array |
121
|
|
|
{ |
122
|
|
|
$validator = Validator::make($value, [ |
123
|
|
|
$attribute => $rules, |
124
|
|
|
], $this->validator->customMessages, $this->validator->customAttributes); |
|
|
|
|
125
|
|
|
|
126
|
|
|
return $validator->errors()->messages()[$attribute] ?? []; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
protected function getValidationAttributeString(string $attribute) |
130
|
|
|
{ |
131
|
|
|
return Str::substrCount($attribute, '.') > 1 ? |
132
|
|
|
Str::before($attribute, '.').'.*.'.Str::afterLast($attribute, '.') : |
133
|
|
|
$attribute; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
protected function validateOnSubmit(string $attribute, mixed $value): array |
137
|
|
|
{ |
138
|
|
|
return $this->validateRules($attribute, $value); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
protected function validateFieldAndFile(string $attribute, mixed $value = null, array|null $data = null, array|null $customRules = null): array |
142
|
|
|
{ |
143
|
|
|
$fieldErrors = $this->validateFieldRules($attribute, $value, $data, $customRules); |
|
|
|
|
144
|
|
|
$fileErrors = $this->validateFileRules($attribute, $value); |
145
|
|
|
|
146
|
|
|
return array_merge($fieldErrors, $fileErrors); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Implementation. |
151
|
|
|
*/ |
152
|
|
|
public function validateFieldRules(string $attribute, mixed $data = null, array|null $customRules = null): array |
153
|
|
|
{ |
154
|
|
|
$data = $data ?? $this->data; |
155
|
|
|
$validationRuleAttribute = $this->getValidationAttributeString($attribute); |
156
|
|
|
$data = $this->prepareValidatorData($data, $attribute); |
157
|
|
|
|
158
|
|
|
return $this->validateAndGetErrors($validationRuleAttribute, $data, $customRules ?? $this->getFieldRules()); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
protected function prepareValidatorData(array $data, string $attribute): array |
|
|
|
|
162
|
|
|
{ |
163
|
|
|
return $data; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
protected function validateFileRules(string $attribute, mixed $value): array |
167
|
|
|
{ |
168
|
|
|
return $this->validateAndGetErrors($attribute, $value, $this->getFileRules()); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function validateRules(string $attribute, mixed $value): array |
172
|
|
|
{ |
173
|
|
|
return $this->validateFieldAndFile($attribute, $value); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|