Completed
Branch feature/conditional-validation... (02769e)
by Albert
34:59 queued 25:31
created

DelegatedValidator   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 198
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 16
c 3
b 1
f 0
lcom 2
cbo 2
dl 0
loc 198
ccs 37
cts 37
cp 1
rs 10

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A callValidator() 0 4 1
A getValidator() 0 4 1
A getData() 0 4 1
A setData() 0 4 1
A getRules() 0 4 1
A getFiles() 0 4 1
A setFiles() 0 4 1
A isImplicit() 0 4 1
A doReplacements() 0 4 1
A hasRule() 0 4 1
A getMessage() 0 4 1
A parseRule() 0 4 1
A explodeRules() 0 4 1
A sometimes() 0 4 1
A __call() 0 6 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 6
    private function callValidator($method, $args = [])
36
    {
37 6
        return $this->callProtected($this->validatorMethod, $method, $args);
38
    }
39
40
    /**
41
     * Get current \Illuminate\Validation\Validator instance.
42
     *
43
     * @return \Illuminate\Validation\Validator
44
     */
45 1
    public function getValidator()
46
    {
47 1
        return $this->validator;
48
    }
49
50
    /**
51
     * Get the data under validation.
52
     *
53
     * @return array
54
     */
55 1
    public function getData()
56
    {
57 1
        return $this->validator->getData();
58
    }
59
60
    /**
61
     * Set the data under validation.
62
     *
63
     * @param array
64
     */
65 1
    public function setData($data)
66
    {
67 1
        $this->validator->setData($data);
68 1
    }
69
70
    /**
71
     * Get the validation rules.
72
     *
73
     * @return array
74
     */
75 1
    public function getRules()
76
    {
77 1
        return $this->validator->getRules();
78
    }
79
80
    /**
81
     * Get the files under validation.
82
     *
83
     * @return array
84
     */
85 1
    public function getFiles()
86
    {
87 1
        return $this->validator->getFiles();
88
    }
89
90
    /**
91
     * Set the files under validation.
92
     *
93
     * @param array $files
94
     *
95
     * @return BaseValidator
96
     */
97 1
    public function setFiles(array $files)
98
    {
99 1
        return $this->validator->setFiles($files);
100
    }
101
102
    /**
103
     * Determine if a given rule implies the attribute is required.
104
     *
105
     * @param string $rule
106
     *
107
     * @return bool
108
     */
109 1
    public function isImplicit($rule)
110
    {
111 1
        return $this->callValidator('isImplicit', [$rule]);
112
    }
113
114
    /**
115
     * Replace all error message place-holders with actual values.
116
     *
117
     * @param string $message
118
     * @param string $attribute
119
     * @param string $rule
120
     * @param array  $parameters
121
     *
122
     * @return string
123
     */
124 1
    public function doReplacements($message, $attribute, $rule, $parameters)
125
    {
126 1
        return $this->callValidator('doReplacements', [$message, $attribute, $rule, $parameters]);
127
    }
128
129
    /**
130
     * Determine if the given attribute has a rule in the given set.
131
     *
132
     * @param string       $attribute
133
     * @param string|array $rules
134
     *
135
     * @return bool
136
     */
137 1
    public function hasRule($attribute, $rules)
138
    {
139 1
        return $this->callValidator('hasRule', [$attribute, $rules]);
140
    }
141
142
    /**
143
     * Get the validation message for an attribute and rule.
144
     *
145
     * @param string $attribute
146
     * @param string $rule
147
     *
148
     * @return string
149
     */
150 1
    public function getMessage($attribute, $rule)
151
    {
152 1
        return $this->callValidator('getMessage', [$attribute, $rule]);
153
    }
154
155
    /**
156
     * Extract the rule name and parameters from a rule.
157
     *
158
     * @param array|string $rules
159
     *
160
     * @return array
161
     */
162 1
    public function parseRule($rules)
163
    {
164 1
        return $this->callValidator('parseRule', [$rules]);
165
    }
166
167
    /**
168
     * Explode the rules into an array of rules.
169
     *
170
     * @param  string|array  $rules
171
     * @return array
172
     */
173 1
    public function explodeRules($rules)
174
    {
175 1
        return $this->callValidator('explodeRules', [$rules]);
176
    }
177
178
    /**
179
     * Add conditions to a given field based on a Closure.
180
     *
181
     * @param  string|array  $attribute
182
     * @param  string|array  $rules
183
     * @param  callable  $callback
184
     * @return void
185
     */
186 1
    public function sometimes($attribute, $rules, callable $callback)
187
    {
188 1
        $this->validator->sometimes($attribute, $rules, $callback);
0 ignored issues
show
Bug introduced by
It seems like $attribute defined by parameter $attribute on line 186 can also be of type array; however, Illuminate\Validation\Validator::sometimes() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
189 1
    }
190
191
    /**
192
     * Delegate method calls to validator instance.
193
     *
194
     * @param $method
195
     * @param $params
196
     *
197
     * @return mixed
198
     */
199 1
    public function __call($method, $params)
200
    {
201 1
        $arrCaller = array($this->validator, $method);
202
203 1
        return call_user_func_array($arrCaller, $params);
204
    }
205
}
206