-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
CompositeRule.php
148 lines (130 loc) · 3.6 KB
/
CompositeRule.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/**
* @link https://github.com/illuminatech
* @copyright Copyright (c) 2019 Illuminatech
* @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
*/
namespace Illuminatech\Validation\Composite;
use Illuminate\Contracts\Validation\Factory;
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Validator;
/**
* CompositeRule allows uniting several validation rules into a single one for easy re-usage.
*
* For example:
*
* ```php
* use Illuminatech\Validation\Composite\CompositeRule;
*
* class PasswordRule extends CompositeRule
* {
* protected function rules(): array
* {
* return ['string', 'min:8', 'max:200'];
* }
* }
*
* class AvatarRule extends CompositeRule
* {
* protected function rules(): array
* {
* return ['file', 'mimes:png,jpg,jpeg', 'max:1024'];
* }
* }
*
* // ...
*
* $request->validate([
* 'password' => ['required', new PasswordRule],
* 'avatar' => ['required', new AvatarRule],
* ]);
* ```
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 1.0
*/
abstract class CompositeRule implements Rule
{
/**
* @var \Illuminate\Contracts\Validation\Factory validator factory used for slave validator creation.
*/
private $validatorFactory;
/**
* @var string validation error message from particular underlying validator.
*/
private $message;
/**
* Constructor.
*
* @param \Illuminate\Contracts\Validation\Factory|null $validatorFactory validator factory used for slave validator creation.
*/
public function __construct(?Factory $validatorFactory = null)
{
if ($validatorFactory !== null) {
$this->setValidatorFactory($validatorFactory);
}
}
/**
* Defines list of the validation rules, which are combined into this one.
*
* @return array validation rules definition.
*/
abstract protected function rules(): array;
/**
* Defines custom error messages for the validation rules defined at {@see rules()}.
* @since 1.2.0
*
* @return array error messages.
*/
protected function messages(): array
{
return [];
}
/**
* {@inheritdoc}
*/
public function passes($attribute, $value): bool
{
$data = [];
Arr::set($data, $attribute, $value); // ensure correct validation for array attributes like 'item_ids.*' or 'items.*.id'
$validator = $this->getValidatorFactory()->make(
$data,
[
$attribute => $this->rules(),
],
$this->messages()
);
if ($validator->fails()) {
$this->message = $validator->getMessageBag()->first();
return false;
}
return true;
}
/**
* {@inheritdoc}
*/
public function message()
{
return $this->message;
}
/**
* @return \Illuminate\Contracts\Validation\Factory validator factory used for slave validator creation.
*/
public function getValidatorFactory(): Factory
{
if ($this->validatorFactory === null) {
$this->validatorFactory = Validator::getFacadeRoot();
}
return $this->validatorFactory;
}
/**
* @param \Illuminate\Contracts\Validation\Factory $validatorFactory validator factory used for slave validator creation.
* @return static self reference.
*/
public function setValidatorFactory(Factory $validatorFactory): self
{
$this->validatorFactory = $validatorFactory;
return $this;
}
}