AbstractValidator::errorsBag()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php namespace Salah3id\Domains\Validator;
2
3
use Illuminate\Support\MessageBag;
4
use Salah3id\Domains\Validator\Contracts\ValidatorInterface;
5
use Salah3id\Domains\Validator\Exceptions\ValidatorException;
6
7
/**
8
 * Class AbstractValidator
9
 * @package Salah3id\Domains\Validator
10
 * @author Anderson Andrade <[email protected]>
11
 */
12
abstract class AbstractValidator implements ValidatorInterface
13
{
14
    /**
15
     * @var int
16
     */
17
    protected $id = null;
18
19
    /**
20
     * Validator
21
     *
22
     * @var object
23
     */
24
    protected $validator;
25
26
    /**
27
     * Data to be validated
28
     *
29
     * @var array
30
     */
31
    protected $data = array();
32
33
    /**
34
     * Validation Rules
35
     *
36
     * @var array
37
     */
38
    protected $rules = array();
39
40
    /**
41
     * Validation Custom Messages
42
     *
43
     * @var array
44
     */
45
    protected $messages = array();
46
47
    /**
48
     * Validation Custom Attributes
49
     *
50
     * @var array
51
     */
52
    protected $attributes = array();
53
54
    /**
55
     * Validation errors
56
     *
57
     * @var MessageBag
58
     */
59
    protected $errors = array();
60
61
62
    /**
63
     * Set Id
64
     *
65
     * @param $id
66
     * @return $this
67
     */
68
    public function setId($id)
69
    {
70
        $this->id = $id;
71
        return $this;
72
    }
73
74
    /**
75
     * Set data to validate
76
     *
77
     * @param array $data
78
     * @return $this
79
     */
80
    public function with(array $data)
81
    {
82
        $this->data = $data;
83
84
        return $this;
85
    }
86
87
    /**
88
     * Return errors
89
     *
90
     * @return array
91
     */
92
    public function errors()
93
    {
94
        return $this->errorsBag()->all();
95
    }
96
97
    /**
98
     * Errors
99
     *
100
     * @return MessageBag
101
     */
102
    public function errorsBag()
103
    {
104
        return $this->errors;
105
    }
106
107
    /**
108
     * Pass the data and the rules to the validator
109
     *
110
     * @param string $action
111
     * @return boolean
112
     */
113
    abstract public function passes($action = null);
114
115
    /**
116
     * Pass the data and the rules to the validator or throws ValidatorException
117
     *
118
     * @throws ValidatorException
119
     * @param string $action
120
     * @return boolean
121
     */
122
    public function passesOrFail($action = null)
123
    {
124
        if (!$this->passes($action)) {
125
            throw new ValidatorException($this->errorsBag());
126
        }
127
128
        return true;
129
    }
130
131
    /**
132
     * Get rule for validation by action ValidatorInterface::RULE_CREATE or ValidatorInterface::RULE_UPDATE
133
     *
134
     * Default rule: ValidatorInterface::RULE_CREATE
135
     *
136
     * @param null $action
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $action is correct as it would always require null to be passed?
Loading history...
137
     * @return array
138
     */
139
    public function getRules($action = null)
140
    {
141
        $rules = $this->rules;
142
143
        if (isset($this->rules[$action])) {
144
            $rules = $this->rules[$action];
145
        }
146
147
        return $this->parserValidationRules($rules, $this->id);
148
    }
149
150
    /**
151
     * Set Rules for Validation
152
     *
153
     * @param array $rules
154
     * @return $this
155
     */
156
    public function setRules(array $rules)
157
    {
158
        $this->rules = $rules;
159
        return $this;
160
    }
161
162
    /**
163
     * Get Custom error messages for validation
164
     *
165
     * @return array
166
     */
167
    public function getMessages()
168
    {
169
        return $this->messages;
170
    }
171
172
    /**
173
     * Set Custom error messages for Validation
174
     *
175
     * @param array $messages
176
     * @return $this
177
     */
178
    public function setMessages(array $messages)
179
    {
180
        $this->messages = $messages;
181
        return $this;
182
    }
183
184
    /**
185
     * Get Custom error attributes for validation
186
     *
187
     * @return array
188
     */
189
    public function getAttributes()
190
    {
191
        return $this->attributes;
192
    }
193
194
    /**
195
     * Set Custom error attributes for Validation
196
     *
197
     * @param array $attributes
198
     * @return $this
199
     */
200
    public function setAttributes(array $attributes)
201
    {
202
        $this->attributes = $attributes;
203
        return $this;
204
    }
205
206
    /**
207
     * Parser Validation Rules
208
     *
209
     * @param $rules
210
     * @param null $id
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $id is correct as it would always require null to be passed?
Loading history...
211
     * @return array
212
     */
213
    protected function parserValidationRules($rules, $id = null)
214
    {
215
        if (null === $id) {
0 ignored issues
show
introduced by
The condition null === $id is always true.
Loading history...
216
            return $rules;
217
        }
218
219
        array_walk($rules, function (&$rules, $field) use ($id) {
220
            if (!is_array($rules)) {
221
                $rules = explode("|", $rules);
222
            }
223
224
            foreach ($rules as $ruleIdx => $rule) {
225
                // get name and parameters
226
                @list($name, $params) = array_pad(explode(":", $rule), 2, null);
227
228
                // only do someting for the unique rule
229
                if (strtolower($name) != "unique") {
230
                    continue; // continue in foreach loop, nothing left to do here
231
                }
232
233
                $p = array_map("trim", explode(",", $params));
234
235
                // set field name to rules key ($field) (laravel convention)
236
                if (!isset($p[1])) {
237
                    $p[1] = $field;
238
                }
239
240
                // set 3rd parameter to id given to getValidationRules()
241
                $p[2] = $id;
242
243
                $params = implode(",", $p);
244
                $rules[$ruleIdx] = $name.":".$params;
245
            }
246
        });
247
248
        return $rules;
249
    }
250
}
251