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