1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Proengsoft\JsValidation\Support; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Illuminate\Validation\Validator as BaseValidator; |
7
|
|
|
|
8
|
|
|
class DelegatedValidator |
9
|
|
|
{ |
10
|
|
|
use AccessProtectedTrait; |
11
|
|
|
/** |
12
|
|
|
* The Validator resolved instance. |
13
|
|
|
* |
14
|
|
|
* @var \Illuminate\Validation\Validator |
15
|
|
|
*/ |
16
|
|
|
protected $validator; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Closure to invoke non accessible Validator methods. |
20
|
|
|
* |
21
|
|
|
* @var Closure |
22
|
|
|
*/ |
23
|
|
|
protected $validatorMethod; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* DelegatedValidator constructor. |
27
|
|
|
* |
28
|
|
|
* @param \Illuminate\Validation\Validator $validator |
29
|
|
|
*/ |
30
|
20 |
|
public function __construct(BaseValidator $validator) |
31
|
|
|
{ |
32
|
20 |
|
$this->validator = $validator; |
33
|
20 |
|
$this->validatorMethod = $this->createProtectedCaller($validator); |
34
|
20 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Call validator method. |
38
|
|
|
* |
39
|
|
|
* @param string $method |
40
|
|
|
* @param array $args |
41
|
|
|
* @return mixed |
42
|
|
|
*/ |
43
|
6 |
|
private function callValidator($method, $args = []) |
44
|
|
|
{ |
45
|
6 |
|
return $this->callProtected($this->validatorMethod, $method, $args); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get current \Illuminate\Validation\Validator instance. |
50
|
|
|
* |
51
|
|
|
* @return \Illuminate\Validation\Validator |
52
|
|
|
*/ |
53
|
1 |
|
public function getValidator() |
54
|
|
|
{ |
55
|
1 |
|
return $this->validator; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get the data under validation. |
60
|
|
|
* |
61
|
|
|
* @return array |
62
|
|
|
*/ |
63
|
1 |
|
public function getData() |
64
|
|
|
{ |
65
|
1 |
|
return $this->validator->getData(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Set the data under validation. |
70
|
|
|
* |
71
|
|
|
* @param array |
72
|
|
|
*/ |
73
|
1 |
|
public function setData($data) |
74
|
|
|
{ |
75
|
1 |
|
$this->validator->setData($data); |
76
|
1 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get the validation rules. |
80
|
|
|
* |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
1 |
|
public function getRules() |
84
|
|
|
{ |
85
|
1 |
|
return $this->validator->getRules(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get the files under validation. |
90
|
|
|
* |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
2 |
|
public function getFiles() |
94
|
|
|
{ |
95
|
2 |
|
if (method_exists($this->validator, 'getFiles')) { |
96
|
1 |
|
return $this->validator->getFiles(); |
97
|
|
|
} |
98
|
|
|
|
99
|
1 |
|
return []; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Set the files under validation. |
104
|
|
|
* |
105
|
|
|
* @param array $files |
106
|
|
|
* |
107
|
|
|
* @return BaseValidator |
108
|
|
|
*/ |
109
|
2 |
|
public function setFiles(array $files) |
110
|
|
|
{ |
111
|
2 |
|
if (method_exists($this->validator, 'setFiles')) { |
112
|
1 |
|
return $this->validator->setFiles($files); |
113
|
|
|
} |
114
|
|
|
|
115
|
1 |
|
return $this->validator; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Determine if a given rule implies the attribute is required. |
120
|
|
|
* |
121
|
|
|
* @param string $rule |
122
|
|
|
* |
123
|
|
|
* @return bool |
124
|
|
|
*/ |
125
|
1 |
|
public function isImplicit($rule) |
126
|
|
|
{ |
127
|
1 |
|
return $this->callValidator('isImplicit', [$rule]); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Replace all error message place-holders with actual values. |
132
|
|
|
* |
133
|
|
|
* @param string $message |
134
|
|
|
* @param string $attribute |
135
|
|
|
* @param string $rule |
136
|
|
|
* @param array $parameters |
137
|
|
|
* |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
1 |
|
public function doReplacements($message, $attribute, $rule, $parameters) |
141
|
|
|
{ |
142
|
1 |
|
return $this->callValidator('doReplacements', [$message, $attribute, $rule, $parameters]); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Determine if the given attribute has a rule in the given set. |
147
|
|
|
* |
148
|
|
|
* @param string $attribute |
149
|
|
|
* @param string|array $rules |
150
|
|
|
* |
151
|
|
|
* @return bool |
152
|
|
|
*/ |
153
|
1 |
|
public function hasRule($attribute, $rules) |
154
|
|
|
{ |
155
|
1 |
|
return $this->callValidator('hasRule', [$attribute, $rules]); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Get the validation message for an attribute and rule. |
160
|
|
|
* |
161
|
|
|
* @param string $attribute |
162
|
|
|
* @param string $rule |
163
|
|
|
* |
164
|
|
|
* @return string |
165
|
|
|
*/ |
166
|
1 |
|
public function getMessage($attribute, $rule) |
167
|
|
|
{ |
168
|
1 |
|
return $this->callValidator('getMessage', [$attribute, $rule]); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Extract the rule name and parameters from a rule. |
173
|
|
|
* |
174
|
|
|
* @param array|string $rules |
175
|
|
|
* |
176
|
|
|
* @return array |
177
|
|
|
*/ |
178
|
1 |
|
public function parseRule($rules) |
179
|
|
|
{ |
180
|
1 |
|
return $this->callValidator('parseRule', [$rules]); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Explode the rules into an array of rules. |
185
|
|
|
* |
186
|
|
|
* @param string|array $rules |
187
|
|
|
* @return array |
188
|
|
|
*/ |
189
|
1 |
|
public function explodeRules($rules) |
190
|
|
|
{ |
191
|
1 |
|
return $this->callValidator('explodeRules', [$rules]); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Add conditions to a given field based on a Closure. |
196
|
|
|
* |
197
|
|
|
* @param string $attribute |
198
|
|
|
* @param string|array $rules |
199
|
|
|
* @param callable $callback |
200
|
|
|
* @return void |
201
|
|
|
*/ |
202
|
1 |
|
public function sometimes($attribute, $rules, callable $callback) |
203
|
|
|
{ |
204
|
1 |
|
$this->validator->sometimes($attribute, $rules, $callback); |
205
|
1 |
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Delegate method calls to validator instance. |
209
|
|
|
* |
210
|
|
|
* @param $method |
211
|
|
|
* @param $params |
212
|
|
|
* |
213
|
|
|
* @return mixed |
214
|
|
|
*/ |
215
|
1 |
|
public function __call($method, $params) |
216
|
|
|
{ |
217
|
1 |
|
$arrCaller = array($this->validator, $method); |
218
|
|
|
|
219
|
1 |
|
return call_user_func_array($arrCaller, $params); |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|