Passed
Push — master ( cd9e8c...6652bc )
by Henri
01:27
created

Check::check_minlength()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 5
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 9
rs 9.6111
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
12
    protected static function check_minlength(string $param, $value)
13
    {
14
        if(self::check_required($param) || strlen($value > 0)){
15
            if(strlen($value)===0){
16
                throw new Exception("O campo '{$param}' é obrigatório.",1);
17
            }
18
             
19
            if($value < intval(self::$validators[self::$model]->getRules(self::$data['role'])[$param]['minlength'])) {
20
                throw new Exception("{$param} não atingiu o mínimo de caracteres esperado.",1);
21
            }
22
        }       
23
    }
24
25
    protected static function check_regex(string $param, $value)
26
    {
27
        if(self::check_required($param) || strlen($value > 0)){
28
            if(!@preg_match(self::$validators[self::$model]->getRules(self::$data['role'])[$param]['regex'], json_decode(self::$data['data'])->$param)){
29
                throw new Exception("{$param} inválido(a).",1);
30
            }  
31
        }       
32
    }
33
34
    protected static function check_index(string $param, $value)
35
    {
36
        if(self::check_required($param) || strlen($value > 0)){
37
            
38
        }
39
    }
40
41
    protected static function check_equals(string $param, $value)
42
    {
43
        if(self::check_required($param) || strlen($value > 0)){
44
45
            if(!array_key_exists($param,json_decode(self::$data['data'],true))){
46
                throw new Exception("O servidor não encontrou a informação '{$value}' para ser comparada a '{$param}'.",1);
47
            }
48
            
49
            if(json_decode(self::$data['data'])->$param != json_decode(self::$data['data'],true)[$value]){
50
                throw new Exception(ucfirst($param).' está diferente de '.ucfirst($value),1);
51
            }
52
53
        }       
54
    }
55
56
    protected static function check_maxlength(string $param, $value)
57
    {
58
        if(self::check_required($param) || strlen($value > 0)){
59
            if($value > intval(self::$validators[self::$model]->getRules(self::$data['role'])[$param]['maxlength'])) {
60
                throw new Exception("{$param} ultrapassou o limite de caracteres permitidos.",1);
61
            }
62
        }       
63
    }
64
65
    protected static function check_type(string $param, $value)
66
    {
67
        if(self::check_required($param) || strlen($value > 0)){
68
            /*
69
            var_dump($value);
70
                    switch ($value) {
71
                        case 'date':
72
                            $date = explode('/', $valuee);
73
                            if(count($date) != 3){
74
                                throw new Exception('Data inválida.',1);
75
                            }
76
                            if(! checkdate( intval($date[1]), intval($date[0]), intval($date[2]) )){
77
                                throw new Exception('Data inválida.',1);
78
                            }
79
                            break;
80
                    }*/
81
        }       
82
    }
83
84
    protected static function check_required(string $param): bool
85
    {
86
        return (array_key_exists('required',self::$validators[self::$model]->getRules(self::$data['role'])[$param]) && self::$validators[self::$model]->getRules(self::$data['role'])[$param]['required']);
87
    }
88
89
}