Completed
Push — master ( 71ec50...18d3f1 )
by Albert
02:10
created

DelegatedValidator   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 14
c 2
b 1
f 0
lcom 2
cbo 2
dl 0
loc 174
ccs 32
cts 32
cp 1
rs 10

14 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 __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 16
    public function __construct(BaseValidator $validator)
30
    {
31 16
        $this->validator = $validator;
32 16
        $this->validatorMethod = $this->createProtectedCaller($validator);
33 16
    }
34
35 5
    private function callValidator($method, $args = [])
36
    {
37 5
        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
     * Delegate method calls to validator instance.
169
     *
170
     * @param $method
171
     * @param $params
172
     *
173
     * @return mixed
174
     */
175 1
    public function __call($method, $params)
176
    {
177 1
        $arrCaller = array($this->validator, $method);
178
179 1
        return call_user_func_array($arrCaller, $params);
180
    }
181
}
182