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