Completed
Pull Request — master (#238)
by
unknown
02:21
created

DelegatedValidator::explodeRules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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