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