@@ -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(); |
@@ -79,23 +79,23 @@ discard block |
||
79 | 79 | |
80 | 80 | $rules = self::getClass('HnrAzevedo\\Validator\\'.ucfirst(self::$data['provider'])); |
81 | 81 | |
82 | - self::existRole($rules); |
|
82 | + self::existRole($rules); |
|
83 | 83 | |
84 | - $validators = self::$validators[get_class($rules)]->getRules($datas['role']); |
|
84 | + $validators = self::$validators[get_class($rules)]->getRules($datas['role']); |
|
85 | 85 | |
86 | 86 | $tests = 0; |
87 | 87 | |
88 | - foreach ($validators as $key => $value) { |
|
89 | - $tests = (@$value['required'] === true ) ? $tests+1 : $tests; |
|
88 | + foreach ($validators as $key => $value) { |
|
89 | + $tests = (@$value['required'] === true ) ? $tests+1 : $tests; |
|
90 | 90 | } |
91 | 91 | |
92 | - $testeds = self::validate($validators); |
|
92 | + $testeds = self::validate($validators); |
|
93 | 93 | |
94 | - if($tests > $testeds){ |
|
94 | + if($tests > $testeds){ |
|
95 | 95 | throw new Exception('Alguma informação necessária não pode ser validada.'); |
96 | 96 | } |
97 | 97 | |
98 | - return true; |
|
98 | + return true; |
|
99 | 99 | } |
100 | 100 | |
101 | 101 | public static function validate(array $validators): int |
@@ -103,31 +103,31 @@ discard block |
||
103 | 103 | $validate = 0; |
104 | 104 | foreach ($validators as $key => $value) { |
105 | 105 | |
106 | - foreach (json_decode(self::$data['data']) as $keyy => $valuee) { |
|
106 | + foreach (json_decode(self::$data['data']) as $keyy => $valuee) { |
|
107 | 107 | |
108 | 108 | $v = $valuee; |
109 | 109 | |
110 | - if(is_array($valuee)){ |
|
111 | - $v = null; |
|
112 | - foreach ($valuee as $vvv) { |
|
113 | - $v .= $vvv; |
|
114 | - } |
|
110 | + if(is_array($valuee)){ |
|
111 | + $v = null; |
|
112 | + foreach ($valuee as $vvv) { |
|
113 | + $v .= $vvv; |
|
114 | + } |
|
115 | 115 | } |
116 | 116 | |
117 | - $valuee = $v; |
|
117 | + $valuee = $v; |
|
118 | 118 | |
119 | - if(!array_key_exists($keyy, $validators)){ |
|
119 | + if(!array_key_exists($keyy, $validators)){ |
|
120 | 120 | throw new Exception("O campo '{$keyy}' não é esperado para está operação."); |
121 | 121 | } |
122 | 122 | |
123 | - if($keyy===$key){ |
|
123 | + if($keyy===$key){ |
|
124 | 124 | |
125 | 125 | $validate++; |
126 | 126 | |
127 | - foreach ($value as $subkey => $subvalue) { |
|
127 | + foreach ($value as $subkey => $subvalue) { |
|
128 | 128 | |
129 | - switch ($subkey) { |
|
130 | - case 'minlength': |
|
129 | + switch ($subkey) { |
|
130 | + case 'minlength': |
|
131 | 131 | if(array_key_exists('required', $value)){ |
132 | 132 | if($value['required'] or strlen($valuee)!==0){ |
133 | 133 | if(strlen($valuee)===0){ |
@@ -157,9 +157,9 @@ discard block |
||
157 | 157 | } |
158 | 158 | } |
159 | 159 | } |
160 | - break; |
|
160 | + break; |
|
161 | 161 | |
162 | - case 'maxlength': |
|
162 | + case 'maxlength': |
|
163 | 163 | if(array_key_exists('required', $value)){ |
164 | 164 | if($value['required'] or strlen($valuee)!==0){ |
165 | 165 | if(strlen($valuee)>(int)$subvalue){ |
@@ -169,7 +169,7 @@ discard block |
||
169 | 169 | } |
170 | 170 | break; |
171 | 171 | |
172 | - case 'regex': |
|
172 | + case 'regex': |
|
173 | 173 | if(array_key_exists('required', $value)){ |
174 | 174 | if($value['required'] or strlen($valuee)!==0){ |
175 | 175 | if(!@preg_match($subvalue,$valuee)){ |
@@ -179,7 +179,7 @@ discard block |
||
179 | 179 | } |
180 | 180 | break; |
181 | 181 | |
182 | - case 'equals': |
|
182 | + case 'equals': |
|
183 | 183 | $equals = false; |
184 | 184 | foreach (self::$data as $ke => $sub) { |
185 | 185 | if($ke===$subvalue){ |
@@ -193,10 +193,10 @@ discard block |
||
193 | 193 | throw new Exception("O servidor não encontrou a informação '{$subvalue}' para ser comparada a '{$key}'.",1); |
194 | 194 | } |
195 | 195 | break; |
196 | - } |
|
197 | - } |
|
198 | - } |
|
199 | - } |
|
196 | + } |
|
197 | + } |
|
198 | + } |
|
199 | + } |
|
200 | 200 | } |
201 | 201 | return $validate; |
202 | 202 | } |
@@ -214,7 +214,7 @@ discard block |
||
214 | 214 | |
215 | 215 | self::existRole($rules); |
216 | 216 | |
217 | - foreach ( self::$validators[get_class($rules)]->getRules($request['role']) as $field => $r) { |
|
217 | + foreach ( self::$validators[get_class($rules)]->getRules($request['role']) as $field => $r) { |
|
218 | 218 | |
219 | 219 | $response .= ("{$field}:".json_encode(array_reverse($r))).','; |
220 | 220 | |
@@ -225,6 +225,6 @@ discard block |
||
225 | 225 | $response = str_replace('{"','',$response); |
226 | 226 | $response = str_replace('":',':',$response); |
227 | 227 | |
228 | - return $response; |
|
229 | - } |
|
228 | + return $response; |
|
229 | + } |
|
230 | 230 | } |
@@ -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 | 11 | private static array $data = []; |
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 | } |
@@ -86,12 +86,12 @@ discard block |
||
86 | 86 | $tests = 0; |
87 | 87 | |
88 | 88 | foreach ($validators as $key => $value) { |
89 | - $tests = (@$value['required'] === true ) ? $tests+1 : $tests; |
|
89 | + $tests = (@$value['required']===true) ? $tests+1 : $tests; |
|
90 | 90 | } |
91 | 91 | |
92 | 92 | $testeds = self::validate($validators); |
93 | 93 | |
94 | - if($tests > $testeds){ |
|
94 | + if ($tests>$testeds) { |
|
95 | 95 | throw new Exception('Alguma informação necessária não pode ser validada.'); |
96 | 96 | } |
97 | 97 | |
@@ -107,7 +107,7 @@ discard block |
||
107 | 107 | |
108 | 108 | $v = $valuee; |
109 | 109 | |
110 | - if(is_array($valuee)){ |
|
110 | + if (is_array($valuee)) { |
|
111 | 111 | $v = null; |
112 | 112 | foreach ($valuee as $vvv) { |
113 | 113 | $v .= $vvv; |
@@ -116,11 +116,11 @@ discard block |
||
116 | 116 | |
117 | 117 | $valuee = $v; |
118 | 118 | |
119 | - if(!array_key_exists($keyy, $validators)){ |
|
119 | + if (!array_key_exists($keyy, $validators)) { |
|
120 | 120 | throw new Exception("O campo '{$keyy}' não é esperado para está operação."); |
121 | 121 | } |
122 | 122 | |
123 | - if($keyy===$key){ |
|
123 | + if ($keyy===$key) { |
|
124 | 124 | |
125 | 125 | $validate++; |
126 | 126 | |
@@ -128,30 +128,30 @@ discard block |
||
128 | 128 | |
129 | 129 | switch ($subkey) { |
130 | 130 | case 'minlength': |
131 | - if(array_key_exists('required', $value)){ |
|
132 | - if($value['required'] or strlen($valuee)!==0){ |
|
133 | - if(strlen($valuee)===0){ |
|
134 | - throw new Exception("O campo '{$key}' é obrigatório.",1); |
|
131 | + if (array_key_exists('required', $value)) { |
|
132 | + if ($value['required'] or strlen($valuee)!==0) { |
|
133 | + if (strlen($valuee)===0) { |
|
134 | + throw new Exception("O campo '{$key}' é obrigatório.", 1); |
|
135 | 135 | } |
136 | 136 | |
137 | - if(strlen($valuee) < (int) $subvalue){ |
|
138 | - throw new Exception("{$key} não atingiu o mínimo de caracteres esperado.",1); |
|
137 | + if (strlen($valuee)<(int) $subvalue) { |
|
138 | + throw new Exception("{$key} não atingiu o mínimo de caracteres esperado.", 1); |
|
139 | 139 | } |
140 | 140 | } |
141 | 141 | } |
142 | 142 | break; |
143 | 143 | |
144 | 144 | case 'type': |
145 | - if(array_key_exists('required', $value)){ |
|
146 | - if($value['required'] or strlen($valuee)!==0){ |
|
145 | + if (array_key_exists('required', $value)) { |
|
146 | + if ($value['required'] or strlen($valuee)!==0) { |
|
147 | 147 | switch ($subvalue) { |
148 | 148 | case 'date': |
149 | 149 | $date = explode('/', $valuee); |
150 | - if(count($date) != 3){ |
|
151 | - throw new Exception('Data inválida.',1); |
|
150 | + if (count($date)!=3) { |
|
151 | + throw new Exception('Data inválida.', 1); |
|
152 | 152 | } |
153 | - if(! checkdate( intval($date[1]), intval($date[0]), intval($date[2]) )){ |
|
154 | - throw new Exception('Data inválida.',1); |
|
153 | + if (!checkdate(intval($date[1]), intval($date[0]), intval($date[2]))) { |
|
154 | + throw new Exception('Data inválida.', 1); |
|
155 | 155 | } |
156 | 156 | break; |
157 | 157 | } |
@@ -160,20 +160,20 @@ discard block |
||
160 | 160 | break; |
161 | 161 | |
162 | 162 | case 'maxlength': |
163 | - if(array_key_exists('required', $value)){ |
|
164 | - if($value['required'] or strlen($valuee)!==0){ |
|
165 | - if(strlen($valuee)>(int)$subvalue){ |
|
166 | - throw new Exception("{$key} ultrapassou o limite de caracteres permitidos.",1); |
|
163 | + if (array_key_exists('required', $value)) { |
|
164 | + if ($value['required'] or strlen($valuee)!==0) { |
|
165 | + if (strlen($valuee)>(int) $subvalue) { |
|
166 | + throw new Exception("{$key} ultrapassou o limite de caracteres permitidos.", 1); |
|
167 | 167 | } |
168 | 168 | } |
169 | 169 | } |
170 | 170 | break; |
171 | 171 | |
172 | 172 | case 'regex': |
173 | - if(array_key_exists('required', $value)){ |
|
174 | - if($value['required'] or strlen($valuee)!==0){ |
|
175 | - if(!@preg_match($subvalue,$valuee)){ |
|
176 | - throw new Exception("{$key} inválido(a).",1); |
|
173 | + if (array_key_exists('required', $value)) { |
|
174 | + if ($value['required'] or strlen($valuee)!==0) { |
|
175 | + if (!@preg_match($subvalue, $valuee)) { |
|
176 | + throw new Exception("{$key} inválido(a).", 1); |
|
177 | 177 | } |
178 | 178 | } |
179 | 179 | } |
@@ -182,15 +182,15 @@ discard block |
||
182 | 182 | case 'equals': |
183 | 183 | $equals = false; |
184 | 184 | foreach (self::$data as $ke => $sub) { |
185 | - if($ke===$subvalue){ |
|
186 | - $equals=true; |
|
187 | - if($valuee !== $sub){ |
|
188 | - throw new Exception(ucfirst($key).' está diferente de '.ucfirst($ke),1); |
|
185 | + if ($ke===$subvalue) { |
|
186 | + $equals = true; |
|
187 | + if ($valuee!==$sub) { |
|
188 | + throw new Exception(ucfirst($key).' está diferente de '.ucfirst($ke), 1); |
|
189 | 189 | } |
190 | 190 | } |
191 | 191 | } |
192 | - if(!$equals){ |
|
193 | - throw new Exception("O servidor não encontrou a informação '{$subvalue}' para ser comparada a '{$key}'.",1); |
|
192 | + if (!$equals) { |
|
193 | + throw new Exception("O servidor não encontrou a informação '{$subvalue}' para ser comparada a '{$key}'.", 1); |
|
194 | 194 | } |
195 | 195 | break; |
196 | 196 | } |
@@ -214,16 +214,16 @@ discard block |
||
214 | 214 | |
215 | 215 | self::existRole($rules); |
216 | 216 | |
217 | - foreach ( self::$validators[get_class($rules)]->getRules($request['role']) as $field => $r) { |
|
217 | + foreach (self::$validators[get_class($rules)]->getRules($request['role']) as $field => $r) { |
|
218 | 218 | |
219 | 219 | $response .= ("{$field}:".json_encode(array_reverse($r))).','; |
220 | 220 | |
221 | 221 | } |
222 | 222 | |
223 | - $response = '{'.substr($response,0,-1).'}'; |
|
224 | - $response = str_replace(',"',',',$response); |
|
225 | - $response = str_replace('{"','',$response); |
|
226 | - $response = str_replace('":',':',$response); |
|
223 | + $response = '{'.substr($response, 0, -1).'}'; |
|
224 | + $response = str_replace(',"', ',', $response); |
|
225 | + $response = str_replace('{"', '', $response); |
|
226 | + $response = str_replace('":', ':', $response); |
|
227 | 227 | |
228 | 228 | return $response; |
229 | 229 | } |