Completed
Push — master ( a5af75...4f6c0e )
by Alexpts
02:10
created

Validator   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 12
dl 0
loc 103
ccs 55
cts 55
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 1
A registerRule() 0 4 1
A getRules() 0 4 1
A validate() 0 17 3
A validateIfExists() 0 11 2
B validateValue() 0 22 5
A extractArrayRule() 0 4 1
A extractStringRule() 0 7 1
1
<?php
2
3
namespace PTS\Validator;
4
5
use PTS\Validator\Validators\AlphaDashValidator;
6
use PTS\Validator\Validators\AlphaNumValidator;
7
use PTS\Validator\Validators\AlphaValidator;
8
use PTS\Validator\Validators\BetweenIntValidator;
9
use PTS\Validator\Validators\BoolValidator;
10
use PTS\Validator\Validators\DateTimeValidator;
11
use PTS\Validator\Validators\DateValidator;
12
use PTS\Validator\Validators\InArrayValidator;
13
use PTS\Validator\Validators\MaxValidator;
14
use PTS\Validator\Validators\MinValidator;
15
use PTS\Validator\Validators\RequiredValidator;
16
17
class Validator
18
{
19
    /** @var bool */
20
    protected $paramDelimiter = ':';
21
22
    /** @var callable[] */
23
    protected $rulesHandlers = [];
24
25 37
    public function __construct()
26
    {
27 37
        $this->registerRule('string', 'is_string');
28 37
        $this->registerRule('int', 'is_int');
29 37
        $this->registerRule('array', 'is_array');
30 37
        $this->registerRule('required', new RequiredValidator);
31 37
        $this->registerRule('betweenInt', new BetweenIntValidator);
32 37
        $this->registerRule('strictBool', 'is_bool');
33 37
        $this->registerRule('bool', new BoolValidator);
34 37
        $this->registerRule('alpha', new AlphaValidator);
35 37
        $this->registerRule('alphaDash', new AlphaDashValidator);
36 37
        $this->registerRule('alphaNum', new AlphaNumValidator);
37 37
        $this->registerRule('date', new DateValidator);
38 37
        $this->registerRule('dateTime', new DateTimeValidator);
39 37
        $this->registerRule('inArray', new InArrayValidator);
40 37
        $this->registerRule('min', new MinValidator);
41 37
        $this->registerRule('max', new MaxValidator);
42 37
    }
43
44 37
    public function registerRule(string $name, callable $handler)
45
    {
46 37
        $this->rulesHandlers[$name] = $handler;
47 37
    }
48
49 2
    public function getRules(): array
50
    {
51 2
        return $this->rulesHandlers;
52
    }
53
54 27
    public function validate(array $data, array $rules): array
55
    {
56 27
        $errors = [];
57
58 27
        foreach ($rules as $name => $attrRules) {
59 27
            if (!array_key_exists($name, $data)) {
60 1
                $errors[$name] = 'Value is not exists: ' . $name;
61 1
                continue;
62
            }
63
64 27
            $value = $data[$name] ?? null;
65
66 27
            $errors[$name] = $this->validateValue($value, $attrRules);
67
        }
68
69 26
        return array_filter($errors);
70
    }
71
72 22
    public function validateIfExists(array $data, array $rules): array
73
    {
74 22
        $errors = [];
75
76 22
        foreach ($data as $name => $value) {
77 22
            $attrRules = $rules[$name] ?? [];
78 22
            $errors[$name] = $this->validateValue($value, $attrRules);
79
        }
80
81 22
        return array_filter($errors);
82
    }
83
84 29
    protected function validateValue($value, array $rules): array
85
    {
86 29
        $errors = [];
87
88 29
        foreach ($rules as $rule) {
89 29
            [$handlerAlias, $params] = is_string($rule)
0 ignored issues
show
Bug introduced by
The variable $handlerAlias does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $params does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
90 27
                ? $this->extractStringRule($rule)
91 29
                : $this->extractArrayRule($rule);
92
93 29
            $handler = $this->rulesHandlers[$handlerAlias] ?? null;
94
95 29
            if (!$handler) {
96 1
                throw new ValidatorRuleException("Handler not found for alias: {$handlerAlias}");
97
            }
98
99 29
            if (!$handler($value, ...$params)) {
100 10
                $errors[] = $handlerAlias;
101
            }
102
        }
103
104 28
        return $errors;
105
    }
106
107 5
    public function extractArrayRule(array $rule): array
108
    {
109 5
        return [key($rule), current($rule)];
110
    }
111
112 30
    public function extractStringRule(string $rule): array
113
    {
114 30
        $params = explode($this->paramDelimiter, $rule);
115 30
        $handlerAlias = array_shift($params);
116
117 30
        return [$handlerAlias, (array)$params];
118
    }
119
}
120