Passed
Push — master ( 536b09...481374 )
by Henri
02:59
created

Check::check_errors()   A

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
namespace HnrAzevedo\Validator;
4
5
use Exception;
6
7
Trait Check{
8
    protected static array $data = [];
9
    protected static array $validators = [];
10
    protected static string $model = '';
11
    protected static array $required = [];
12
    protected static array $errors = [];
13
14
    protected static function check_minlength(string $param, $value)
15
    {
16
        if(self::toNext($param,$value)){    
17
            
18
            $realval = (is_array(json_decode(self::$data['data'])->$param)) ? json_decode(self::$data['data'])->$param : [json_decode(self::$data['data'])->$param];
19
20
            foreach($realval as $val){
21
                if($value > strlen($val)) {
22
                    throw new Exception("{$param} não atingiu o mínimo de caracteres esperado.",1);
23
                }
24
            }
25
        }       
26
    }
27
28
    protected static function check_errors(): bool
29
    {
30
        return (count(self::$errors) === 0);
31
    }
32
33
    protected static function check_requireds()
34
    {
35
        if(count(self::$required) > 0){
36
            self::$errors[] = 'As seguintes informações não poderam ser validadas: '.implode(', ',array_keys(self::$required)).'.';
37
        }
38
    }
39
40
    protected static function check_regex(string $param, $value)
41
    {
42
        if(self::toNext($param,$value)){
43
44
            $realval = (is_array(json_decode(self::$data['data'])->$param)) ? json_decode(self::$data['data'])->$param : [json_decode(self::$data['data'])->$param];
45
46
            foreach($realval as $val){
47
48
                if(!preg_match(self::$validators[self::$model]->getRules(self::$data['role'])[$param]['regex'], $val)){
49
                    throw new Exception("{$param} inválido(a).",1);
50
                }  
51
52
            }
53
        }       
54
    }
55
56
    protected static function check_mincount(string $param, $value)
57
    {
58
        if(self::toNext($param,$value)){
59
            $array = self::testArray($param, json_decode(self::$data['data'])->$param);
60
            if(count($array) < $value){
61
                throw new Exception("{$param} não atingiu o mínimo esperado.",1);
62
            }
63
        }
64
    }
65
66
    protected static function check_maxcount(string $param, $value)
67
    {
68
        if(self::toNext($param,$value)){
69
            $array = self::testArray($param, json_decode(self::$data['data'])->$param);
70
            if(count($array) > $value){
71
                throw new Exception("{$param} ultrapassou o esperado.",1);
72
            }
73
        }
74
    }
75
76
    protected static function testArray(string $param, $value): ?array
77
    {
78
        if(!is_array($value)){
79
            throw new Exception("Era esperado um informação em array para {$param}.");
80
        }
81
        return $value;
82
    }
83
84
    protected static function check_equals(string $param, $value)
85
    {
86
        if(self::toNext($param,$value)){
87
88
            if(!array_key_exists($param,json_decode(self::$data['data'],true))){
89
                throw new Exception("O servidor não encontrou a informação '{$value}' para ser comparada a '{$param}'.",1);
90
            }
91
            
92
            if(json_decode(self::$data['data'])->$param != json_decode(self::$data['data'],true)[$value]){
93
                throw new Exception(ucfirst($param).' está diferente de '.ucfirst($value),1);
94
            }
95
96
        }       
97
    }
98
99
    protected static function check_maxlength(string $param, $value)
100
    {
101
        if(self::toNext($param,$value)){
102
103
            $realval = (is_array(json_decode(self::$data['data'])->$param)) ? json_decode(self::$data['data'])->$param : [json_decode(self::$data['data'])->$param];
104
105
            foreach($realval as $val){
106
107
                if($value < strlen($val)) {
108
                    throw new Exception("{$param} ultrapassou o máximo de caracteres esperado.",1);
109
                }
110
            
111
            }
112
        }       
113
    }
114
115
    protected static function check_type(string $param, $value)
116
    {
117
        if(self::toNext($param,$value)){
118
119
            switch ($value) {
120
                case 'date':
121
                    if(!self::validateDate(json_decode(self::$data['data'])->$param , 'd/m/Y')){
122
                        throw new Exception("{$param} não é uma data válida.");
123
                    }
124
                    break;
125
            }
126
        }       
127
    }
128
129
    protected static function check_filter(string $param, $value)
130
    {
131
        if(self::toNext($param,$value)){
132
133
            if(!filter_var(json_decode(self::$data['data'])->$param, $value)){
134
                throw new Exception("{$param} não passou pela filtragem de dados.");
135
            }
136
137
        }
138
    }
139
140
    public static function validateDate($date, $format = 'Y-m-d H:i:s')
141
    {
142
        $d = \DateTime::createFromFormat($format, $date);
143
        return $d && $d->format($format) == $date;
144
    }
145
146
    protected static function check_required(string $param): bool
147
    {
148
        return (array_key_exists('required',self::$validators[self::$model]->getRules(self::$data['role'])[$param]) && self::$validators[self::$model]->getRules(self::$data['role'])[$param]['required']);
149
    }
150
151
    protected static function toNext(string $param, $value)
152
    {
153
        return (self::check_required($param) || strlen($value > 0));
154
    }
155
156
}