Completed
Push — master ( d659e0...3965b8 )
by Albert
06:25
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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
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
     * @param \Illuminate\Validation\Validator $validator
28
     */
29 18
    public function __construct(BaseValidator $validator)
30
    {
31 18
        $this->validator = $validator;
32 18
        $this->validatorMethod = $this->createProtectedCaller($validator);
33 18
    }
34
35
    /**
36
     * @param string $method
37
     */
38 6
    private function callValidator($method, $args = [])
39
    {
40 6
        return $this->callProtected($this->validatorMethod, $method, $args);
41
    }
42
43
    /**
44
     * Get current \Illuminate\Validation\Validator instance.
45
     *
46
     * @return \Illuminate\Validation\Validator
47
     */
48 1
    public function getValidator()
49
    {
50 1
        return $this->validator;
51
    }
52
53
    /**
54
     * Get the data under validation.
55
     *
56
     * @return array
57
     */
58 1
    public function getData()
59
    {
60 1
        return $this->validator->getData();
61
    }
62
63
    /**
64
     * Set the data under validation.
65
     *
66
     * @param array
67
     */
68 1
    public function setData($data)
69
    {
70 1
        $this->validator->setData($data);
71 1
    }
72
73
    /**
74
     * Get the validation rules.
75
     *
76
     * @return array
77
     */
78 1
    public function getRules()
79
    {
80 1
        return $this->validator->getRules();
81
    }
82
83
    /**
84
     * Get the files under validation.
85
     *
86
     * @return array
87
     */
88 1
    public function getFiles()
89
    {
90 1
        return $this->validator->getFiles();
91
    }
92
93
    /**
94
     * Set the files under validation.
95
     *
96
     * @param array $files
97
     *
98
     * @return BaseValidator
99
     */
100 1
    public function setFiles(array $files)
101
    {
102 1
        return $this->validator->setFiles($files);
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 1
    public function isImplicit($rule)
113
    {
114 1
        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 1
    public function doReplacements($message, $attribute, $rule, $parameters)
128
    {
129 1
        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 1
    public function hasRule($attribute, $rules)
141
    {
142 1
        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 1
    public function getMessage($attribute, $rule)
154
    {
155 1
        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 1
    public function parseRule($rules)
166
    {
167 1
        return $this->callValidator('parseRule', [$rules]);
168
    }
169
170
    /**
171
     * Explode the rules into an array of rules.
172
     *
173
     * @param  string|array  $rules
174
     * @return array
175
     */
176 1
    public function explodeRules($rules)
177
    {
178 1
        return $this->callValidator('explodeRules', [$rules]);
179
    }
180
181
    /**
182
     * Add conditions to a given field based on a Closure.
183
     *
184
     * @param  string  $attribute
185
     * @param  string|array  $rules
186
     * @param  callable  $callback
187
     * @return void
188
     */
189 1
    public function sometimes($attribute, $rules, callable $callback)
190
    {
191 1
        $this->validator->sometimes($attribute, $rules, $callback);
192 1
    }
193
194
    /**
195
     * Delegate method calls to validator instance.
196
     *
197
     * @param $method
198
     * @param $params
199
     *
200
     * @return mixed
201
     */
202 1
    public function __call($method, $params)
203
    {
204 1
        $arrCaller = array($this->validator, $method);
205
206 1
        return call_user_func_array($arrCaller, $params);
207
    }
208
}
209