Passed
Branch master (a71bcb)
by Henri
06:52
created

Check::checkEquals()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 5
nop 2
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace HnrAzevedo\Validator;
4
5
Trait Check{
6
    use ExtraCheck;
7
8
    protected static function checkMinlength(string $param, $value)
9
    {
10
        if(self::toNext($param,$value)){    
11
            
12
            $realval = (is_array(json_decode(self::$data['data'])->$param)) ? json_decode(self::$data['data'])->$param : [json_decode(self::$data['data'])->$param];
13
14
            foreach($realval as $val){
15
                if(strlen($val) === 0) {
16
                    self::$errors[] = [
17
                        $param => 'é obrigatório.'
18
                    ];
19
                    continue;
20
                }
21
                if($value > strlen($val)) {
22
                    self::$errors[] = [
23
                        $param => 'não atingiu o mínimo de caracteres esperado.'
24
                    ];
25
                }
26
            }
27
        }       
28
    }
29
30
    protected static function checkRegex(string $param, $value)
31
    {
32
        if(self::toNext($param,$value)){
33
34
            $realval = (is_array(json_decode(self::$data['data'])->$param)) ? json_decode(self::$data['data'])->$param : [json_decode(self::$data['data'])->$param];
35
36
            foreach($realval as $val){
37
38
                if(!preg_match(self::$validators[self::$model]->getRules(self::$data['role'])[$param]['regex'], $val)){
39
                    self::$errors[] = [
40
                        $param => 'inválido(a).'
41
                    ];
42
                }  
43
44
            }
45
        }       
46
    }
47
48
    protected static function checkMincount(string $param, $value)
49
    {
50
        if(self::toNext($param,$value)){
51
            $array = self::testArray($param, json_decode(self::$data['data'])->$param);
52
            if(count($array) < $value){
53
                self::$errors[] = [
54
                    $param => 'não atingiu o mínimo esperado.'
55
                ];
56
            }
57
        }
58
    }
59
60
    protected static function checkMaxcount(string $param, $value)
61
    {
62
        if(self::toNext($param,$value)){
63
            $array = self::testArray($param, json_decode(self::$data['data'])->$param);
64
            if(count($array) > $value){
65
                self::$errors[] = [
66
                    $param => 'ultrapassou o esperado.'
67
                ];
68
            }
69
        }
70
    }
71
72
    protected static function checkEquals(string $param, $value)
73
    {
74
        if(self::toNext($param,$value)){
75
76
            if(!array_key_exists($param,json_decode(self::$data['data'],true))){
77
                self::$errors[] = [
78
                    $param => "O servidor não encontrou a informação '{$value}' para ser comparada."
79
                ];
80
            }
81
            
82
            if(json_decode(self::$data['data'])->$param != json_decode(self::$data['data'],true)[$value]){
83
                self::$errors[] = [
84
                    $param => 'está diferente de '.ucfirst($value)
85
                ];
86
            }
87
88
        }       
89
    }
90
91
    protected static function checkMaxlength(string $param, $value)
92
    {
93
        if(self::toNext($param,$value)){
94
95
            $realval = (is_array(json_decode(self::$data['data'])->$param)) ? json_decode(self::$data['data'])->$param : [json_decode(self::$data['data'])->$param];
96
97
            foreach($realval as $val){
98
99
                if($value < strlen($val)) {
100
                    self::$errors[] = [
101
                        $param => 'ultrapassou o máximo de caracteres esperado.'
102
                    ];
103
                }
104
        
105
            }
106
        }       
107
    }
108
109
    protected static function checkType(string $param, $value)
110
    {
111
        if(self::toNext($param,$value)){
112
113
            switch ($value) {
114
                case 'date':
115
                    if(!self::validateDate(json_decode(self::$data['data'])->$param , 'd/m/Y')){
116
                        self::$errors[] = [
117
                            $param => 'não é uma data válida.'
118
                        ];
119
                    }
120
                    break;
121
            }
122
        }       
123
    }
124
125
    protected static function checkFilter(string $param, $value)
126
    {
127
        if(self::toNext($param,$value)){
128
129
            if(!filter_var(json_decode(self::$data['data'])->$param, $value)){
130
                self::$errors[] = [
131
                    $param => 'não passou pela filtragem de dados.'
132
                ];
133
            }
134
135
        }
136
    }
137
138
}
139