Completed
Pull Request — master (#206)
by Albert
02:28
created

DelegatedValidator::sometimes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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