Passed
Push — master ( 2eee6e...3d96d8 )
by Henri
01:27
created

Validator   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Importance

Changes 27
Bugs 0 Features 9
Metric Value
eloc 67
dl 0
loc 156
rs 10
c 27
b 0
f 9
wmc 29

8 Methods

Rating   Name   Duplication   Size   Complexity  
A Check::checkType() 0 12 4
A Check::checkEquals() 0 11 3
A Check::checkFilter() 0 7 3
A Check::checkMaxlength() 0 11 5
A Check::checkMinlength() 0 16 6
A Check::checkMaxcount() 0 7 3
A Check::checkMincount() 0 7 3
A Check::checkRegex() 0 10 5
1
<?php
2
3
namespace HnrAzevedo\Validator;
4
5
Trait Check
6
{
7
    use ExtraCheck,
8
        Helper;
9
10
    protected function checkMinlength(string $param, $value): void
11
    {
12
        if(self::getInstance()->toNext($param, self::getInstance()->data($param))){ 
13
            
14
            $realval = (is_array(self::getInstance()->data($param))) ? self::getInstance()->data($param) : [self::getInstance()->data($param)];
15
16
            foreach($realval as $val){
17
                if(strlen($val) === 0) {
18
                    self::getInstance()->error([
19
                        $param => ' é obrigatório'
20
                    ]);
21
                    continue;
22
                }
23
                if($value > strlen($val)) {
24
                    self::getInstance()->error([
25
                        $param => 'não atingiu o mínimo de caracteres esperado'
26
                    ]);
27
                }
28
            }
29
        }       
30
    }
31
32
    protected function checkRegex(string $param, $value): void
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

32
    protected function checkRegex(string $param, /** @scrutinizer ignore-unused */ $value): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
33
    {
34
        if(self::getInstance()->toNext($param, self::getInstance()->data($param))){
35
36
            $realval = (is_array(self::getInstance()->data($param))) ? self::getInstance()->data($param) : [self::getInstance()->data($param)];
37
38
            foreach($realval as $val){
39
                if(!preg_match(self::getInstance()->validator(self::getInstance()->model())->getRules(self::getInstance()->data('ROLE'))[$param]['regex'], $val)){
40
                    self::getInstance()->error([
41
                        $param => 'inválido(a)'
42
                    ]);
43
                }  
44
            }
45
        }       
46
    }
47
48
    protected function checkMincount(string $param, $value): void
49
    {
50
        if(self::getInstance()->toNext($param, self::getInstance()->data($param))){
51
            $array = self::getInstance()->testArray($param, self::getInstance()->data($param));
52
            if(count($array) < $value){
53
                self::getInstance()->error([
54
                    $param => 'não atingiu o mínimo esperado'
55
                ]);
56
            }
57
        }
58
    }
59
60
    protected function checkMaxcount(string $param, $value): void
61
    {
62
        if(self::getInstance()->toNext($param, self::getInstance()->data($param))){
63
            $array = self::getInstance()->testArray($param, self::getInstance()->data($param));
64
            if(count($array) > $value){
65
                self::getInstance()->error([
66
                    $param => 'ultrapassou o esperado'
67
                ]);
68
            }
69
        }
70
    }
71
72
    protected function checkEquals(string $param, $value): void
73
    {
74
        if(!array_key_exists($param, self::getInstance()->data())){
75
            self::getInstance()->error([
76
                $param => "O servidor não encontrou a informação '{$value}' para ser comparada"
77
            ]);
78
        }
79
            
80
        if(self::getInstance()->data($param) != self::getInstance()->data($value)){
81
            self::getInstance()->error([
82
                $param => 'está diferente de '.ucfirst($value)
83
            ]);
84
        } 
85
    }
86
87
    protected function checkMaxlength(string $param, $value)
88
    {
89
        if(self::getInstance()->toNext($param, self::getInstance()->data($param))){
90
91
            $realval = (is_array(self::getInstance()->data($param))) ? self::getInstance()->data($param) : [self::getInstance()->data($param)];
92
93
            foreach($realval as $val){
94
95
                if($value < strlen($val)) {
96
                    self::getInstance()->error([
97
                        $param => 'ultrapassou o máximo de caracteres esperado'
98
                    ]);
99
                }
100
        
101
            }
102
        }       
103
    }
104
105
    protected function checkType(string $param, $value)
106
    {
107
        if(self::getInstance()->toNext($param, self::getInstance()->data($param))){
108
109
            switch ($value) {
110
                case 'date':
111
                    if(!self::getInstance()->validateDate(self::getInstance()->data($param) , 'd/m/Y')){
112
                        self::getInstance()->error([
113
                            $param => 'não é uma data válida'
114
                        ]);
115
                    }
116
                    break;
117
            }
118
        }       
119
    }
120
121
    protected function checkFilter(string $param, $value)
122
    {
123
        if(self::getInstance()->toNext($param, self::getInstance()->data($param))){
124
            
125
            if(!filter_var(self::getInstance()->data($param), $value)){
126
                self::getInstance()->error([
127
                    $param => 'não passou pela filtragem de dados'
128
                ]);
129
            }
130
131
        }
132
    }
133
134
}
135