Completed
Push — master ( 328c23...368b19 )
by
unknown
07:01 queued 12s
created

DelegatedValidator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Proengsoft\JsValidation\Support;
4
5
use Illuminate\Validation\Validator as BaseValidator;
6
7
class DelegatedValidator
8
{
9
    use AccessProtectedTrait;
10
11
    /**
12
     * The Validator resolved instance.
13
     *
14
     * @var \Illuminate\Validation\Validator
15
     */
16
    protected $validator;
17
18
    /**
19
     * Validation rule parser instance.
20
     *
21
     * @var \Proengsoft\JsValidation\Support\ValidationRuleParserProxy
22
     */
23
    protected $ruleParser;
24
25
    /**
26
     * Closure to invoke non accessible Validator methods.
27
     *
28
     * @var \Closure
29
     */
30
    protected $validatorMethod;
31
32
    /**
33
     * DelegatedValidator constructor.
34
     *
35
     * @param \Illuminate\Validation\Validator $validator
36
     * @param \Proengsoft\JsValidation\Support\ValidationRuleParserProxy $ruleParser
37
     */
38 288
    public function __construct(BaseValidator $validator, ValidationRuleParserProxy $ruleParser)
39
    {
40 288
        $this->validator = $validator;
41 288
        $this->ruleParser = $ruleParser;
42 288
        $this->validatorMethod = $this->createProtectedCaller($validator);
43 288
    }
44
45
    /**
46
     * Call validator method.
47
     *
48
     * @param string $method
49
     * @param array $args
50
     * @return mixed
51
     */
52 84
    private function callValidator($method, $args = [])
53
    {
54 84
        return $this->callProtected($this->validatorMethod, $method, $args);
55
    }
56
57
    /**
58
     * Get current \Illuminate\Validation\Validator instance.
59
     *
60
     * @return \Illuminate\Validation\Validator
61
     */
62 12
    public function getValidator()
63
    {
64 12
        return $this->validator;
65
    }
66
67
    /**
68
     * Get the data under validation.
69
     *
70
     * @return array
71
     */
72 12
    public function getData()
73
    {
74 12
        return $this->validator->getData();
75
    }
76
77
    /**
78
     * Set the data under validation.
79
     *
80
     * @param array
81
     */
82 12
    public function setData($data)
83
    {
84 12
        $this->validator->setData($data);
85 12
    }
86
87
    /**
88
     * Get the validation rules.
89
     *
90
     * @return array
91
     */
92 12
    public function getRules()
93
    {
94 12
        return $this->validator->getRules();
95
    }
96
97
    /**
98
     * Determine if a given rule implies the attribute is required.
99
     *
100
     * @param string $rule
101
     * @return bool
102
     */
103 12
    public function isImplicit($rule)
104
    {
105 12
        return $this->callValidator('isImplicit', [$rule]);
106
    }
107
108
    /**
109
     * Replace all error message place-holders with actual values.
110
     *
111
     * @param string $message
112
     * @param string $attribute
113
     * @param string $rule
114
     * @param array $parameters
115
     * @return string
116
     */
117 24
    public function makeReplacements($message, $attribute, $rule, $parameters)
118
    {
119 24
        if (is_object($rule)) {
120
            $rule = get_class($rule);
121
        }
122
        
123 24
        return $this->callValidator('makeReplacements', [$message, $attribute, $rule, $parameters]);
124
    }
125
126
    /**
127
     * Determine if the given attribute has a rule in the given set.
128
     *
129
     * @param string $attribute
130
     * @param string|array $rules
131
     * @return bool
132
     */
133 12
    public function hasRule($attribute, $rules)
134
    {
135 12
        return $this->callValidator('hasRule', [$attribute, $rules]);
136
    }
137
138
    /**
139
     * Get the validation message for an attribute and rule.
140
     *
141
     * @param string $attribute
142
     * @param string $rule
143
     * @return string
144
     */
145 24
    public function getMessage($attribute, $rule)
146
    {
147 24
        if (is_object($rule)) {
148 12
            $rule = get_class($rule);
149 1
        }
150
        
151 24
        return $this->callValidator('getMessage', [$attribute, $rule]);
152
    }
153
154
    /**
155
     * Extract the rule name and parameters from a rule.
156
     *
157
     * @param array|string $rules
158
     * @return array
159
     */
160 12
    public function parseRule($rules)
161
    {
162 12
        return $this->ruleParser->parse($rules);
163
    }
164
165
    /**
166
     * Explode the rules into an array of rules.
167
     *
168
     * @param string|array $rules
169
     * @return array
170
     */
171 12
    public function explodeRules($rules)
172
    {
173 12
        return $this->callValidator('explodeRules', [$rules]);
174
    }
175
176
    /**
177
     * Add conditions to a given field based on a Closure.
178
     *
179
     * @param string $attribute
180
     * @param string|array $rules
181
     * @param callable $callback
182
     * @return void
183
     */
184 12
    public function sometimes($attribute, $rules, callable $callback)
185
    {
186 12
        $this->validator->sometimes($attribute, $rules, $callback);
187 12
    }
188
189
    /**
190
     * Delegate method calls to validator instance.
191
     *
192
     * @param $method
193
     * @param $params
194
     * @return mixed
195
     */
196 60
    public function __call($method, $params)
197
    {
198 60
        $arrCaller = [$this->validator, $method];
199
200 60
        return call_user_func_array($arrCaller, $params);
201
    }
202
}
203