|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace HnrAzevedo\Validator; |
|
4
|
|
|
|
|
5
|
|
|
use HnrAzevedo\Validator\Rules; |
|
6
|
|
|
use Exception; |
|
7
|
|
|
|
|
8
|
|
|
Class Validator{ |
|
9
|
|
|
use Check; |
|
10
|
|
|
|
|
11
|
|
|
public static function add(object $model,callable $return): void |
|
12
|
|
|
{ |
|
13
|
|
|
self::$model = get_class($model); |
|
14
|
|
|
self::$validators[self::$model] = $return($Rules = new Rules($model)); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
private static function existData() |
|
18
|
|
|
{ |
|
19
|
|
|
if(!array_key_exists('data', self::$data)){ |
|
20
|
|
|
throw new Exception('Informações cruciais não foram recebidas.'); |
|
21
|
|
|
} |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
private static function jsonData() |
|
25
|
|
|
{ |
|
26
|
|
|
if(json_decode(self::$data['data']) === null){ |
|
27
|
|
|
throw new Exception('O servidor recebeu as informações no formato esperado.'); |
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
private static function hasProvider() |
|
32
|
|
|
{ |
|
33
|
|
|
if(!array_key_exists('provider',self::$data)){ |
|
34
|
|
|
throw new Exception('O servidor não recebeu o ID do formulário.'); |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
private static function hasRole() |
|
39
|
|
|
{ |
|
40
|
|
|
if(!array_key_exists('role',self::$data)){ |
|
41
|
|
|
throw new Exception('O servidor não conseguiu identificar a finalidade deste formulário.'); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
private static function getClass(string $class) |
|
46
|
|
|
{ |
|
47
|
|
|
if(!class_exists($class)){ |
|
48
|
|
|
$class = basename($class); |
|
49
|
|
|
throw new Exception("Form ID {$class} inválido."); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return new $class(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
private static function existRole($rules) |
|
56
|
|
|
{ |
|
57
|
|
|
if(empty(self::$validators[$rules]->getRules(self::$data['role']))){ |
|
58
|
|
|
throw new Exception('Não existe regras para validar este formulário.'); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public static function checkDatas() |
|
63
|
|
|
{ |
|
64
|
|
|
self::existData(); |
|
65
|
|
|
self::jsonData(); |
|
66
|
|
|
self::hasProvider(); |
|
67
|
|
|
self::hasRole(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public static function execute(array $datas): bool |
|
71
|
|
|
{ |
|
72
|
|
|
self::$data = $datas; |
|
73
|
|
|
|
|
74
|
|
|
self::checkDatas(); |
|
75
|
|
|
|
|
76
|
|
|
$model = VALIDATOR_CONFIG['rules.namespace'].'\\'.ucfirst(self::$data['provider']); |
|
77
|
|
|
if(!class_exists($model)){ |
|
78
|
|
|
throw new Exception("No rules {$model} found."); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
self::$model = $model(); |
|
82
|
|
|
|
|
83
|
|
|
self::existRole(self::$model); |
|
84
|
|
|
|
|
85
|
|
|
foreach ( (self::$validators[self::$model]->getRules($datas['role'])) as $key => $value) { |
|
86
|
|
|
if(@$value['required'] === true){ |
|
87
|
|
|
self::$required[$key] = $value; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
self::$errors = []; |
|
92
|
|
|
|
|
93
|
|
|
self::validate(); |
|
94
|
|
|
|
|
95
|
|
|
self::checkRequireds(); |
|
96
|
|
|
|
|
97
|
|
|
return self::checkErrors(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public static function checkErrors(): bool |
|
101
|
|
|
{ |
|
102
|
|
|
return (count(self::$errors) === 0); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public static function validate() |
|
106
|
|
|
{ |
|
107
|
|
|
foreach ( (self::$validators[self::$model]->getRules(self::$data['role'])) as $key => $value) { |
|
108
|
|
|
|
|
109
|
|
|
foreach (json_decode(self::$data['data']) as $keyy => $valuee) { |
|
110
|
|
|
|
|
111
|
|
|
if(!array_key_exists($keyy, (self::$validators[self::$model]->getRules(self::$data['role'])) )){ |
|
112
|
|
|
throw new Exception("O campo '{$keyy}' não é esperado para está operação."); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
if($keyy===$key){ |
|
116
|
|
|
|
|
117
|
|
|
unset(self::$required[$key]); |
|
118
|
|
|
|
|
119
|
|
|
foreach ($value as $subkey => $subvalue) { |
|
120
|
|
|
$function = "check_{$subkey}"; |
|
121
|
|
|
self::testMethod($function); |
|
122
|
|
|
self::$function($keyy,$subvalue); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public static function getErrors(): array |
|
130
|
|
|
{ |
|
131
|
|
|
return self::$errors; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
public static function testMethod($method) |
|
135
|
|
|
{ |
|
136
|
|
|
if(!method_exists(static::class, $method)){ |
|
137
|
|
|
throw new Exception("{$method} não é uma validação válida."); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public static function toJson(array $request): string |
|
142
|
|
|
{ |
|
143
|
|
|
$response = null; |
|
144
|
|
|
|
|
145
|
|
|
self::$data['provider'] = $request['provider']; |
|
146
|
|
|
self::$data['role'] = $request['role']; |
|
147
|
|
|
|
|
148
|
|
|
self::includeValidations(); |
|
|
|
|
|
|
149
|
|
|
|
|
150
|
|
|
self::$model = get_class( self::getClass('HnrAzevedo\\Validator\\'.ucfirst($request['provider'])) ); |
|
151
|
|
|
|
|
152
|
|
|
self::existRole(self::$model); |
|
153
|
|
|
|
|
154
|
|
|
foreach ( self::$validators[self::$model]->getRules($request['role']) as $field => $r) { |
|
155
|
|
|
$r = self::replaceRegex($r); |
|
156
|
|
|
$response .= ("{$field}:".json_encode(array_reverse($r))).','; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return '{'.substr($response,0,-1).'}'; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
private static function replaceRegex(array $rules): array |
|
163
|
|
|
{ |
|
164
|
|
|
if(array_key_exists('regex',$rules)){ |
|
165
|
|
|
$rules['regex'] = substr($rules['regex'],1,-2); |
|
166
|
|
|
} |
|
167
|
|
|
return $rules; |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.