Completed
Push — master ( 230401...398601 )
by Albert
02:42
created

DelegatedValidator::callValidator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 1
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 22
    public function __construct(BaseValidator $validator)
31
    {
32 22
        $this->validator = $validator;
33 22
        $this->validatorMethod = $this->createProtectedCaller($validator);
34 22
    }
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
    /**
90
     * Determine if a given rule implies the attribute is required.
91
     *
92
     * @param string $rule
93
     *
94
     * @return bool
95
     */
96 1
    public function isImplicit($rule)
97
    {
98 1
        return $this->callValidator('isImplicit', [$rule]);
99
    }
100
101
    /**
102
     * Replace all error message place-holders with actual values.
103
     *
104
     * @param string $message
105
     * @param string $attribute
106
     * @param string $rule
107
     * @param array  $parameters
108
     *
109
     * @return string
110
     */
111 1
    public function doReplacements($message, $attribute, $rule, $parameters)
112
    {
113 1
        return $this->callValidator('doReplacements', [$message, $attribute, $rule, $parameters]);
114
    }
115
116
    /**
117
     * Determine if the given attribute has a rule in the given set.
118
     *
119
     * @param string       $attribute
120
     * @param string|array $rules
121
     *
122
     * @return bool
123
     */
124 1
    public function hasRule($attribute, $rules)
125
    {
126 1
        return $this->callValidator('hasRule', [$attribute, $rules]);
127
    }
128
129
    /**
130
     * Get the validation message for an attribute and rule.
131
     *
132
     * @param string $attribute
133
     * @param string $rule
134
     *
135
     * @return string
136
     */
137 1
    public function getMessage($attribute, $rule)
138
    {
139 1
        return $this->callValidator('getMessage', [$attribute, $rule]);
140
    }
141
142
    /**
143
     * Extract the rule name and parameters from a rule.
144
     *
145
     * @param array|string $rules
146
     *
147
     * @return array
148
     */
149 1
    public function parseRule($rules)
150
    {
151 1
        return $this->callValidator('parseRule', [$rules]);
152
    }
153
154
    /**
155
     * Explode the rules into an array of rules.
156
     *
157
     * @param  string|array  $rules
158
     * @return array
159
     */
160 1
    public function explodeRules($rules)
161
    {
162 1
        return $this->callValidator('explodeRules', [$rules]);
163
    }
164
165
    /**
166
     * Add conditions to a given field based on a Closure.
167
     *
168
     * @param  string  $attribute
169
     * @param  string|array  $rules
170
     * @param  callable  $callback
171
     * @return void
172
     */
173 1
    public function sometimes($attribute, $rules, callable $callback)
174
    {
175 1
        $this->validator->sometimes($attribute, $rules, $callback);
176 1
    }
177
178
    /**
179
     * Delegate method calls to validator instance.
180
     *
181
     * @param $method
182
     * @param $params
183
     *
184
     * @return mixed
185
     */
186 5
    public function __call($method, $params)
187
    {
188 5
        $arrCaller = array($this->validator, $method);
189
190 5
        return call_user_func_array($arrCaller, $params);
191
    }
192
}
193