@@ -2,16 +2,16 @@ |
||
| 2 | 2 | |
| 3 | 3 | namespace HnrAzevedo\Validator; |
| 4 | 4 | |
| 5 | -Class User{ |
|
| 5 | +Class User { |
|
| 6 | 6 | |
| 7 | 7 | public function __construct() |
| 8 | 8 | { |
| 9 | 9 | |
| 10 | - Validator::add($this, function(Rules $rules){ |
|
| 10 | + Validator::add($this, function(Rules $rules) { |
|
| 11 | 11 | $rules->setAction('login') |
| 12 | - ->addField('email',['minlength'=>1,'regex'=>'/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/','required'=>true]) |
|
| 13 | - ->addField('password',['index'=>2,'minlength'=>6,'maxlength'=>20,'required'=>true]) |
|
| 14 | - ->addField('remember',['index'=>3,'minlength'=>2,'maxlength'=>2,'required'=>false]); |
|
| 12 | + ->addField('email', ['minlength'=>1, 'regex'=>'/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/', 'required'=>true]) |
|
| 13 | + ->addField('password', ['index'=>2, 'minlength'=>6, 'maxlength'=>20, 'required'=>true]) |
|
| 14 | + ->addField('remember', ['index'=>3, 'minlength'=>2, 'maxlength'=>2, 'required'=>false]); |
|
| 15 | 15 | |
| 16 | 16 | return $rules; |
| 17 | 17 | }); |
@@ -7,7 +7,7 @@ discard block |
||
| 7 | 7 | |
| 8 | 8 | /* NOTE: in case of error an exception is thrown */ |
| 9 | 9 | |
| 10 | -try{ |
|
| 10 | +try { |
|
| 11 | 11 | /* Required format for validation */ |
| 12 | 12 | $data = [ |
| 13 | 13 | 'data' => json_encode([ |
@@ -24,7 +24,7 @@ discard block |
||
| 24 | 24 | /* Transforms validation to Json format to be validated on the client if desired */ |
| 25 | 25 | $json = Validator::toJson($data); |
| 26 | 26 | |
| 27 | -}catch(Exception $er){ |
|
| 27 | +}catch (Exception $er) { |
|
| 28 | 28 | |
| 29 | 29 | die($er->getCode().' - '.$er->getMessage()); |
| 30 | 30 | |
@@ -4,7 +4,7 @@ discard block |
||
| 4 | 4 | |
| 5 | 5 | use Exception; |
| 6 | 6 | |
| 7 | -Class Rules{ |
|
| 7 | +Class Rules { |
|
| 8 | 8 | private string $action; |
| 9 | 9 | |
| 10 | 10 | private array $form = array(); |
@@ -22,11 +22,11 @@ discard block |
||
| 22 | 22 | |
| 23 | 23 | public function addField(string $field, array $test): Rules |
| 24 | 24 | { |
| 25 | - if(empty($this->action)){ |
|
| 25 | + if (empty($this->action)) { |
|
| 26 | 26 | throw new Exception("Form action not registered."); |
| 27 | 27 | } |
| 28 | 28 | |
| 29 | - if(empty($this->form[$this->action][$field])){ |
|
| 29 | + if (empty($this->form[$this->action][$field])) { |
|
| 30 | 30 | $this->form[$this->action][$field] = $test; |
| 31 | 31 | } |
| 32 | 32 | |
@@ -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 | } |