forked from illuminate/validation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InvokableValidationRule.php
189 lines (163 loc) · 4.7 KB
/
InvokableValidationRule.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
namespace Illuminate\Validation;
use Illuminate\Contracts\Validation\DataAwareRule;
use Illuminate\Contracts\Validation\ImplicitRule;
use Illuminate\Contracts\Validation\InvokableRule;
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Contracts\Validation\ValidatorAwareRule;
use Illuminate\Translation\PotentiallyTranslatedString;
class InvokableValidationRule implements Rule, ValidatorAwareRule
{
/**
* The invokable that validates the attribute.
*
* @var \Illuminate\Contracts\Validation\InvokableRule
*/
protected $invokable;
/**
* Indicates if the validation invokable failed.
*
* @var bool
*/
protected $failed = false;
/**
* The validation error messages.
*
* @var array
*/
protected $messages = [];
/**
* The current validator.
*
* @var \Illuminate\Validation\Validator
*/
protected $validator;
/**
* The data under validation.
*
* @var array
*/
protected $data = [];
/**
* Create a new explicit Invokable validation rule.
*
* @param \Illuminate\Contracts\Validation\InvokableRule $invokable
* @return void
*/
protected function __construct(InvokableRule $invokable)
{
$this->invokable = $invokable;
}
/**
* Create a new implicit or explicit Invokable validation rule.
*
* @param \Illuminate\Contracts\Validation\InvokableRule $invokable
* @return \Illuminate\Contracts\Validation\Rule
*/
public static function make($invokable)
{
if ($invokable->implicit ?? false) {
return new class($invokable) extends InvokableValidationRule implements ImplicitRule
{
//
};
}
return new InvokableValidationRule($invokable);
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
$this->failed = false;
if ($this->invokable instanceof DataAwareRule) {
$this->invokable->setData($this->validator->getData());
}
if ($this->invokable instanceof ValidatorAwareRule) {
$this->invokable->setValidator($this->validator);
}
$this->invokable->__invoke($attribute, $value, function ($attribute, $message = null) {
$this->failed = true;
return $this->pendingPotentiallyTranslatedString($attribute, $message);
});
return ! $this->failed;
}
/**
* Get the validation error messages.
*
* @return array
*/
public function message()
{
return $this->messages;
}
/**
* Set the data under validation.
*
* @param array $data
* @return $this
*/
public function setData($data)
{
$this->data = $data;
return $this;
}
/**
* Set the current validator.
*
* @param \Illuminate\Validation\Validator $validator
* @return $this
*/
public function setValidator($validator)
{
$this->validator = $validator;
return $this;
}
/**
* Create a pending potentially translated string.
*
* @param string $attribute
* @param ?string $message
* @return \Illuminate\Translation\PotentiallyTranslatedString
*/
protected function pendingPotentiallyTranslatedString($attribute, $message)
{
$destructor = $message === null
? fn ($message) => $this->messages[] = $message
: fn ($message) => $this->messages[$attribute] = $message;
return new class($message ?? $attribute, $this->validator->getTranslator(), $destructor) extends PotentiallyTranslatedString
{
/**
* The callback to call when the object destructs.
*
* @var \Closure
*/
protected $destructor;
/**
* Create a new pending potentially translated string.
*
* @param string $message
* @param \Illuminate\Contracts\Translation\Translator $translator
* @param \Closure $destructor
*/
public function __construct($message, $translator, $destructor)
{
parent::__construct($message, $translator);
$this->destructor = $destructor;
}
/**
* Handle the object's destruction.
*
* @return void
*/
public function __destruct()
{
($this->destructor)($this->toString());
}
};
}
}