@@ -52,7 +52,7 @@ discard block |
||
52 | 52 | private static function getClass(string $class) |
53 | 53 | { |
54 | 54 | if(!class_exists($class)){ |
55 | - $class = basename($class); |
|
55 | + $class = basename($class); |
|
56 | 56 | throw new Exception("Form ID {$class} inválido."); |
57 | 57 | } |
58 | 58 | |
@@ -68,7 +68,7 @@ discard block |
||
68 | 68 | |
69 | 69 | public static function checkDatas() |
70 | 70 | { |
71 | - self::existData(); |
|
71 | + self::existData(); |
|
72 | 72 | self::jsonData(); |
73 | 73 | self::hasProvider(); |
74 | 74 | self::hasRole(); |
@@ -84,9 +84,9 @@ discard block |
||
84 | 84 | |
85 | 85 | self::$model = get_class(self::getClass('HnrAzevedo\\Validator\\'.ucfirst(self::$data['provider']))); |
86 | 86 | |
87 | - self::existRole(self::$model); |
|
87 | + self::existRole(self::$model); |
|
88 | 88 | |
89 | - foreach ( (self::$validators[self::$model]->getRules($datas['role'])) as $key => $value) { |
|
89 | + foreach ( (self::$validators[self::$model]->getRules($datas['role'])) as $key => $value) { |
|
90 | 90 | if(@$value['required'] === true){ |
91 | 91 | self::$required[$key] = $value; |
92 | 92 | } |
@@ -98,7 +98,7 @@ discard block |
||
98 | 98 | |
99 | 99 | self::check_requireds(); |
100 | 100 | |
101 | - return self::check_errors(); |
|
101 | + return self::check_errors(); |
|
102 | 102 | } |
103 | 103 | |
104 | 104 | public static function check_errors(): bool |
@@ -110,23 +110,23 @@ discard block |
||
110 | 110 | { |
111 | 111 | foreach ( (self::$validators[self::$model]->getRules(self::$data['role'])) as $key => $value) { |
112 | 112 | |
113 | - foreach (json_decode(self::$data['data']) as $keyy => $valuee) { |
|
113 | + foreach (json_decode(self::$data['data']) as $keyy => $valuee) { |
|
114 | 114 | |
115 | - if(!array_key_exists($keyy, (self::$validators[self::$model]->getRules(self::$data['role'])) )){ |
|
115 | + if(!array_key_exists($keyy, (self::$validators[self::$model]->getRules(self::$data['role'])) )){ |
|
116 | 116 | throw new Exception("O campo '{$keyy}' não é esperado para está operação."); |
117 | 117 | } |
118 | 118 | |
119 | - if($keyy===$key){ |
|
119 | + if($keyy===$key){ |
|
120 | 120 | |
121 | 121 | unset(self::$required[$key]); |
122 | 122 | |
123 | - foreach ($value as $subkey => $subvalue) { |
|
123 | + foreach ($value as $subkey => $subvalue) { |
|
124 | 124 | $function = "check_{$subkey}"; |
125 | 125 | self::testMethod($function); |
126 | 126 | self::$function($keyy,$subvalue); |
127 | - } |
|
128 | - } |
|
129 | - } |
|
127 | + } |
|
128 | + } |
|
129 | + } |
|
130 | 130 | } |
131 | 131 | } |
132 | 132 | |
@@ -155,10 +155,10 @@ discard block |
||
155 | 155 | |
156 | 156 | self::existRole(self::$model); |
157 | 157 | |
158 | - foreach ( self::$validators[self::$model]->getRules($request['role']) as $field => $r) { |
|
158 | + foreach ( self::$validators[self::$model]->getRules($request['role']) as $field => $r) { |
|
159 | 159 | $response .= ("{$field}:".json_encode(array_reverse($r))).','; |
160 | 160 | } |
161 | 161 | |
162 | 162 | return '{'.substr($response,0,-1).'}'; |
163 | - } |
|
163 | + } |
|
164 | 164 | } |
@@ -5,10 +5,10 @@ discard block |
||
5 | 5 | use HnrAzevedo\Validator\Rules; |
6 | 6 | use Exception; |
7 | 7 | |
8 | -Class Validator{ |
|
8 | +Class Validator { |
|
9 | 9 | use Check; |
10 | 10 | |
11 | - public static function add(object $model,callable $return): void |
|
11 | + public static function add(object $model, callable $return): void |
|
12 | 12 | { |
13 | 13 | self::$model = get_class($model); |
14 | 14 | self::$validators[self::$model] = $return($Rules = new Rules($model)); |
@@ -16,42 +16,42 @@ discard block |
||
16 | 16 | |
17 | 17 | private static function existData() |
18 | 18 | { |
19 | - if(!array_key_exists('data', self::$data)){ |
|
19 | + if (!array_key_exists('data', self::$data)) { |
|
20 | 20 | throw new Exception('Informações cruciais não foram recebidas.'); |
21 | 21 | } |
22 | 22 | } |
23 | 23 | |
24 | 24 | private static function jsonData() |
25 | 25 | { |
26 | - if(json_decode(self::$data['data']) === null){ |
|
26 | + if (json_decode(self::$data['data'])===null) { |
|
27 | 27 | throw new Exception('O servidor recebeu as informações no formato esperado.'); |
28 | 28 | } |
29 | 29 | } |
30 | 30 | |
31 | 31 | private static function hasProvider() |
32 | 32 | { |
33 | - if(!array_key_exists('provider',self::$data)){ |
|
33 | + if (!array_key_exists('provider', self::$data)) { |
|
34 | 34 | throw new Exception('O servidor não recebeu o ID do formulário.'); |
35 | 35 | } |
36 | 36 | } |
37 | 37 | |
38 | 38 | private static function hasRole() |
39 | 39 | { |
40 | - if(!array_key_exists('role',self::$data)){ |
|
40 | + if (!array_key_exists('role', self::$data)) { |
|
41 | 41 | throw new Exception('O servidor não conseguiu identificar a finalidade deste formulário.'); |
42 | 42 | } |
43 | 43 | } |
44 | 44 | |
45 | 45 | private static function includeValidations() |
46 | 46 | { |
47 | - if( file_exists(VALIDATOR_CONFIG['path'] . ucfirst(self::$data['provider']) . '.php') ){ |
|
48 | - require_once(VALIDATOR_CONFIG['path'] . ucfirst(self::$data['provider']) . '.php'); |
|
47 | + if (file_exists(VALIDATOR_CONFIG['path'].ucfirst(self::$data['provider']).'.php')) { |
|
48 | + require_once(VALIDATOR_CONFIG['path'].ucfirst(self::$data['provider']).'.php'); |
|
49 | 49 | } |
50 | 50 | } |
51 | 51 | |
52 | 52 | private static function getClass(string $class) |
53 | 53 | { |
54 | - if(!class_exists($class)){ |
|
54 | + if (!class_exists($class)) { |
|
55 | 55 | $class = basename($class); |
56 | 56 | throw new Exception("Form ID {$class} inválido."); |
57 | 57 | } |
@@ -61,7 +61,7 @@ discard block |
||
61 | 61 | |
62 | 62 | private static function existRole($rules) |
63 | 63 | { |
64 | - if(empty(self::$validators[$rules]->getRules(self::$data['role']))){ |
|
64 | + if (empty(self::$validators[$rules]->getRules(self::$data['role']))) { |
|
65 | 65 | throw new Exception('Não existe regras para validar este formulário.'); |
66 | 66 | } |
67 | 67 | } |
@@ -86,8 +86,8 @@ discard block |
||
86 | 86 | |
87 | 87 | self::existRole(self::$model); |
88 | 88 | |
89 | - foreach ( (self::$validators[self::$model]->getRules($datas['role'])) as $key => $value) { |
|
90 | - if(@$value['required'] === true){ |
|
89 | + foreach ((self::$validators[self::$model]->getRules($datas['role'])) as $key => $value) { |
|
90 | + if (@$value['required']===true) { |
|
91 | 91 | self::$required[$key] = $value; |
92 | 92 | } |
93 | 93 | } |
@@ -103,27 +103,27 @@ discard block |
||
103 | 103 | |
104 | 104 | public static function check_errors(): bool |
105 | 105 | { |
106 | - return (count(self::$errors) === 0); |
|
106 | + return (count(self::$errors)===0); |
|
107 | 107 | } |
108 | 108 | |
109 | 109 | public static function validate() |
110 | 110 | { |
111 | - foreach ( (self::$validators[self::$model]->getRules(self::$data['role'])) as $key => $value) { |
|
111 | + foreach ((self::$validators[self::$model]->getRules(self::$data['role'])) as $key => $value) { |
|
112 | 112 | |
113 | 113 | foreach (json_decode(self::$data['data']) as $keyy => $valuee) { |
114 | 114 | |
115 | - if(!array_key_exists($keyy, (self::$validators[self::$model]->getRules(self::$data['role'])) )){ |
|
115 | + if (!array_key_exists($keyy, (self::$validators[self::$model]->getRules(self::$data['role'])))) { |
|
116 | 116 | throw new Exception("O campo '{$keyy}' não é esperado para está operação."); |
117 | 117 | } |
118 | 118 | |
119 | - if($keyy===$key){ |
|
119 | + if ($keyy===$key) { |
|
120 | 120 | |
121 | 121 | unset(self::$required[$key]); |
122 | 122 | |
123 | 123 | foreach ($value as $subkey => $subvalue) { |
124 | 124 | $function = "check_{$subkey}"; |
125 | 125 | self::testMethod($function); |
126 | - self::$function($keyy,$subvalue); |
|
126 | + self::$function($keyy, $subvalue); |
|
127 | 127 | } |
128 | 128 | } |
129 | 129 | } |
@@ -137,7 +137,7 @@ discard block |
||
137 | 137 | |
138 | 138 | public static function testMethod($method) |
139 | 139 | { |
140 | - if(!method_exists(static::class, $method)){ |
|
140 | + if (!method_exists(static::class, $method)) { |
|
141 | 141 | throw new Exception("{$method} não é uma validação válida."); |
142 | 142 | } |
143 | 143 | } |
@@ -151,14 +151,14 @@ discard block |
||
151 | 151 | |
152 | 152 | self::includeValidations(); |
153 | 153 | |
154 | - self::$model = get_class( self::getClass('HnrAzevedo\\Validator\\'.ucfirst($request['provider'])) ); |
|
154 | + self::$model = get_class(self::getClass('HnrAzevedo\\Validator\\'.ucfirst($request['provider']))); |
|
155 | 155 | |
156 | 156 | self::existRole(self::$model); |
157 | 157 | |
158 | - foreach ( self::$validators[self::$model]->getRules($request['role']) as $field => $r) { |
|
158 | + foreach (self::$validators[self::$model]->getRules($request['role']) as $field => $r) { |
|
159 | 159 | $response .= ("{$field}:".json_encode(array_reverse($r))).','; |
160 | 160 | } |
161 | 161 | |
162 | - return '{'.substr($response,0,-1).'}'; |
|
162 | + return '{'.substr($response, 0, -1).'}'; |
|
163 | 163 | } |
164 | 164 | } |