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
|
|
|
/** |
27
|
|
|
* DelegatedValidator constructor. |
28
|
|
|
* @param \Illuminate\Validation\Validator $validator |
29
|
|
|
*/ |
30
|
|
|
public function __construct(BaseValidator $validator ) |
31
|
|
|
{ |
32
|
|
|
|
33
|
|
|
$this->validator = $validator; |
34
|
|
|
$this->validatorMethod = $this->createProtectedCaller($validator); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
private function callValidator($method, $args = []) |
38
|
|
|
{ |
39
|
|
|
return $this->callProtected($this->validatorMethod, $method, $args); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get current \Illuminate\Validation\Validator instance. |
44
|
|
|
* |
45
|
|
|
* @return \Illuminate\Validation\Validator |
46
|
|
|
*/ |
47
|
|
|
public function getValidator() |
48
|
|
|
{ |
49
|
|
|
return $this->validator; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get the data under validation. |
54
|
|
|
* |
55
|
|
|
* @return array |
56
|
|
|
*/ |
57
|
|
|
public function getData() |
58
|
|
|
{ |
59
|
|
|
return $this->validator->getData(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Set the data under validation. |
64
|
|
|
* |
65
|
|
|
* @param array |
66
|
|
|
*/ |
67
|
|
|
public function setData($data) |
68
|
|
|
{ |
69
|
|
|
$this->validator->setData($data); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get the validation rules. |
74
|
|
|
* |
75
|
|
|
* @return array |
76
|
|
|
*/ |
77
|
|
|
public function getRules() |
78
|
|
|
{ |
79
|
|
|
return $this->validator->getRules(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get the files under validation. |
84
|
|
|
* |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
|
|
public function getFiles() |
88
|
|
|
{ |
89
|
|
|
return $this->validator->getFiles(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Set the files under validation. |
94
|
|
|
* |
95
|
|
|
* @param array $files |
96
|
|
|
* |
97
|
|
|
* @return BaseValidator |
98
|
|
|
*/ |
99
|
|
|
public function setFiles(array $files) |
100
|
|
|
{ |
101
|
|
|
return $this->validator->setFiles($files); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Determine if a given rule implies the attribute is required. |
107
|
|
|
* |
108
|
|
|
* @param string $rule |
109
|
|
|
* |
110
|
|
|
* @return bool |
111
|
|
|
*/ |
112
|
|
|
public function isImplicit($rule) |
113
|
|
|
{ |
114
|
|
|
return $this->callValidator('isImplicit', [$rule]); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Replace all error message place-holders with actual values. |
119
|
|
|
* |
120
|
|
|
* @param string $message |
121
|
|
|
* @param string $attribute |
122
|
|
|
* @param string $rule |
123
|
|
|
* @param array $parameters |
124
|
|
|
* |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
public function doReplacements($message, $attribute, $rule, $parameters) |
128
|
|
|
{ |
129
|
|
|
return $this->callValidator('doReplacements', [$message, $attribute, $rule, $parameters]); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Determine if the given attribute has a rule in the given set. |
134
|
|
|
* |
135
|
|
|
* @param string $attribute |
136
|
|
|
* @param string|array $rules |
137
|
|
|
* |
138
|
|
|
* @return bool |
139
|
|
|
*/ |
140
|
|
|
public function hasRule($attribute, $rules) |
141
|
|
|
{ |
142
|
|
|
return $this->callValidator('hasRule', [$attribute, $rules]); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Get the validation message for an attribute and rule. |
147
|
|
|
* |
148
|
|
|
* @param string $attribute |
149
|
|
|
* @param string $rule |
150
|
|
|
* |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
|
|
public function getMessage($attribute, $rule) |
154
|
|
|
{ |
155
|
|
|
return $this->callValidator('getMessage', [$attribute, $rule]); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Extract the rule name and parameters from a rule. |
160
|
|
|
* |
161
|
|
|
* @param array|string $rules |
162
|
|
|
* |
163
|
|
|
* @return array |
164
|
|
|
*/ |
165
|
|
|
public function parseRule($rules) |
166
|
|
|
{ |
167
|
|
|
return $this->callValidator('parseRule', [$rules]); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Delegate method calls to validator instance. |
173
|
|
|
* |
174
|
|
|
* @param $method |
175
|
|
|
* @param $params |
176
|
|
|
* |
177
|
|
|
* @return mixed |
178
|
|
|
*/ |
179
|
|
|
public function __call($method, $params) |
180
|
|
|
{ |
181
|
|
|
$arrCaller = array($this->validator, $method); |
182
|
|
|
|
183
|
|
|
return call_user_func_array($arrCaller, $params); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|