Completed
Push — master ( c77bf4...c35302 )
by Renato
16:40
created

BaseValidator::passes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 1
dl 0
loc 14
ccs 9
cts 9
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace NwLaravel\Validation;
3
4
use Illuminate\Validation\Factory;
5
use Prettus\Validator\AbstractValidator;
6
7
/**
8
 * Class BaseValidator
9
 * @abstract
10
 */
11
abstract class BaseValidator extends AbstractValidator
12
{
13
    /**
14
     * Validator
15
     *
16
     * @var \Illuminate\Validation\Factory
17
     */
18
    protected $validator;
19
20
    /**
21
     * @var string
22
     */
23
    protected $keyName;
24
25
    /**
26
     * @var array
27
     */
28
    protected $messages = [];
29
30
    /**
31
     * @var array
32
     */
33
    protected $attributes = [];
34
35
    /**
36
     * Construct
37
     *
38
     * @param \Illuminate\Validation\Factory $validator
39
     */
40 3
    public function __construct(Factory $validator)
41
    {
42 3
        $this->validator = $validator;
43 3
        $this->rules = array_merge_recursive((array) $this->rules, $this->makeRules());
44 3
        $this->messages = array_merge_recursive((array) $this->messages, $this->makeMessages());
45 3
        $this->attributes = array_merge_recursive((array) $this->attributes, $this->makeAttributes());
46 3
    }
47
48
    /**
49
     * MakeRules
50
     *
51
     * @return array
52
     */
53 3
    protected function makeRules()
54
    {
55 3
        return [];
56
    }
57
58
    /**
59
     * Make Messages
60
     *
61
     * @return array
62
     */
63 3
    protected function makeMessages()
64
    {
65 3
        return [];
66
    }
67
68
    /**
69
     * Make Attributes
70
     *
71
     * @return array
72
     */
73 3
    protected function makeAttributes()
74
    {
75 3
        return [];
76
    }
77
78
    /**
79
     * Get Messages
80
     *
81
     * @param array $messages
0 ignored issues
show
Bug introduced by
There is no parameter named $messages. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
82
     *
83
     * @return array
84
     */
85 3
    public function getMessages()
86
    {
87 3
        return $this->messages;
88
    }
89
90
    /**
91
     * Get Attributes
92
     *
93
     * @param array $attributes
0 ignored issues
show
Bug introduced by
There is no parameter named $attributes. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
94
     *
95
     * @return array
96
     */
97 3
    public function getAttributes()
98
    {
99 3
        return $this->attributes;
100
    }
101
102
    /**
103
     * Get Validator
104
     *
105
     * @return \Illuminate\Validation\Factory
106
     */
107 1
    public function getValidator()
108
    {
109 1
        return $this->validator;
110
    }
111
112
    /**
113
     * Set Key Name
114
     *
115
     * @param string $keyName
116
     *
117
     * @return BaseValidator
118
     */
119 1
    public function setKeyName($keyName)
120
    {
121 1
        $this->keyName = $keyName;
122 1
    }
123
124
    /**
125
     * Get rule for validation by action ValidatorInterface::RULE_CREATE or ValidatorInterface::RULE_UPDATE
126
     *
127
     * Default rule: ValidatorInterface::RULE_CREATE
128
     *
129
     * @param string|null $action
130
     *
131
     * @return array
132
     */
133 3
    public function getRules($action = null)
134
    {
135 3
        $rules = [];
136
137 3
        if (isset($this->rules[$action])) {
138 3
            $rules = $this->rules[$action];
139 3
        }
140
141 3
        return $this->parserValidationRules($rules, $this->id);
142
    }
143
144
    /**
145
     * Pass the data and the rules to the validator
146
     *
147
     * @param string $action
148
     * @return bool
149
     */
150 2
    public function passes($action = null)
151
    {
152 2
        $rules      = $this->getRules($action);
153 2
        $messages   = $this->getMessages();
154 2
        $attributes = $this->getAttributes();
155 2
        $validator  = $this->validator->make($this->data, $rules, $messages, $attributes);
156
157 2
        if ($validator->fails()) {
158 1
            $this->errors = $validator->messages();
159 1
            return false;
160
        }
161
162 1
        return true;
163
    }
164
165
    /**
166
     * Parser Validation Rules
167
     *
168
     * @param array    $rules
169
     * @param int|null $id
170
     *
171
     * @return array
172
     */
173 3
    protected function parserValidationRules($rules, $id = null)
174
    {
175
176 3
        if ($id === null) {
177 3
            return $rules;
178
        }
179
180 1
        array_walk($rules, function (&$rules, $field) use ($id) {
181 1
            if (!is_array($rules)) {
182 1
                $rules = explode("|", $rules);
183 1
            }
184
185 1
            foreach ($rules as $ruleIdx => $rule) {
186
                // get name and parameters
187 1
                list($name, $params) = array_pad(explode(":", $rule), 2, null);
188
189
                // only do someting for the unique rule
190 1
                if (strtolower($name) != "unique") {
191 1
                    if (preg_match('/\[(.*)\]/', $params, $matches)) {
192 1
                        if (array_key_exists($matches[1], $this->data)) {
193 1
                            $params = str_replace("[".$matches[1]."]", $this->getValue($matches[1]), $params);
194 1
                            $rules[$ruleIdx] = $name.":".$params;
195 1
                        }
196 1
                    }
197 1
                    continue; // continue in foreach loop, nothing left to do here
198
                }
199
200 1
                $p = array_map("trim", explode(",", $params));
201
202
                // set field name to rules key ($field) (laravel convention)
203 1
                if (!isset($p[1])) {
204 1
                    $p[1] = $field;
205 1
                }
206
207
                // set 3rd parameter to id given to getValidationRules()
208 1
                if (!isset($p[2]) || empty($p[2])) {
209 1
                    $p[2] = $id;
210 1
                }
211
212 1
                if ($this->keyName && (!isset($p[3]) || empty($p[3]))) {
213 1
                    $p[3] = $this->keyName;
214 1
                }
215
216 1
                $params = implode(",", $p);
217 1
                $rules[$ruleIdx] = $name.":".$params;
218 1
            }
219 1
        });
220
221 1
        return $rules;
222
    }
223
224
    /**
225
     * Get the value of a given attribute.
226
     *
227
     * @param  string  $attribute
228
     * @return mixed
229
     */
230 1
    protected function getValue($attribute)
231
    {
232 1
        if (! is_null($value = array_get($this->data, $attribute))) {
233 1
            return $value;
234
        }
235
    }
236
}
237