Validator::flushErrors()   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
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.9.5
13
 */
14
15
namespace Quantum\Libraries\Validation;
16
17
use Quantum\Libraries\Validation\Rules\Resource;
18
use Quantum\Libraries\Validation\Rules\General;
19
use Quantum\Libraries\Validation\Rules\Length;
20
use Quantum\Libraries\Validation\Rules\Lists;
21
use Quantum\Libraries\Validation\Rules\Type;
22
use Quantum\Libraries\Validation\Rules\File;
23
use Closure;
24
25
/**
26
 * Class Validator
27
 * @package Quantum\Libraries\Validator
28
 */
29
class Validator
30
{
31
32
    use General;
33
    use Type;
34
    use File;
35
    use Lists;
36
    use Length;
37
    use Resource;
38
39
    /**
40
     * Rules
41
     * @var array
42
     */
43
    private $rules = [];
44
45
    /**
46
     * Validation Errors
47
     * @var array
48
     */
49
    private $errors = [];
50
51
    /**
52
     * Request data
53
     * @var array
54
     */
55
    private $data = [];
56
57
    /**
58
     * Custom validations
59
     * @var array
60
     */
61
    private $customValidations = [];
62
63
    /**
64
     * Add a rules for given field
65
     * @param string $field
66
     * @param array $rules
67
     */
68
    public function addRule(string $field, array $rules)
69
    {
70
        if (!empty($field)) {
71
            foreach ($rules as $rule) {
72
                if (!isset($this->rules[$field])) {
73
                    $this->rules[$field] = [];
74
                }
75
76
                $this->rules[$field][array_keys($rule)[0]] = array_values($rule)[0];
77
            }
78
        }
79
    }
80
81
    /**
82
     * Adds rules for multiple fields
83
     * @param array $rules
84
     */
85
    public function addRules(array $rules)
86
    {
87
        foreach ($rules as $field => $params) {
88
            $this->addRule($field, $params);
89
        }
90
    }
91
92
    /**
93
     * Updates the single rule in rules list for given field
94
     * @param string $field
95
     * @param array $rule
96
     */
97
    public function updateRule(string $field, array $rule)
98
    {
99
        if (!empty($field)) {
100
            if (isset($this->rules[$field][array_keys($rule)[0]])) {
101
                $this->rules[$field][array_keys($rule)[0]] = array_values($rule)[0];
102
            }
103
        }
104
    }
105
106
    /**
107
     * Deletes the the rule in rules list for given field
108
     * @param string $field
109
     * @param string|null $rule
110
     */
111
    public function deleteRule(string $field, string $rule = null)
112
    {
113
        if (!empty($field)) {
114
            if (isset($this->rules[$field])) {
115
                if (!empty($rule) && isset($this->rules[$field][$rule])) {
116
                    unset($this->rules[$field][$rule]);
117
                } else {
118
                    if (empty($rule)) {
119
                        unset($this->rules[$field]);
120
                    }
121
                }
122
            }
123
        }
124
    }
125
126
    /**
127
     * Flush ruels
128
     */
129
    public function flushRules()
130
    {
131
        $this->rules = [];
132
        $this->flushErrors();
133
    }
134
135
    /**
136
     * Validates the data against the rules
137
     * @param array $data
138
     * @return bool
139
     */
140
    public function isValid(array $data): bool
141
    {
142
        $this->data = $data;
143
144
        if (count($this->rules)) {
145
146
            foreach ($this->rules as $field => $rule) {
147
                if (!array_key_exists($field, $data)) {
148
                    $data[$field] = '';
149
                }
150
            }
151
152
            foreach ($data as $field => $value) {
153
154
                if (isset($this->rules[$field])) {
155
                    foreach ($this->rules[$field] as $method => $param) {
156
157
                        if (is_callable([$this, $method])) {
158
                            $this->$method($field, $value, $param);
159
                        } elseif (isset($this->customValidations[$method])) {
160
                            $data = [
161
                                'rule' => $method,
162
                                'field' => $field,
163
                                'value' => $value,
164
                                'param' => $param ?? null
165
                            ];
166
167
                            $this->callCustomFunction($this->customValidations[$method], $data);
168
                        }
169
                    }
170
                }
171
            }
172
        }
173
174
        return !count($this->errors);
175
    }
176
177
    /**
178
     * Adds custom validation
179
     * @param string $rule
180
     * @param Closure $function
181
     */
182
    public function addValidation(string $rule, Closure $function)
183
    {
184
        if (!empty($rule) && is_callable($function)) {
185
            $this->customValidations[$rule] = $function;
186
        }
187
    }
188
189
    /**
190
     * Gets validation errors
191
     * @return array
192
     */
193
    public function getErrors(): array
194
    {
195
        if (count($this->errors)) {
196
            $messages = [];
197
            foreach ($this->errors as $field => $errors) {
198
                if (count($errors)) {
199
                    foreach ($errors as $rule => $param) {
200
                        $translationParams = [t('common.'.$field)];
201
202
                        if ($param) {
203
                            $translationParams[] = $param;
204
                        }
205
206
                        if (!isset($messages[$field])) {
207
                            $messages[$field] = [];
208
                        }
209
210
                        $messages[$field][] = t("validation.$rule", $translationParams);
211
                    }
212
                }
213
            }
214
215
            return $messages;
216
        }
217
218
        return [];
219
    }
220
221
    /**
222
     * Adds validation Error
223
     * @param string $field
224
     * @param string $rule
225
     * @param null|mixed $param
226
     */
227
    protected function addError(string $field, string $rule, $param = null)
228
    {
229
        if (!isset($this->errors[$field])) {
230
            $this->errors[$field] = [];
231
        }
232
233
        $this->errors[$field][$rule] = $param;
234
    }
235
236
    /**
237
     * Flush errors
238
     */
239
    public function flushErrors()
240
    {
241
        $this->errors = [];
242
    }
243
244
    /**
245
     * Calls custom function defined by developer
246
     * @param Closure $function
247
     * @param array $data
248
     */
249
    protected function callCustomFunction(Closure $function, array $data)
250
    {
251
        if (!empty($data['value'])) {
252
            if (is_callable($function)) {
253
                if (!$function($data['value'], $data['param'])) {
254
                    $this->addError($data['field'], $data['rule'], $data['param']);
255
                }
256
            }
257
        }
258
    }
259
}