1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library\Validation\Rules; |
4
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Library\Validation\Rules\Support\HasFiles; |
6
|
|
|
use Closure; |
7
|
|
|
use Illuminate\Contracts\Validation\Rule; |
8
|
|
|
use Illuminate\Contracts\Validation\ValidationRule; |
9
|
|
|
use Illuminate\Support\Facades\Validator; |
10
|
|
|
use Illuminate\Support\Str; |
11
|
|
|
|
12
|
|
|
abstract class ValidFileArray extends BackpackCustomRule |
13
|
|
|
{ |
14
|
|
|
use HasFiles; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Run the validation rule. |
18
|
|
|
* |
19
|
|
|
* @param string $attribute |
20
|
|
|
* @param mixed $value |
21
|
|
|
* @param Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail |
22
|
|
|
* @return void |
23
|
|
|
*/ |
24
|
|
|
public function validate(string $attribute, mixed $value, Closure $fail): void |
25
|
|
|
{ |
26
|
|
|
if (! $value = self::ensureValidValue($value)) { |
27
|
|
|
$fail('Unable to determine the value type.'); |
28
|
|
|
|
29
|
|
|
return; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$this->validateArrayData($attribute, $fail, $value); |
33
|
|
|
$this->validateItems($attribute, $value, $fail); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public static function field(string|array|ValidationRule|Rule $rules = []): self |
37
|
|
|
{ |
38
|
|
|
$instance = new static(); |
39
|
|
|
$instance->fieldRules = self::getRulesAsArray($rules); |
40
|
|
|
|
41
|
|
|
if (! in_array('array', $instance->getFieldRules())) { |
42
|
|
|
$instance->fieldRules[] = 'array'; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $instance; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
protected function validateItems(string $attribute, array $items, Closure $fail): void |
49
|
|
|
{ |
50
|
|
|
$cleanAttribute = Str::afterLast($attribute, '.'); |
51
|
|
|
foreach ($items as $file) { |
52
|
|
|
$validator = Validator::make([$cleanAttribute => $file], [ |
53
|
|
|
$cleanAttribute => $this->getFileRules(), |
54
|
|
|
], $this->validator->customMessages, $this->validator->customAttributes); |
|
|
|
|
55
|
|
|
|
56
|
|
|
if ($validator->fails()) { |
57
|
|
|
foreach ($validator->errors()->messages() ?? [] as $attr => $message) { |
58
|
|
|
foreach ($message as $messageText) { |
59
|
|
|
$fail($messageText)->translate(); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function validateArrayData(string $attribute, Closure $fail, null|array $data = null, null|array $rules = null): void |
67
|
|
|
{ |
68
|
|
|
$data = $data ?? $this->data; |
69
|
|
|
$rules = $rules ?? $this->getFieldRules(); |
70
|
|
|
$validationRuleAttribute = $this->getValidationAttributeString($attribute); |
71
|
|
|
$validator = Validator::make($data, [ |
72
|
|
|
$validationRuleAttribute => $rules, |
73
|
|
|
], $this->validator->customMessages, $this->validator->customAttributes); |
|
|
|
|
74
|
|
|
|
75
|
|
|
if ($validator->fails()) { |
76
|
|
|
foreach ($validator->errors()->messages()[$attribute] as $message) { |
77
|
|
|
$fail($message)->translate(); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
protected static function ensureValidValue($value) |
83
|
|
|
{ |
84
|
|
|
if (! is_array($value)) { |
85
|
|
|
try { |
86
|
|
|
$value = json_decode($value, true); |
87
|
|
|
} catch (\Exception $e) { |
88
|
|
|
return false; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $value; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
private function getValidationAttributeString($attribute) |
96
|
|
|
{ |
97
|
|
|
return Str::substrCount($attribute, '.') > 1 ? |
98
|
|
|
Str::before($attribute, '.').'.*.'.Str::afterLast($attribute, '.') : |
99
|
|
|
$attribute; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|