@@ -62,7 +62,7 @@ discard block |
||
| 62 | 62 | |
| 63 | 63 | public static function checkDatas() |
| 64 | 64 | { |
| 65 | - self::existData(); |
|
| 65 | + self::existData(); |
|
| 66 | 66 | self::jsonData(); |
| 67 | 67 | self::hasProvider(); |
| 68 | 68 | self::hasRole(); |
@@ -78,9 +78,9 @@ discard block |
||
| 78 | 78 | |
| 79 | 79 | self::$model = self::getClass($model); |
| 80 | 80 | |
| 81 | - self::existRole(self::$model); |
|
| 81 | + self::existRole(self::$model); |
|
| 82 | 82 | |
| 83 | - foreach ( (self::$validators[self::$model]->getRules($datas['role'])) as $key => $value) { |
|
| 83 | + foreach ( (self::$validators[self::$model]->getRules($datas['role'])) as $key => $value) { |
|
| 84 | 84 | if(@$value['required'] === true){ |
| 85 | 85 | self::$required[$key] = $value; |
| 86 | 86 | } |
@@ -92,7 +92,7 @@ discard block |
||
| 92 | 92 | |
| 93 | 93 | self::checkRequireds(); |
| 94 | 94 | |
| 95 | - return self::checkErrors(); |
|
| 95 | + return self::checkErrors(); |
|
| 96 | 96 | } |
| 97 | 97 | |
| 98 | 98 | public static function checkErrors(): bool |
@@ -104,23 +104,23 @@ discard block |
||
| 104 | 104 | { |
| 105 | 105 | foreach ( (self::$validators[self::$model]->getRules(self::$data['role'])) as $key => $value) { |
| 106 | 106 | |
| 107 | - foreach (json_decode(self::$data['data']) as $keyy => $valuee) { |
|
| 107 | + foreach (json_decode(self::$data['data']) as $keyy => $valuee) { |
|
| 108 | 108 | |
| 109 | - if(!array_key_exists($keyy, (self::$validators[self::$model]->getRules(self::$data['role'])) )){ |
|
| 109 | + if(!array_key_exists($keyy, (self::$validators[self::$model]->getRules(self::$data['role'])) )){ |
|
| 110 | 110 | throw new Exception("O campo '{$keyy}' não é esperado para está operação."); |
| 111 | 111 | } |
| 112 | 112 | |
| 113 | - if($keyy===$key){ |
|
| 113 | + if($keyy===$key){ |
|
| 114 | 114 | |
| 115 | 115 | unset(self::$required[$key]); |
| 116 | 116 | |
| 117 | - foreach ($value as $subkey => $subvalue) { |
|
| 117 | + foreach ($value as $subkey => $subvalue) { |
|
| 118 | 118 | $function = "check".ucfirst($subkey); |
| 119 | 119 | self::testMethod($function); |
| 120 | 120 | self::$function($keyy,$subvalue); |
| 121 | - } |
|
| 122 | - } |
|
| 123 | - } |
|
| 121 | + } |
|
| 122 | + } |
|
| 123 | + } |
|
| 124 | 124 | } |
| 125 | 125 | } |
| 126 | 126 | |
@@ -149,7 +149,7 @@ discard block |
||
| 149 | 149 | |
| 150 | 150 | self::existRole(self::$model); |
| 151 | 151 | |
| 152 | - foreach ( self::$validators[self::$model]->getRules($request['role']) as $field => $r) { |
|
| 152 | + foreach ( self::$validators[self::$model]->getRules($request['role']) as $field => $r) { |
|
| 153 | 153 | $r = self::replaceRegex($r); |
| 154 | 154 | $response .= ("{$field}:".json_encode(array_reverse($r))).','; |
| 155 | 155 | } |
@@ -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,35 +16,35 @@ 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 getClass(string $class) |
| 46 | 46 | { |
| 47 | - if(!class_exists($class)){ |
|
| 47 | + if (!class_exists($class)) { |
|
| 48 | 48 | throw new Exception("Form ID {$class} inválido."); |
| 49 | 49 | } |
| 50 | 50 | |
@@ -55,7 +55,7 @@ discard block |
||
| 55 | 55 | |
| 56 | 56 | private static function existRole($rules) |
| 57 | 57 | { |
| 58 | - if(empty(self::$validators[$rules]->getRules(self::$data['role']))){ |
|
| 58 | + if (empty(self::$validators[$rules]->getRules(self::$data['role']))) { |
|
| 59 | 59 | throw new Exception('Não existe regras para validar este formulário.'); |
| 60 | 60 | } |
| 61 | 61 | } |
@@ -80,8 +80,8 @@ discard block |
||
| 80 | 80 | |
| 81 | 81 | self::existRole(self::$model); |
| 82 | 82 | |
| 83 | - foreach ( (self::$validators[self::$model]->getRules($datas['role'])) as $key => $value) { |
|
| 84 | - if(@$value['required'] === true){ |
|
| 83 | + foreach ((self::$validators[self::$model]->getRules($datas['role'])) as $key => $value) { |
|
| 84 | + if (@$value['required']===true) { |
|
| 85 | 85 | self::$required[$key] = $value; |
| 86 | 86 | } |
| 87 | 87 | } |
@@ -97,27 +97,27 @@ discard block |
||
| 97 | 97 | |
| 98 | 98 | public static function checkErrors(): bool |
| 99 | 99 | { |
| 100 | - return (count(self::$errors) === 0); |
|
| 100 | + return (count(self::$errors)===0); |
|
| 101 | 101 | } |
| 102 | 102 | |
| 103 | 103 | public static function validate() |
| 104 | 104 | { |
| 105 | - foreach ( (self::$validators[self::$model]->getRules(self::$data['role'])) as $key => $value) { |
|
| 105 | + foreach ((self::$validators[self::$model]->getRules(self::$data['role'])) as $key => $value) { |
|
| 106 | 106 | |
| 107 | 107 | foreach (json_decode(self::$data['data']) as $keyy => $valuee) { |
| 108 | 108 | |
| 109 | - if(!array_key_exists($keyy, (self::$validators[self::$model]->getRules(self::$data['role'])) )){ |
|
| 109 | + if (!array_key_exists($keyy, (self::$validators[self::$model]->getRules(self::$data['role'])))) { |
|
| 110 | 110 | throw new Exception("O campo '{$keyy}' não é esperado para está operação."); |
| 111 | 111 | } |
| 112 | 112 | |
| 113 | - if($keyy===$key){ |
|
| 113 | + if ($keyy===$key) { |
|
| 114 | 114 | |
| 115 | 115 | unset(self::$required[$key]); |
| 116 | 116 | |
| 117 | 117 | foreach ($value as $subkey => $subvalue) { |
| 118 | 118 | $function = "check".ucfirst($subkey); |
| 119 | 119 | self::testMethod($function); |
| 120 | - self::$function($keyy,$subvalue); |
|
| 120 | + self::$function($keyy, $subvalue); |
|
| 121 | 121 | } |
| 122 | 122 | } |
| 123 | 123 | } |
@@ -131,7 +131,7 @@ discard block |
||
| 131 | 131 | |
| 132 | 132 | public static function testMethod($method) |
| 133 | 133 | { |
| 134 | - if(!method_exists(static::class, $method)){ |
|
| 134 | + if (!method_exists(static::class, $method)) { |
|
| 135 | 135 | throw new Exception("{$method} não é uma validação válida."); |
| 136 | 136 | } |
| 137 | 137 | } |
@@ -149,18 +149,18 @@ discard block |
||
| 149 | 149 | |
| 150 | 150 | self::existRole(self::$model); |
| 151 | 151 | |
| 152 | - foreach ( self::$validators[self::$model]->getRules($request['role']) as $field => $r) { |
|
| 152 | + foreach (self::$validators[self::$model]->getRules($request['role']) as $field => $r) { |
|
| 153 | 153 | $r = self::replaceRegex($r); |
| 154 | 154 | $response .= ("{$field}:".json_encode(array_reverse($r))).','; |
| 155 | 155 | } |
| 156 | 156 | |
| 157 | - return '{'.substr($response,0,-1).'}'; |
|
| 157 | + return '{'.substr($response, 0, -1).'}'; |
|
| 158 | 158 | } |
| 159 | 159 | |
| 160 | 160 | private static function replaceRegex(array $rules): array |
| 161 | 161 | { |
| 162 | - if(array_key_exists('regex',$rules)){ |
|
| 163 | - $rules['regex'] = substr($rules['regex'],1,-2); |
|
| 162 | + if (array_key_exists('regex', $rules)) { |
|
| 163 | + $rules['regex'] = substr($rules['regex'], 1, -2); |
|
| 164 | 164 | } |
| 165 | 165 | return $rules; |
| 166 | 166 | } |
@@ -12,7 +12,7 @@ discard block |
||
| 12 | 12 | 'password' => 123456, |
| 13 | 13 | 'password2' => 123456, |
| 14 | 14 | 'phones' => json_encode([ |
| 15 | - '949164770','949164771','949164772' |
|
| 15 | + '949164770', '949164771', '949164772' |
|
| 16 | 16 | ]), |
| 17 | 17 | 'birth' => '28/09/1996' |
| 18 | 18 | ]), |
@@ -20,16 +20,16 @@ discard block |
||
| 20 | 20 | 'role' => 'login' |
| 21 | 21 | ]; |
| 22 | 22 | |
| 23 | -try{ |
|
| 23 | +try { |
|
| 24 | 24 | |
| 25 | 25 | /* Checks whether the passed data is valid for the selected function */ |
| 26 | 26 | $valid = Validator::execute($data); |
| 27 | 27 | |
| 28 | - if(!$valid){ |
|
| 28 | + if (!$valid) { |
|
| 29 | 29 | $errors = []; |
| 30 | - foreach(Validator::getErrors() as $err => $message){ |
|
| 30 | + foreach (Validator::getErrors() as $err => $message) { |
|
| 31 | 31 | $errors[] = [ |
| 32 | - 'input' => array_keys($message)[0], // Return name input error |
|
| 32 | + 'input' => array_keys($message)[0], // Return name input error |
|
| 33 | 33 | 'message' => $message[array_keys($message)[0]] // Return message error |
| 34 | 34 | ]; |
| 35 | 35 | } |
@@ -40,7 +40,7 @@ discard block |
||
| 40 | 40 | |
| 41 | 41 | var_dump($json); |
| 42 | 42 | |
| 43 | -}catch(Exception $er){ |
|
| 43 | +}catch (Exception $er) { |
|
| 44 | 44 | |
| 45 | 45 | die("Code Error: {$er->getCode()}, Line: {$er->getLine()}, File: {$er->getFile()}, Message: {$er->getMessage()}."); |
| 46 | 46 | |
@@ -40,7 +40,7 @@ |
||
| 40 | 40 | |
| 41 | 41 | var_dump($json); |
| 42 | 42 | |
| 43 | -}catch(Exception $er){ |
|
| 43 | +} catch(Exception $er){ |
|
| 44 | 44 | |
| 45 | 45 | die("Code Error: {$er->getCode()}, Line: {$er->getLine()}, File: {$er->getFile()}, Message: {$er->getMessage()}."); |
| 46 | 46 | |