@@ -70,7 +70,7 @@ discard block |
||
70 | 70 | { |
71 | 71 | self::$data = $datas; |
72 | 72 | |
73 | - self::existData(); |
|
73 | + self::existData(); |
|
74 | 74 | self::jsonData(); |
75 | 75 | self::hasProvider(); |
76 | 76 | self::hasRole(); |
@@ -81,45 +81,45 @@ discard block |
||
81 | 81 | |
82 | 82 | $rules = self::getClass('HnrAzevedo\\Validator\\'.ucfirst(self::$data['provider'])); |
83 | 83 | |
84 | - self::existRole($rules); |
|
84 | + self::existRole($rules); |
|
85 | 85 | |
86 | - $validators = self::$validators[get_class($rules)]->getRules($datas['role']); |
|
86 | + $validators = self::$validators[get_class($rules)]->getRules($datas['role']); |
|
87 | 87 | |
88 | 88 | $tests = 0; |
89 | 89 | |
90 | - foreach ($validators as $key => $value) { |
|
91 | - $tests = (array_key_exists('required',$value) and $value['required']===true) ? $tests+1 : $tests; |
|
92 | - } |
|
90 | + foreach ($validators as $key => $value) { |
|
91 | + $tests = (array_key_exists('required',$value) and $value['required']===true) ? $tests+1 : $tests; |
|
92 | + } |
|
93 | 93 | |
94 | - $testeds = 0; |
|
94 | + $testeds = 0; |
|
95 | 95 | |
96 | - foreach ($validators as $key => $value) { |
|
96 | + foreach ($validators as $key => $value) { |
|
97 | 97 | |
98 | - foreach ($data as $keyy => $valuee) { |
|
98 | + foreach ($data as $keyy => $valuee) { |
|
99 | 99 | |
100 | 100 | $v = $valuee; |
101 | 101 | |
102 | - if(is_array($valuee)){ |
|
103 | - $v = null; |
|
104 | - foreach ($valuee as $vvv) { |
|
105 | - $v .= $vvv; |
|
106 | - } |
|
102 | + if(is_array($valuee)){ |
|
103 | + $v = null; |
|
104 | + foreach ($valuee as $vvv) { |
|
105 | + $v .= $vvv; |
|
106 | + } |
|
107 | 107 | } |
108 | 108 | |
109 | - $valuee = $v; |
|
109 | + $valuee = $v; |
|
110 | 110 | |
111 | - if(!array_key_exists($keyy, $validators)){ |
|
111 | + if(!array_key_exists($keyy, $validators)){ |
|
112 | 112 | throw new Exception("O campo '{$keyy}' não é esperado para está operação."); |
113 | 113 | } |
114 | 114 | |
115 | - if($keyy===$key){ |
|
115 | + if($keyy===$key){ |
|
116 | 116 | |
117 | 117 | $testeds++; |
118 | 118 | |
119 | - foreach ($value as $subkey => $subvalue) { |
|
119 | + foreach ($value as $subkey => $subvalue) { |
|
120 | 120 | |
121 | - switch ($subkey) { |
|
122 | - case 'minlength': |
|
121 | + switch ($subkey) { |
|
122 | + case 'minlength': |
|
123 | 123 | if(array_key_exists('required', $value)){ |
124 | 124 | if($value['required'] or strlen($valuee)!==0){ |
125 | 125 | if(strlen($valuee)===0){ |
@@ -149,9 +149,9 @@ discard block |
||
149 | 149 | } |
150 | 150 | } |
151 | 151 | } |
152 | - break; |
|
152 | + break; |
|
153 | 153 | |
154 | - case 'maxlength': |
|
154 | + case 'maxlength': |
|
155 | 155 | if(array_key_exists('required', $value)){ |
156 | 156 | if($value['required'] or strlen($valuee)!==0){ |
157 | 157 | if(strlen($valuee)>(int)$subvalue){ |
@@ -161,7 +161,7 @@ discard block |
||
161 | 161 | } |
162 | 162 | break; |
163 | 163 | |
164 | - case 'regex': |
|
164 | + case 'regex': |
|
165 | 165 | if(array_key_exists('required', $value)){ |
166 | 166 | if($value['required'] or strlen($valuee)!==0){ |
167 | 167 | if(!@preg_match($subvalue,$valuee)){ |
@@ -171,7 +171,7 @@ discard block |
||
171 | 171 | } |
172 | 172 | break; |
173 | 173 | |
174 | - case 'equals': |
|
174 | + case 'equals': |
|
175 | 175 | $equals = false; |
176 | 176 | foreach ($data as $ke => $sub) { |
177 | 177 | if($ke===$subvalue){ |
@@ -184,18 +184,18 @@ discard block |
||
184 | 184 | throw new Exception("O servidor não encontrou a informação '{$subvalue}' para ser comparada a '{$key}'.",1); |
185 | 185 | } |
186 | 186 | break; |
187 | - } |
|
188 | - } |
|
189 | - } |
|
190 | - } |
|
187 | + } |
|
188 | + } |
|
189 | + } |
|
190 | + } |
|
191 | 191 | } |
192 | 192 | |
193 | - if($tests > $testeds){ |
|
193 | + if($tests > $testeds){ |
|
194 | 194 | throw new Exception('Alguma informação necessária não pode ser validada.'); |
195 | 195 | } |
196 | 196 | |
197 | - return true; |
|
198 | - } |
|
197 | + return true; |
|
198 | + } |
|
199 | 199 | |
200 | 200 | public static function toJson(array $request): string |
201 | 201 | { |
@@ -205,29 +205,29 @@ discard block |
||
205 | 205 | |
206 | 206 | $rules = self::getClass('HnrAzevedo\\Validator\\'.ucfirst($request['provider'])); |
207 | 207 | |
208 | - self::existRole($rules); |
|
208 | + self::existRole($rules); |
|
209 | 209 | |
210 | 210 | /* For function to validate information in javascript */ |
211 | 211 | $response = '{'; |
212 | 212 | |
213 | - foreach ( self::$validators[get_class($rules)]->getRules($request['role']) as $field => $r) { |
|
213 | + foreach ( self::$validators[get_class($rules)]->getRules($request['role']) as $field => $r) { |
|
214 | 214 | $response .= $field.':{'; |
215 | 215 | |
216 | - foreach(array_reverse($r) as $rule => $value){ |
|
216 | + foreach(array_reverse($r) as $rule => $value){ |
|
217 | 217 | $value = (gettype($value)==='string') ? '\''.$value.'\'' : $value; |
218 | 218 | |
219 | - if(gettype($value)==='boolean'){ |
|
219 | + if(gettype($value)==='boolean'){ |
|
220 | 220 | $value = ($value) ? 'true' : 'false'; |
221 | 221 | } |
222 | 222 | |
223 | 223 | $value = ($rule=='regex') ? str_replace('\\','\\\\','\''.substr($value,2,strlen($value)-4).'\'') : $value; |
224 | 224 | |
225 | - $response .= $rule.':'.$value.','; |
|
225 | + $response .= $rule.':'.$value.','; |
|
226 | 226 | } |
227 | 227 | |
228 | - $response .='},'; |
|
228 | + $response .='},'; |
|
229 | 229 | } |
230 | 230 | |
231 | - return substr(str_replace(',}','}',$response),0,-1).'}'; |
|
232 | - } |
|
231 | + return substr(str_replace(',}','}',$response),0,-1).'}'; |
|
232 | + } |
|
233 | 233 | } |
@@ -5,54 +5,54 @@ discard block |
||
5 | 5 | use HnrAzevedo\Validator\Rules; |
6 | 6 | use Exception; |
7 | 7 | |
8 | -Class Validator{ |
|
8 | +Class Validator { |
|
9 | 9 | |
10 | 10 | private static array $validators = array(); |
11 | - private static ?array $data = null; |
|
11 | + private static ? array $data = null; |
|
12 | 12 | |
13 | - public static function add(object $model,callable $return): void |
|
13 | + public static function add(object $model, callable $return): void |
|
14 | 14 | { |
15 | 15 | self::$validators[get_class($model)] = $return($Rules = new Rules($model)); |
16 | 16 | } |
17 | 17 | |
18 | 18 | private static function existData() |
19 | 19 | { |
20 | - if(!array_key_exists('data', self::$data)){ |
|
20 | + if (!array_key_exists('data', self::$data)) { |
|
21 | 21 | throw new Exception('Informações cruciais não foram recebidas.'); |
22 | 22 | } |
23 | 23 | } |
24 | 24 | |
25 | 25 | private static function jsonData() |
26 | 26 | { |
27 | - if(json_decode(self::$data['data']) === null){ |
|
27 | + if (json_decode(self::$data['data'])===null) { |
|
28 | 28 | throw new Exception('O servidor recebeu as informações no formato esperado.'); |
29 | 29 | } |
30 | 30 | } |
31 | 31 | |
32 | 32 | private static function hasProvider() |
33 | 33 | { |
34 | - if(!array_key_exists('provider',self::$data)){ |
|
34 | + if (!array_key_exists('provider', self::$data)) { |
|
35 | 35 | throw new Exception('O servidor não recebeu o ID do formulário.'); |
36 | 36 | } |
37 | 37 | } |
38 | 38 | |
39 | 39 | private static function hasRole() |
40 | 40 | { |
41 | - if(!array_key_exists('role',self::$data)){ |
|
41 | + if (!array_key_exists('role', self::$data)) { |
|
42 | 42 | throw new Exception('O servidor não conseguiu identificar a finalidade deste formulário.'); |
43 | 43 | } |
44 | 44 | } |
45 | 45 | |
46 | 46 | private static function includeValidations() |
47 | 47 | { |
48 | - if( file_exists(VALIDATOR_CONFIG['path'] . ucfirst(self::$data['provider']) . '.php') ){ |
|
49 | - require_once(VALIDATOR_CONFIG['path'] . ucfirst(self::$data['provider']) . '.php'); |
|
48 | + if (file_exists(VALIDATOR_CONFIG['path'].ucfirst(self::$data['provider']).'.php')) { |
|
49 | + require_once(VALIDATOR_CONFIG['path'].ucfirst(self::$data['provider']).'.php'); |
|
50 | 50 | } |
51 | 51 | } |
52 | 52 | |
53 | 53 | private static function getClass(string $class) |
54 | 54 | { |
55 | - if(!class_exists($class)){ |
|
55 | + if (!class_exists($class)) { |
|
56 | 56 | throw new Exception("Form ID {$class} inválido."); |
57 | 57 | } |
58 | 58 | |
@@ -61,7 +61,7 @@ discard block |
||
61 | 61 | |
62 | 62 | private static function existRole($rules) |
63 | 63 | { |
64 | - if(empty(self::$validators[get_class($rules)]->getRules(self::$data['role']))){ |
|
64 | + if (empty(self::$validators[get_class($rules)]->getRules(self::$data['role']))) { |
|
65 | 65 | throw new Exception('Não existe regras para validar este formulário.'); |
66 | 66 | } |
67 | 67 | } |
@@ -88,7 +88,7 @@ discard block |
||
88 | 88 | $tests = 0; |
89 | 89 | |
90 | 90 | foreach ($validators as $key => $value) { |
91 | - $tests = (array_key_exists('required',$value) and $value['required']===true) ? $tests+1 : $tests; |
|
91 | + $tests = (array_key_exists('required', $value) and $value['required']===true) ? $tests+1 : $tests; |
|
92 | 92 | } |
93 | 93 | |
94 | 94 | $testeds = 0; |
@@ -99,7 +99,7 @@ discard block |
||
99 | 99 | |
100 | 100 | $v = $valuee; |
101 | 101 | |
102 | - if(is_array($valuee)){ |
|
102 | + if (is_array($valuee)) { |
|
103 | 103 | $v = null; |
104 | 104 | foreach ($valuee as $vvv) { |
105 | 105 | $v .= $vvv; |
@@ -108,11 +108,11 @@ discard block |
||
108 | 108 | |
109 | 109 | $valuee = $v; |
110 | 110 | |
111 | - if(!array_key_exists($keyy, $validators)){ |
|
111 | + if (!array_key_exists($keyy, $validators)) { |
|
112 | 112 | throw new Exception("O campo '{$keyy}' não é esperado para está operação."); |
113 | 113 | } |
114 | 114 | |
115 | - if($keyy===$key){ |
|
115 | + if ($keyy===$key) { |
|
116 | 116 | |
117 | 117 | $testeds++; |
118 | 118 | |
@@ -120,30 +120,30 @@ discard block |
||
120 | 120 | |
121 | 121 | switch ($subkey) { |
122 | 122 | case 'minlength': |
123 | - if(array_key_exists('required', $value)){ |
|
124 | - if($value['required'] or strlen($valuee)!==0){ |
|
125 | - if(strlen($valuee)===0){ |
|
126 | - throw new Exception("O campo '{$key}' é obrigatório.",1); |
|
123 | + if (array_key_exists('required', $value)) { |
|
124 | + if ($value['required'] or strlen($valuee)!==0) { |
|
125 | + if (strlen($valuee)===0) { |
|
126 | + throw new Exception("O campo '{$key}' é obrigatório.", 1); |
|
127 | 127 | } |
128 | 128 | |
129 | - if(strlen($valuee) < (int) $subvalue){ |
|
130 | - throw new Exception("{$key} não atingiu o mínimo de caracteres esperado.",1); |
|
129 | + if (strlen($valuee)<(int) $subvalue) { |
|
130 | + throw new Exception("{$key} não atingiu o mínimo de caracteres esperado.", 1); |
|
131 | 131 | } |
132 | 132 | } |
133 | 133 | } |
134 | 134 | break; |
135 | 135 | |
136 | 136 | case 'type': |
137 | - if(array_key_exists('required', $value)){ |
|
138 | - if($value['required'] or strlen($valuee)!==0){ |
|
137 | + if (array_key_exists('required', $value)) { |
|
138 | + if ($value['required'] or strlen($valuee)!==0) { |
|
139 | 139 | switch ($subvalue) { |
140 | 140 | case 'date': |
141 | 141 | $date = explode('/', $valuee); |
142 | - if(count($date) != 3){ |
|
143 | - throw new Exception('Data inválida.',1); |
|
142 | + if (count($date)!=3) { |
|
143 | + throw new Exception('Data inválida.', 1); |
|
144 | 144 | } |
145 | - if(!@checkdate(intval($date[1]), intval($date[0]), intval($date[2]) )){ |
|
146 | - throw new Exception('Data inválida.',1); |
|
145 | + if (!@checkdate(intval($date[1]), intval($date[0]), intval($date[2]))) { |
|
146 | + throw new Exception('Data inválida.', 1); |
|
147 | 147 | } |
148 | 148 | break; |
149 | 149 | } |
@@ -152,20 +152,20 @@ discard block |
||
152 | 152 | break; |
153 | 153 | |
154 | 154 | case 'maxlength': |
155 | - if(array_key_exists('required', $value)){ |
|
156 | - if($value['required'] or strlen($valuee)!==0){ |
|
157 | - if(strlen($valuee)>(int)$subvalue){ |
|
158 | - throw new Exception("{$key} ultrapassou o limite de caracteres permitidos.",1); |
|
155 | + if (array_key_exists('required', $value)) { |
|
156 | + if ($value['required'] or strlen($valuee)!==0) { |
|
157 | + if (strlen($valuee)>(int) $subvalue) { |
|
158 | + throw new Exception("{$key} ultrapassou o limite de caracteres permitidos.", 1); |
|
159 | 159 | } |
160 | 160 | } |
161 | 161 | } |
162 | 162 | break; |
163 | 163 | |
164 | 164 | case 'regex': |
165 | - if(array_key_exists('required', $value)){ |
|
166 | - if($value['required'] or strlen($valuee)!==0){ |
|
167 | - if(!@preg_match($subvalue,$valuee)){ |
|
168 | - throw new Exception("{$key} inválido(a).",1); |
|
165 | + if (array_key_exists('required', $value)) { |
|
166 | + if ($value['required'] or strlen($valuee)!==0) { |
|
167 | + if (!@preg_match($subvalue, $valuee)) { |
|
168 | + throw new Exception("{$key} inválido(a).", 1); |
|
169 | 169 | } |
170 | 170 | } |
171 | 171 | } |
@@ -174,14 +174,14 @@ discard block |
||
174 | 174 | case 'equals': |
175 | 175 | $equals = false; |
176 | 176 | foreach ($data as $ke => $sub) { |
177 | - if($ke===$subvalue){ |
|
178 | - $equals=true; |
|
179 | - if($valuee !== $sub) |
|
180 | - throw new \Exception(ucfirst($key).' está diferente de '.ucfirst($ke),1); |
|
177 | + if ($ke===$subvalue) { |
|
178 | + $equals = true; |
|
179 | + if ($valuee!==$sub) |
|
180 | + throw new \Exception(ucfirst($key).' está diferente de '.ucfirst($ke), 1); |
|
181 | 181 | } |
182 | 182 | } |
183 | - if(!$equals){ |
|
184 | - throw new Exception("O servidor não encontrou a informação '{$subvalue}' para ser comparada a '{$key}'.",1); |
|
183 | + if (!$equals) { |
|
184 | + throw new Exception("O servidor não encontrou a informação '{$subvalue}' para ser comparada a '{$key}'.", 1); |
|
185 | 185 | } |
186 | 186 | break; |
187 | 187 | } |
@@ -190,7 +190,7 @@ discard block |
||
190 | 190 | } |
191 | 191 | } |
192 | 192 | |
193 | - if($tests > $testeds){ |
|
193 | + if ($tests>$testeds) { |
|
194 | 194 | throw new Exception('Alguma informação necessária não pode ser validada.'); |
195 | 195 | } |
196 | 196 | |
@@ -210,24 +210,24 @@ discard block |
||
210 | 210 | /* For function to validate information in javascript */ |
211 | 211 | $response = '{'; |
212 | 212 | |
213 | - foreach ( self::$validators[get_class($rules)]->getRules($request['role']) as $field => $r) { |
|
213 | + foreach (self::$validators[get_class($rules)]->getRules($request['role']) as $field => $r) { |
|
214 | 214 | $response .= $field.':{'; |
215 | 215 | |
216 | - foreach(array_reverse($r) as $rule => $value){ |
|
216 | + foreach (array_reverse($r) as $rule => $value) { |
|
217 | 217 | $value = (gettype($value)==='string') ? '\''.$value.'\'' : $value; |
218 | 218 | |
219 | - if(gettype($value)==='boolean'){ |
|
219 | + if (gettype($value)==='boolean') { |
|
220 | 220 | $value = ($value) ? 'true' : 'false'; |
221 | 221 | } |
222 | 222 | |
223 | - $value = ($rule=='regex') ? str_replace('\\','\\\\','\''.substr($value,2,strlen($value)-4).'\'') : $value; |
|
223 | + $value = ($rule=='regex') ? str_replace('\\', '\\\\', '\''.substr($value, 2, strlen($value)-4).'\'') : $value; |
|
224 | 224 | |
225 | 225 | $response .= $rule.':'.$value.','; |
226 | 226 | } |
227 | 227 | |
228 | - $response .='},'; |
|
228 | + $response .= '},'; |
|
229 | 229 | } |
230 | 230 | |
231 | - return substr(str_replace(',}','}',$response),0,-1).'}'; |
|
231 | + return substr(str_replace(',}', '}', $response), 0, -1).'}'; |
|
232 | 232 | } |
233 | 233 | } |
@@ -176,8 +176,9 @@ |
||
176 | 176 | foreach ($data as $ke => $sub) { |
177 | 177 | if($ke===$subvalue){ |
178 | 178 | $equals=true; |
179 | - if($valuee !== $sub) |
|
180 | - throw new \Exception(ucfirst($key).' está diferente de '.ucfirst($ke),1); |
|
179 | + if($valuee !== $sub) { |
|
180 | + throw new \Exception(ucfirst($key).' está diferente de '.ucfirst($ke),1); |
|
181 | + } |
|
181 | 182 | } |
182 | 183 | } |
183 | 184 | if(!$equals){ |