1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HnrAzevedo\Validator; |
4
|
|
|
|
5
|
|
|
use HnrAzevedo\Validator\Rules; |
6
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
7
|
|
|
|
8
|
|
|
Class Validator implements MiddlewareInterface |
9
|
|
|
{ |
10
|
|
|
use Check, |
11
|
|
|
MiddlewareTrait, |
12
|
|
|
Helper; |
13
|
|
|
|
14
|
|
|
private string $namespace = ''; |
15
|
|
|
private array $defaultData = [ |
16
|
|
|
'REQUEST_METHOD', |
17
|
|
|
'PROVIDER', |
18
|
|
|
'ROLE' |
19
|
|
|
]; |
20
|
|
|
|
21
|
|
|
public static function add(object $model,callable $return): void |
22
|
|
|
{ |
23
|
|
|
self::getInstance()->model = get_class($model); |
24
|
|
|
self::getInstance()->validators[self::getInstance()->model] = $return(new Rules($model)); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
private static function getClass(string $class) |
28
|
|
|
{ |
29
|
|
|
if(!class_exists($class)){ |
30
|
|
|
throw new \RuntimeException("Form ID {$class} inválido"); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$class = get_class(new $class()); |
34
|
|
|
|
35
|
|
|
return $class; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
private function existRole($rules) |
39
|
|
|
{ |
40
|
|
|
if(empty(self::getInstance()->validators[$rules]->getRules(self::getInstance()->data['ROLE']))){ |
41
|
|
|
throw new \RuntimeException('Não existe regras para validar este formulário'); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function checkDatas(array $data): void |
46
|
|
|
{ |
47
|
|
|
if(!isset($data['PROVIDER']) || !isset($data['ROLE'])){ |
48
|
|
|
throw new \RuntimeException('The server did not receive the information needed to retrieve the requested validator'); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public static function execute(array $data): bool |
53
|
|
|
{ |
54
|
|
|
try{ |
55
|
|
|
self::getInstance()->checkDatas($data); |
56
|
|
|
|
57
|
|
|
self::getInstance()->data = $data; |
58
|
|
|
|
59
|
|
|
$model = self::getInstance()->namespace.'\\'.ucfirst(self::getInstance()->data['PROVIDER']); |
60
|
|
|
|
61
|
|
|
self::getInstance()->model = self::getInstance()->getClass($model); |
62
|
|
|
|
63
|
|
|
self::getInstance()->existRole(self::getInstance()->model); |
64
|
|
|
|
65
|
|
|
foreach ( (self::getInstance()->validators[self::getInstance()->model]->getRules($data['ROLE'])) as $key => $value) { |
66
|
|
|
if(@$value['required'] === true){ |
67
|
|
|
self::getInstance()->required[$key] = $value; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
self::getInstance()->errors = []; |
72
|
|
|
|
73
|
|
|
self::getInstance()->validate(); |
74
|
|
|
self::getInstance()->checkRequireds(); |
75
|
|
|
}catch(\Exception $er){ |
76
|
|
|
self::getInstance()->errors[] = $er->getMessage(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return self::checkErrors(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public static function checkErrors(): bool |
83
|
|
|
{ |
84
|
|
|
return (count(self::getInstance()->errors) === 0); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function validate(): void |
88
|
|
|
{ |
89
|
|
|
foreach ( (self::getInstance()->validators[self::getInstance()->model]->getRules(self::getInstance()->data['ROLE'])) as $key => $value) { |
90
|
|
|
|
91
|
|
|
foreach (self::getInstance()->data as $keyy => $valuee) { |
92
|
|
|
|
93
|
|
|
self::getInstance()->checkExpected($keyy); |
94
|
|
|
|
95
|
|
|
if($keyy===$key){ |
96
|
|
|
|
97
|
|
|
unset(self::getInstance()->required[$key]); |
98
|
|
|
|
99
|
|
|
foreach ($value as $subkey => $subvalue) { |
100
|
|
|
$function = "check".ucfirst($subkey); |
101
|
|
|
self::getInstance()->testMethod($function); |
102
|
|
|
self::getInstance()->$function($keyy, $subvalue); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
private function checkExpected(string $keyy): void |
110
|
|
|
{ |
111
|
|
|
if(!array_key_exists($keyy, (self::getInstance()->validators[self::getInstance()->model]->getRules(self::getInstance()->data['ROLE'])) ) && !in_array($keyy, self::getInstance()->defaultData)){ |
112
|
|
|
throw new \RuntimeException("O campo '{$keyy}' não é esperado para está operação"); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public static function getErrors(): array |
117
|
|
|
{ |
118
|
|
|
return self::getInstance()->errors; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function testMethod($method) |
122
|
|
|
{ |
123
|
|
|
if(!method_exists(static::class, $method)){ |
124
|
|
|
throw new \RuntimeException("{$method} não é uma validação válida"); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public static function toJson(array $request): string |
129
|
|
|
{ |
130
|
|
|
$response = null; |
131
|
|
|
|
132
|
|
|
self::getInstance()->checkDatas($request); |
133
|
|
|
|
134
|
|
|
self::getInstance()->data['PROVIDER'] = $request['PROVIDER']; |
135
|
|
|
self::getInstance()->data['ROLE'] = $request['ROLE']; |
136
|
|
|
|
137
|
|
|
$model = self::getInstance()->namespace.'\\'.ucfirst($request['PROVIDER']); |
138
|
|
|
|
139
|
|
|
self::getInstance()->model = self::getClass($model); |
140
|
|
|
|
141
|
|
|
self::getInstance()->existRole(self::getInstance()->model); |
142
|
|
|
|
143
|
|
|
foreach ( self::getInstance()->validators[self::getInstance()->model]->getRules($request['ROLE']) as $field => $r) { |
144
|
|
|
$r = self::getInstance()->replaceRegex($r); |
145
|
|
|
$response .= ("{$field}:".json_encode(array_reverse($r))).','; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return '{'.substr($response,0,-1).'}'; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
private function replaceRegex(array $rules): array |
152
|
|
|
{ |
153
|
|
|
if(array_key_exists('regex', $rules)){ |
154
|
|
|
$rules['regex'] = substr($rules['regex'], 1, -2); |
155
|
|
|
} |
156
|
|
|
return $rules; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public static function namespace(string $namespace): Validator |
160
|
|
|
{ |
161
|
|
|
self::getInstance()->namespace = $namespace; |
162
|
|
|
return self::getInstance(); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|