@@ -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 | $class = basename($class); |
| 49 | 49 | throw new Exception("Form ID {$class} inválido."); |
| 50 | 50 | } |
@@ -54,7 +54,7 @@ discard block |
||
| 54 | 54 | |
| 55 | 55 | private static function existRole($rules) |
| 56 | 56 | { |
| 57 | - if(empty(self::$validators[$rules]->getRules(self::$data['role']))){ |
|
| 57 | + if (empty(self::$validators[$rules]->getRules(self::$data['role']))) { |
|
| 58 | 58 | throw new Exception('Não existe regras para validar este formulário.'); |
| 59 | 59 | } |
| 60 | 60 | } |
@@ -74,7 +74,7 @@ discard block |
||
| 74 | 74 | self::checkDatas(); |
| 75 | 75 | |
| 76 | 76 | $model = VALIDATOR_CONFIG['rules.namespace'].'\\'.ucfirst(self::$data['provider']); |
| 77 | - if(!class_exists($model)){ |
|
| 77 | + if (!class_exists($model)) { |
|
| 78 | 78 | throw new Exception("No rules {$model} found."); |
| 79 | 79 | } |
| 80 | 80 | |
@@ -82,8 +82,8 @@ discard block |
||
| 82 | 82 | |
| 83 | 83 | self::existRole(self::$model); |
| 84 | 84 | |
| 85 | - foreach ( (self::$validators[self::$model]->getRules($datas['role'])) as $key => $value) { |
|
| 86 | - if(@$value['required'] === true){ |
|
| 85 | + foreach ((self::$validators[self::$model]->getRules($datas['role'])) as $key => $value) { |
|
| 86 | + if (@$value['required']===true) { |
|
| 87 | 87 | self::$required[$key] = $value; |
| 88 | 88 | } |
| 89 | 89 | } |
@@ -99,27 +99,27 @@ discard block |
||
| 99 | 99 | |
| 100 | 100 | public static function checkErrors(): bool |
| 101 | 101 | { |
| 102 | - return (count(self::$errors) === 0); |
|
| 102 | + return (count(self::$errors)===0); |
|
| 103 | 103 | } |
| 104 | 104 | |
| 105 | 105 | public static function validate() |
| 106 | 106 | { |
| 107 | - foreach ( (self::$validators[self::$model]->getRules(self::$data['role'])) as $key => $value) { |
|
| 107 | + foreach ((self::$validators[self::$model]->getRules(self::$data['role'])) as $key => $value) { |
|
| 108 | 108 | |
| 109 | 109 | foreach (json_decode(self::$data['data']) as $keyy => $valuee) { |
| 110 | 110 | |
| 111 | - if(!array_key_exists($keyy, (self::$validators[self::$model]->getRules(self::$data['role'])) )){ |
|
| 111 | + if (!array_key_exists($keyy, (self::$validators[self::$model]->getRules(self::$data['role'])))) { |
|
| 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 | unset(self::$required[$key]); |
| 118 | 118 | |
| 119 | 119 | foreach ($value as $subkey => $subvalue) { |
| 120 | 120 | $function = "check".ucfirst($subkey); |
| 121 | 121 | self::testMethod($function); |
| 122 | - self::$function($keyy,$subvalue); |
|
| 122 | + self::$function($keyy, $subvalue); |
|
| 123 | 123 | } |
| 124 | 124 | } |
| 125 | 125 | } |
@@ -133,7 +133,7 @@ discard block |
||
| 133 | 133 | |
| 134 | 134 | public static function testMethod($method) |
| 135 | 135 | { |
| 136 | - if(!method_exists(static::class, $method)){ |
|
| 136 | + if (!method_exists(static::class, $method)) { |
|
| 137 | 137 | throw new Exception("{$method} não é uma validação válida."); |
| 138 | 138 | } |
| 139 | 139 | } |
@@ -147,22 +147,22 @@ discard block |
||
| 147 | 147 | |
| 148 | 148 | self::includeValidations(); |
| 149 | 149 | |
| 150 | - self::$model = get_class( self::getClass('HnrAzevedo\\Validator\\'.ucfirst($request['provider'])) ); |
|
| 150 | + self::$model = get_class(self::getClass('HnrAzevedo\\Validator\\'.ucfirst($request['provider']))); |
|
| 151 | 151 | |
| 152 | 152 | self::existRole(self::$model); |
| 153 | 153 | |
| 154 | - foreach ( self::$validators[self::$model]->getRules($request['role']) as $field => $r) { |
|
| 154 | + foreach (self::$validators[self::$model]->getRules($request['role']) as $field => $r) { |
|
| 155 | 155 | $r = self::replaceRegex($r); |
| 156 | 156 | $response .= ("{$field}:".json_encode(array_reverse($r))).','; |
| 157 | 157 | } |
| 158 | 158 | |
| 159 | - return '{'.substr($response,0,-1).'}'; |
|
| 159 | + return '{'.substr($response, 0, -1).'}'; |
|
| 160 | 160 | } |
| 161 | 161 | |
| 162 | 162 | private static function replaceRegex(array $rules): array |
| 163 | 163 | { |
| 164 | - if(array_key_exists('regex',$rules)){ |
|
| 165 | - $rules['regex'] = substr($rules['regex'],1,-2); |
|
| 164 | + if (array_key_exists('regex', $rules)) { |
|
| 165 | + $rules['regex'] = substr($rules['regex'], 1, -2); |
|
| 166 | 166 | } |
| 167 | 167 | return $rules; |
| 168 | 168 | } |