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
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @method static static itemRules() |
14
|
|
|
*/ |
15
|
|
|
abstract class BackpackCustomRule 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 $fieldRules = []; |
25
|
|
|
|
26
|
|
|
public bool $implicit = true; |
27
|
|
|
|
28
|
|
|
public static function field(string|array|ValidationRule|Rule $rules = []): self |
29
|
|
|
{ |
30
|
|
|
$instance = new static(); |
31
|
|
|
$instance->fieldRules = self::getRulesAsArray($rules); |
32
|
|
|
|
33
|
|
|
return $instance; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Run the validation rule. |
38
|
|
|
* |
39
|
|
|
* @param string $attribute |
40
|
|
|
* @param mixed $value |
41
|
|
|
* @param Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
|
|
public function validate(string $attribute, mixed $value, Closure $fail): void |
45
|
|
|
{ |
46
|
|
|
// is the extending class reponsability the implementation of the validation logic |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Set the performing validator. |
51
|
|
|
* |
52
|
|
|
* @param \Illuminate\Contracts\Validation\Validator $validator |
53
|
|
|
* @return $this |
54
|
|
|
*/ |
55
|
|
|
public function setValidator($validator) |
56
|
|
|
{ |
57
|
|
|
$this->validator = $validator; |
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Set the data under validation. |
64
|
|
|
* |
65
|
|
|
* @param array $data |
66
|
|
|
* @return $this |
67
|
|
|
*/ |
68
|
|
|
public function setData($data) |
69
|
|
|
{ |
70
|
|
|
$this->data = $data; |
71
|
|
|
|
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function getFieldRules(): array |
76
|
|
|
{ |
77
|
|
|
return tap($this->fieldRules, function ($rule) { |
78
|
|
|
if (is_a($rule, BackpackCustomRule::class, true)) { |
79
|
|
|
$rule = $rule->getFieldRules(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $rule; |
83
|
|
|
}); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
protected static function getRulesAsArray($rules) |
87
|
|
|
{ |
88
|
|
|
if (is_string($rules)) { |
89
|
|
|
$rules = explode('|', $rules); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if (! is_array($rules)) { |
93
|
|
|
$rules = [$rules]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $rules; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Implementation. |
101
|
|
|
*/ |
102
|
|
|
public function validateFieldRules(string $attribute, mixed $value, Closure $fail): void |
103
|
|
|
{ |
104
|
|
|
$validator = Validator::make([$attribute => $value], [ |
105
|
|
|
$attribute => $this->getFieldRules(), |
106
|
|
|
], $this->validator->customMessages, $this->validator->customAttributes); |
|
|
|
|
107
|
|
|
|
108
|
|
|
if ($validator->fails()) { |
109
|
|
|
foreach ($validator->errors()->messages()[$attribute] as $message) { |
110
|
|
|
$fail($message)->translate(); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function createValidator(string $attribute, array $rules, mixed $value, Closure $fail): void |
116
|
|
|
{ |
117
|
|
|
$validator = Validator::make([$attribute => $value], [ |
118
|
|
|
$attribute => $rules, |
119
|
|
|
], $this->validator->customMessages, $this->validator->customAttributes); |
|
|
|
|
120
|
|
|
|
121
|
|
|
if ($validator->fails()) { |
122
|
|
|
foreach ($validator->errors()->messages()[$attribute] as $message) { |
123
|
|
|
$fail($message)->translate(); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|