1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library\Uploaders\Validation; |
4
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade; |
6
|
|
|
use Closure; |
7
|
|
|
use Illuminate\Contracts\Validation\DataAwareRule; |
8
|
|
|
use Illuminate\Contracts\Validation\ValidationRule; |
|
|
|
|
9
|
|
|
use Illuminate\Contracts\Validation\ValidatorAwareRule; |
10
|
|
|
use Illuminate\Database\Eloquent\Model; |
11
|
|
|
use Illuminate\Support\Facades\Validator; |
12
|
|
|
use Illuminate\Validation\Rules\File; |
13
|
|
|
|
14
|
|
|
class ValidArray implements ValidationRule, DataAwareRule, ValidatorAwareRule |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var \Illuminate\Contracts\Validation\Validator |
18
|
|
|
*/ |
19
|
|
|
protected $validator; |
20
|
|
|
|
21
|
|
|
protected array $data; |
22
|
|
|
|
23
|
|
|
public array $arrayRules = []; |
24
|
|
|
|
25
|
|
|
public array $itemRules = []; |
26
|
|
|
|
27
|
|
|
public ?Model $entry; |
28
|
|
|
|
29
|
|
|
public static function make(): self |
30
|
|
|
{ |
31
|
|
|
$instance = new static(); |
32
|
|
|
$entry = CrudPanelFacade::getCurrentEntry(); |
|
|
|
|
33
|
|
|
$instance->entry = $entry !== false ? $entry : null; |
34
|
|
|
|
35
|
|
|
return $instance; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Run the validation rule. |
40
|
|
|
* |
41
|
|
|
* @param string $attribute |
42
|
|
|
* @param mixed $value |
43
|
|
|
* @param Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail |
44
|
|
|
* @return void |
45
|
|
|
*/ |
46
|
|
|
public function validate(string $attribute, mixed $value, Closure $fail): void |
47
|
|
|
{ |
48
|
|
|
if (! is_array($value)) { |
49
|
|
|
try { |
50
|
|
|
$value = json_decode($value, true); |
51
|
|
|
} catch (\Exception $e) { |
52
|
|
|
$fail('Unable to determine the value type.'); |
53
|
|
|
|
54
|
|
|
return; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
$this->validateArrayData($attribute, $value, $fail); |
|
|
|
|
58
|
|
|
$this->validateItemsAsArray($attribute, $value, $fail); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Set the performing validator. |
63
|
|
|
* |
64
|
|
|
* @param \Illuminate\Contracts\Validation\Validator $validator |
65
|
|
|
* @return $this |
66
|
|
|
*/ |
67
|
|
|
public function setValidator($validator) |
68
|
|
|
{ |
69
|
|
|
$this->validator = $validator; |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Set the data under validation. |
76
|
|
|
* |
77
|
|
|
* @param array $data |
78
|
|
|
* @return $this |
79
|
|
|
*/ |
80
|
|
|
public function setData($data) |
81
|
|
|
{ |
82
|
|
|
$this->data = $data; |
83
|
|
|
|
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Set the rules that apply to the "array" aka the field, if it's required, min, max etc. |
89
|
|
|
*/ |
90
|
|
|
public function arrayRules(string|array|File $rules): self |
91
|
|
|
{ |
92
|
|
|
if (is_string($rules)) { |
|
|
|
|
93
|
|
|
$rules = explode('|', $rules); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if (! in_array('array', $rules)) { |
97
|
|
|
$rules[] = 'array'; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$this->arrayRules = $rules; |
101
|
|
|
|
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Set the rules that apply to the items beeing sent in array. |
107
|
|
|
*/ |
108
|
|
|
public function itemRules(string|array|File $rules): self |
109
|
|
|
{ |
110
|
|
|
if (is_string($rules)) { |
|
|
|
|
111
|
|
|
$rules = explode('|', $rules); |
112
|
|
|
} |
113
|
|
|
if (! is_array($rules)) { |
|
|
|
|
114
|
|
|
$rules = [$rules]; |
115
|
|
|
} |
116
|
|
|
$this->itemRules = $rules; |
117
|
|
|
|
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Performs the validation on the array of items, item by item, using the item validation array. |
123
|
|
|
* |
124
|
|
|
* @param string $attribute |
125
|
|
|
* @param array $files |
126
|
|
|
* @param Closure $fail |
127
|
|
|
* @return void |
128
|
|
|
*/ |
129
|
|
|
protected function validateItems($attribute, $items, $fail) |
130
|
|
|
{ |
131
|
|
|
foreach ($items as $item) { |
132
|
|
|
$validator = Validator::make([$attribute => $item], [ |
133
|
|
|
$attribute => $this->itemRules, |
134
|
|
|
], $this->validator->customMessages, $this->validator->customAttributes); |
|
|
|
|
135
|
|
|
|
136
|
|
|
if ($validator->fails()) { |
137
|
|
|
foreach ($validator->errors()->messages()[$attribute] as $message) { |
138
|
|
|
$fail($message)->translate(); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Performs the validation on the array of items, using the item validation array. |
146
|
|
|
* |
147
|
|
|
* @param string $attribute |
148
|
|
|
* @param array $files |
149
|
|
|
* @param Closure $fail |
150
|
|
|
* @return void |
151
|
|
|
*/ |
152
|
|
|
protected function validateItemsAsArray($attribute, $items, $fail) |
153
|
|
|
{ |
154
|
|
|
$validator = Validator::make([$attribute => $items], [ |
155
|
|
|
$attribute.'.*' => $this->itemRules, |
156
|
|
|
], $this->validator->customMessages, $this->validator->customAttributes); |
|
|
|
|
157
|
|
|
|
158
|
|
|
if ($validator->fails()) { |
159
|
|
|
foreach ($validator->errors()->messages()[$attribute] as $message) { |
160
|
|
|
$fail($message)->translate(); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Validate the given data or the array of data from the validator againts the array rules. |
167
|
|
|
* |
168
|
|
|
* @param string $attribute |
169
|
|
|
* @param Closure $fail |
170
|
|
|
* @param null|array $data |
171
|
|
|
* @param null|array $rules |
172
|
|
|
* @return void |
173
|
|
|
*/ |
174
|
|
|
protected function validateArrayData($attribute, $fail, $data = null, $rules = null) |
175
|
|
|
{ |
176
|
|
|
$data = $data ?? $this->data; |
177
|
|
|
$rules = $rules ?? $this->arrayRules; |
178
|
|
|
|
179
|
|
|
$validator = Validator::make($data, [ |
180
|
|
|
$attribute => $rules, |
181
|
|
|
], $this->validator->customMessages, $this->validator->customAttributes); |
|
|
|
|
182
|
|
|
|
183
|
|
|
if ($validator->fails()) { |
184
|
|
|
foreach ($validator->errors()->messages()[$attribute] as $message) { |
185
|
|
|
$fail($message)->translate(); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
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