1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library\Uploaders\Validation; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Illuminate\Contracts\Validation\DataAwareRule; |
7
|
|
|
use Illuminate\Contracts\Validation\ValidationRule; |
8
|
|
|
use Illuminate\Contracts\Validation\ValidatorAwareRule; |
9
|
|
|
use Illuminate\Validation\Rules\File; |
10
|
|
|
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade; |
11
|
|
|
use Illuminate\Database\Eloquent\Model; |
12
|
|
|
use Illuminate\Support\Facades\Validator; |
13
|
|
|
use Closure; |
|
|
|
|
14
|
|
|
|
15
|
|
|
class ValidBackpackUpload implements ValidationRule, DataAwareRule, ValidatorAwareRule |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var \Illuminate\Contracts\Validation\Validator |
19
|
|
|
*/ |
20
|
|
|
protected $validator; |
21
|
|
|
|
22
|
|
|
protected array $data; |
23
|
|
|
|
24
|
|
|
public array $arrayRules = []; |
25
|
|
|
|
26
|
|
|
public array $fileRules = []; |
27
|
|
|
|
28
|
|
|
public ?Model $entry; |
29
|
|
|
|
30
|
|
|
public static function make(): self |
31
|
|
|
{ |
32
|
|
|
$instance = new static(); |
33
|
|
|
$entry = CrudPanelFacade::getCurrentEntry(); |
34
|
|
|
$instance->entry = $entry !== false ? $entry : null; |
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
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Set the performing validator. |
52
|
|
|
* |
53
|
|
|
* @param \Illuminate\Contracts\Validation\Validator $validator |
54
|
|
|
* @return $this |
55
|
|
|
*/ |
56
|
|
|
public function setValidator($validator) |
57
|
|
|
{ |
58
|
|
|
$this->validator = $validator; |
59
|
|
|
|
60
|
|
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Set the data under validation. |
65
|
|
|
* |
66
|
|
|
* @param array $data |
67
|
|
|
* @return $this |
68
|
|
|
*/ |
69
|
|
|
public function setData($data) |
70
|
|
|
{ |
71
|
|
|
$this->data = $data; |
72
|
|
|
|
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Set the rules that apply to the "array" aka the field, if it's required, min, max etc. |
78
|
|
|
*/ |
79
|
|
|
public function arrayRules(string|array|File $rules): self |
80
|
|
|
{ |
81
|
|
|
if (is_string($rules)) { |
82
|
|
|
$rules = explode('|', $rules); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if (! in_array('array', $rules)) { |
86
|
|
|
$rules[] = 'array'; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$this->arrayRules = $rules; |
90
|
|
|
|
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Set the rules that apply to the files beeing uploaded. |
96
|
|
|
*/ |
97
|
|
|
public function fileRules(string|array|File $rules): self |
98
|
|
|
{ |
99
|
|
|
if (is_string($rules)) { |
100
|
|
|
$rules = explode('|', $rules); |
101
|
|
|
} |
102
|
|
|
if (! is_array($rules)) { |
103
|
|
|
$rules = [$rules]; |
104
|
|
|
} |
105
|
|
|
$this->fileRules = $rules; |
106
|
|
|
|
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Performs the validation on the array of files using the file validation array. |
112
|
|
|
* |
113
|
|
|
* @param string $attribute |
114
|
|
|
* @param array $files |
115
|
|
|
* @param Closure $fail |
116
|
|
|
* @return void |
117
|
|
|
*/ |
118
|
|
|
protected function validateFiles($attribute, $files, $fail) |
119
|
|
|
{ |
120
|
|
|
foreach ($files as $file) { |
121
|
|
|
if(!is_file($file)) { |
122
|
|
|
continue; |
123
|
|
|
} |
124
|
|
|
$validator = Validator::make([$attribute => $file], [ |
125
|
|
|
$attribute => $this->fileRules, |
126
|
|
|
], $this->validator->customMessages, $this->validator->customAttributes); |
127
|
|
|
|
128
|
|
|
if ($validator->fails()) { |
129
|
|
|
foreach($validator->errors()->messages()[$attribute] as $message) { |
130
|
|
|
$fail($message); |
131
|
|
|
}; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Validate the given data or the array of data from the validator againts the array rules. |
138
|
|
|
* |
139
|
|
|
* @param string $attribute |
140
|
|
|
* @param Closure $fail |
141
|
|
|
* @param null|array $data |
142
|
|
|
* @return void |
143
|
|
|
*/ |
144
|
|
|
protected function validateArrayData($attribute, $fail, $data = null) |
145
|
|
|
{ |
146
|
|
|
$data = $data ?? $this->data; |
147
|
|
|
|
148
|
|
|
$validator = Validator::make($data, [ |
149
|
|
|
$attribute => $this->arrayRules, |
150
|
|
|
], $this->validator->customMessages, $this->validator->customAttributes); |
151
|
|
|
|
152
|
|
|
if ($validator->fails()) { |
153
|
|
|
foreach($validator->errors()->messages()[$attribute] as $message) { |
154
|
|
|
$fail($message); |
155
|
|
|
}; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|