Completed
Push — master ( e95e41...a095e7 )
by Alessandro
02:03
created

Validator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Validoo;
4
5
/**
6
 * Validator Class
7
 * @author Alessandro Manno <[email protected]>
8
 * @author Chiara Ferrazza <[email protected]>
9
 * @copyright (c) 2016, Facile.it
10
 * @license https://github.com/facile-it/validoo/blob/master/LICENSE MIT Licence
11
 * @link https://github.com/facile-it/validoo
12
 */
13
14
/**
15
 * TODO: Exception handling for rules with parameters
16
 * TODO: unit tests for numeric, float, alpha_numeric, max_length, min_length, exact_length
17
 * TODO: add protection filters for several input vulnerabilities.
18
 */
19
class Validator
20
{
21
22
    /** @var array */
23
    private $errors = [];
24
    /** @var array */
25
    private $namings = [];
26
    /** @var array */
27
    private $customErrorsWithInputName = [];
28
    /** @var array */
29
    private $customErrors = [];
30
31
    /**
32
     * Constructor is not allowed because Validoo uses its own
33
     * static method to instantiate the validation
34
     * @param $errors
35
     * @param $namings
36
     */
37 48
    private function __construct($errors, $namings)
38
    {
39 48
        $this->errors = $errors;
40 48
        $this->namings = $namings;
41 48
    }
42
43
    /**
44
     * @param $inputs
45
     * @param array $rules
46
     * @param array|null $naming
47
     * @return Validator
48
     * @throws ValidooException
49
     */
50 48
    public static function validate($inputs, array $rules, array $naming = null): self
51
    {
52 48
        $errors = null;
53 48
        foreach ($rules as $input => $input_rules) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
54
55 48
            if (is_string($input_rules))
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
56 2
                $input_rules = explode("|", $input_rules);
57
58 48
            if (!is_array($input_rules))
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
59
                throw new ValidooException(ValidooException::ARRAY_EXPECTED, $input);
60
61 48
            if (in_array("onlyifset", $input_rules) && !isset($inputs[$input]))
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
62 1
                continue;
63
64 48
            foreach ($input_rules as $rule => $closure) {
65 48
                if (!isset($inputs[$input])) {
66 7
                    $input_value = null;
67
                } else {
68 41
                    $input_value = $inputs[$input];
69
                }
70 48
                if (is_numeric($rule)) {
71 46
                    $rule = $closure;
72
                }
73 48
                if ('onlyifset' == $rule)
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
74 1
                    continue;
75
76 48
                $rule_and_params = self::getParams($rule);
77 48
                $params = $real_params = $rule_and_params['params'];
78 48
                $rule = $rule_and_params['rule'];
79 48
                $params = self::getParamValues($params, $inputs);
80 48
                array_unshift($params, $input_value);
81
82 48
                if (false == self::doValidation($closure, $params, $rule)) {
83 27
                    $errors[$input][$rule]['result'] = false;
84 48
                    $errors[$input][$rule]['params'] = $real_params;
85
                }
86
            }
87
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
88
        }
89 48
        return new self($errors, $naming);
90
    }
91
92
    /**
93
     * @param $closure
94
     * @param $params
95
     * @param $rule
96
     * @return mixed
97
     * @throws ValidooException
98
     */
99 48
    private static function doValidation($closure, $params, $rule)
100
    {
101 48
        if (@get_class($closure) === 'Closure') {
102 2
            $refl_func = new \ReflectionFunction($closure);
103 2
            $validation = $refl_func->invokeArgs($params);
104 46
        } else if (@method_exists(get_called_class(), $rule)) {
105 46
            $refl = new \ReflectionMethod(get_called_class(), $rule);
106 46
            if ($refl->isStatic()) {
107 46
                $refl->setAccessible(true);
108 46
                $validation = $refl->invokeArgs(null, $params);
109
            } else {
110 46
                throw new ValidooException(ValidooException::STATIC_METHOD, $rule);
111
            }
112
        } else {
113
            throw new ValidooException(ValidooException::UNKNOWN_RULE, $rule);
114
        }
115 48
        return $validation;
116
    }
117
118
    /**
119
     * Gets the parameter names of a rule
120
     * @param $rule
121
     * @return mixed
122
     */
123 48
    private static function getParams($rule)
124
    {
125 48
        if (preg_match("#^([\w]+)\((.+?)\)$#", $rule, $matches)) {
126
            return [
127 6
                'rule' => $matches[1],
128 6
                'params' => explode(',', $matches[2])
129
            ];
130
        }
131
        return [
132 42
            'rule' => $rule,
133
            'params' => []
134
        ];
135
    }
136
137
    /**
138
     * Handle parameter with input name
139
     * eg: equals(:name)
140
     * @param mixed $params
141
     * @param $inputs
142
     * @return mixed
143
     */
144 48
    private static function getParamValues($params, $inputs)
145
    {
146 48
        foreach ($params as $key => $param) {
147 6
            if (preg_match("#^:([\w]+)$#", $param, $param_type)) {
148 6
                $params[$key] = @$inputs[(string)$param_type[1]];
149
            }
150
        }
151 48
        return $params;
152
    }
153
154
    /**
155
     * @param null $input
156
     * @return bool
157
     */
158 9
    protected static function required($input = null): bool
159
    {
160 9
        if (is_string($input))
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
161 3
            $input = trim($input);
162 9
        if (is_numeric($input))
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
163 2
            return true;
164
165 7
        return (null !== $input && !empty($input));
166
    }
167
168
    /**
169
     * @param $input
170
     * @return bool
171
     */
172
    protected static function numeric($input): bool
173
    {
174
        return is_numeric($input);
175
    }
176
177
    /**
178
     * @param $input
179
     * @return bool
180
     */
181 4
    protected static function email($input): bool
182
    {
183 4
        return filter_var($input, FILTER_VALIDATE_EMAIL);
184
    }
185
186
    /**
187
     * @param $input
188
     * @return bool
189
     */
190 2
    protected static function isdir($input): bool
191
    {
192 2
        return is_dir($input);
193
    }
194
195
    /**
196
     * @param $input
197
     * @return bool
198
     */
199
    protected static function isarray($input): bool
200
    {
201
        return is_array($input);
202
    }
203
204
    /**
205
     * @param $input
206
     * @return bool
207
     */
208 11
    protected static function integer($input): bool
209
    {
210 11
        return is_int($input) || ($input == (string)(int)$input);
211
    }
212
213
    /**
214
     * @param $input
215
     * @return bool
216
     */
217
    protected static function float($input): bool
218
    {
219
        return is_float($input) || ($input == (string)(float)$input);
220
    }
221
222
    /**
223
     * @param $input
224
     * @return bool
225
     */
226 4
    protected static function alpha($input): bool
227
    {
228 4
        return (preg_match("#^[a-zA-ZÀ-ÿ]+$#", $input) == 1);
229
    }
230
231
    /**
232
     * @param $input
233
     * @return bool
234
     */
235
    protected static function alpha_numeric($input): bool
0 ignored issues
show
Coding Style introduced by
Method name "Validator::alpha_numeric" is not in camel caps format
Loading history...
236
    {
237
        return (preg_match("#^[a-zA-ZÀ-ÿ0-9]+$#", $input) == 1);
238
    }
239
240
    /**
241
     * @param $input
242
     * @return bool
243
     */
244 4
    protected static function ip($input): bool
245
    {
246 4
        return filter_var($input, FILTER_VALIDATE_IP);
247
    }
248
249
    /**
250
     * @param $input
251
     * @return bool
252
     */
253 9
    protected static function url($input): bool
254
    {
255 9
        return filter_var($input, FILTER_VALIDATE_URL);
256
    }
257
258
    /**
259
     * @param $input
260
     * @param $length
261
     * @return bool
262
     */
263
    protected static function max_length($input, $length): bool
0 ignored issues
show
Coding Style introduced by
Method name "Validator::max_length" is not in camel caps format
Loading history...
264
    {
265
        return (strlen($input) <= $length);
266
    }
267
268
    /**
269
     * @param $input
270
     * @param $length
271
     * @return bool
272
     */
273
    protected static function min_length($input, $length): bool
0 ignored issues
show
Coding Style introduced by
Method name "Validator::min_length" is not in camel caps format
Loading history...
274
    {
275
        return (strlen($input) >= $length);
276
    }
277
278
    /**
279
     * @param $input
280
     * @param $length
281
     * @return bool
282
     */
283
    protected static function exact_length($input, $length): bool
0 ignored issues
show
Coding Style introduced by
Method name "Validator::exact_length" is not in camel caps format
Loading history...
284
    {
285
        return (strlen($input) == $length);
286
    }
287
288
    /**
289
     * @param $input
290
     * @param $param
291
     * @return bool
292
     */
293 4
    protected static function equals($input, $param): bool
294
    {
295 4
        return ($input == $param);
296
    }
297
298
    /**
299
     * @param $input
300
     * @return bool
301
     */
302
    protected static function is_filename($input): bool
0 ignored issues
show
Coding Style introduced by
Method name "Validator::is_filename" is not in camel caps format
Loading history...
303
    {
304
        return preg_match('/^[A-Za-z0-9-_]+[.]{1}[A-Za-z]+$/', $input);
305
    }
306
307
    /**
308
     * @return bool
309
     */
310 46
    public function isSuccess()
311
    {
312 46
        return empty($this->errors);
313
    }
314
315
    /**
316
     * @param array $errors_array
317
     */
318 1
    public function customErrors(array $errors_array)
319
    {
320 1
        foreach ($errors_array as $key => $value) {
321
            // handle input.rule eg (name.required)
322 1
            if (preg_match("#^(.+?)\.(.+?)$#", $key, $matches)) {
323
                // $this->customErrorsWithInputName[name][required] = error message
324
                $this->customErrorsWithInputName[(string)$matches[1]][(string)$matches[2]] = $value;
325
            } else {
326 1
                $this->customErrors[(string)$key] = $value;
327
            }
328
        }
329 1
    }
330
331
    /**
332
     * @param string|null $lang
333
     * @return array
334
     * @throws ValidooException
335
     */
336 3
    public function getErrors(string $lang = null): array
337
    {
338 3
        if (null === $lang) {
339 3
            $lang = $this->getDefaultLang();
340
        }
341
342 3
        $error_results = [];
343 3
        $default_error_texts = $this->getDefaultErrorTexts($lang);
344
345 3
        foreach ($this->errors as $input_name => $results) {
346 3
            foreach ($results as $rule => $result) {
347 3
                $named_input = $this->handleNaming($input_name);
348
                /**
349
                 * if parameters are input name they should be named as well
350
                 */
351 3
                $result['params'] = $this->handleParameterNaming($result['params']);
352
                // if there is a custom message with input name, apply it
353 3
                if (isset($this->customErrorsWithInputName[(string)$input_name][(string)$rule])) {
354
                    $error_message = $this->customErrorsWithInputName[(string)$input_name][(string)$rule];
355
                } // if there is a custom message for the rule, apply it
356 3
                else if (isset($this->customErrors[(string)$rule])) {
357 1
                    $error_message = $this->customErrors[(string)$rule];
358
                } // if there is a custom validator try to fetch from its error file
359 2
                else if (isset($custom_error_texts[(string)$rule])) {
360
                    $error_message = $custom_error_texts[(string)$rule];
361
                } // if none try to fetch from default error file
362 2
                else if (isset($default_error_texts[(string)$rule])) {
363 2
                    $error_message = $default_error_texts[(string)$rule];
364
                } else {
365
                    throw new ValidooException(ValidooException::NO_ERROR_TEXT, $rule);
366
                }
367
                /**
368
                 * handle :params(..)
369
                 */
370 3
                if (preg_match_all("#:params\((.+?)\)#", $error_message, $param_indexes)) {
371 1
                    foreach ($param_indexes[1] as $param_index) {
372 1
                        $error_message = str_replace(':params(' . $param_index . ')', $result['params'][$param_index], $error_message);
373
                    }
374
                }
375 3
                $error_results[] = str_replace(':attribute', $named_input, $error_message);
376
            }
377
        }
378
379 3
        return $error_results;
380
    }
381
382
    /**
383
     * @return string
384
     */
385 3
    protected function getDefaultLang(): string
386
    {
387 3
        return 'en';
388
    }
389
390
    /*
391
     * TODO: need improvements for tel and urn urls.
392
     * check out url.test.php for the test result
393
     * urn syntax: http://www.faqs.org/rfcs/rfc2141.html
394
     *
395
     */
396
397
    /**
398
     * @param string|null $lang
399
     * @return array|mixed
400
     */
401 3
    protected function getDefaultErrorTexts(string $lang = null)
402
    {
403
        /* handle default error text file */
404 3
        $default_error_texts = [];
405 3
        if (file_exists(__DIR__ . '/errors/' . $lang . '.php')) {
406
            /** @noinspection PhpIncludeInspection */
407 3
            $default_error_texts = include __DIR__ . '/errors/' . $lang . '.php';
408
        }
409 3
        if (file_exists(__DIR__ . '/errors/' . $lang . '.json')) {
410
            $default_error_texts = json_decode(file_get_contents(__DIR__ . '/errors/' . $lang . '.json'), true);
411
        }
412
413
414 3
        return $default_error_texts;
415
    }
416
417
    /**
418
     * @param string $input_name
419
     * @return mixed|string
420
     */
421 3
    protected function handleNaming(string $input_name)
422
    {
423 3
        if (isset($this->namings[$input_name])) {
424
            $named_input = $this->namings[$input_name];
425
        } else {
426 3
            $named_input = $input_name;
427
        }
428
429 3
        return $named_input;
430
    }
431
432
    /**
433
     * @param array $params
434
     * @return array
435
     */
436 3
    protected function handleParameterNaming(array $params)
437
    {
438 3
        foreach ($params as $key => $param) {
439 1
            if (preg_match("#^:([\w]+)$#", $param, $param_type)) {
440 1
                if (isset($this->namings[(string)$param_type[1]])) {
441 1
                    $params[$key] = $this->namings[(string)$param_type[1]];
442
                } else {
443 1
                    $params[$key] = $param_type[1];
444
                }
445
            }
446
        }
447
448 3
        return $params;
449
    }
450
451
    /**
452
     * @param string $input_name
453
     * @param string|null $rule_name
454
     * @return bool
455
     */
456
    public function has(string $input_name, string $rule_name = null): bool
457
    {
458
        if (null !== $rule_name) {
459
            return isset($this->errors[$input_name][$rule_name]);
460
        }
461
462
        return isset($this->errors[$input_name]);
463
    }
464
465
    /**
466
     * @return array
467
     */
468
    final public function getResults(): array
469
    {
470
        return $this->errors;
471
    }
472
}
473