@@ -69,7 +69,7 @@ discard block |
||
| 69 | 69 | { |
| 70 | 70 | self::$data = $datas; |
| 71 | 71 | |
| 72 | - self::existData(); |
|
| 72 | + self::existData(); |
|
| 73 | 73 | self::jsonData(); |
| 74 | 74 | self::hasProvider(); |
| 75 | 75 | self::hasRole(); |
@@ -78,21 +78,21 @@ discard block |
||
| 78 | 78 | |
| 79 | 79 | self::$model = get_class(self::getClass('HnrAzevedo\\Validator\\'.ucfirst(self::$data['provider']))); |
| 80 | 80 | |
| 81 | - self::existRole(self::$model); |
|
| 81 | + self::existRole(self::$model); |
|
| 82 | 82 | |
| 83 | 83 | $tests = 0; |
| 84 | 84 | |
| 85 | - foreach ( (self::$validators[self::$model]->getRules($datas['role'])) as $key => $value) { |
|
| 86 | - $tests = (@$value['required'] === true ) ? $tests+1 : $tests; |
|
| 85 | + foreach ( (self::$validators[self::$model]->getRules($datas['role'])) as $key => $value) { |
|
| 86 | + $tests = (@$value['required'] === true ) ? $tests+1 : $tests; |
|
| 87 | 87 | } |
| 88 | 88 | |
| 89 | - $testeds = self::validate(); |
|
| 89 | + $testeds = self::validate(); |
|
| 90 | 90 | |
| 91 | - if($tests > $testeds){ |
|
| 91 | + if($tests > $testeds){ |
|
| 92 | 92 | throw new Exception('Alguma informação necessária não pode ser validada.'); |
| 93 | 93 | } |
| 94 | 94 | |
| 95 | - return true; |
|
| 95 | + return true; |
|
| 96 | 96 | } |
| 97 | 97 | |
| 98 | 98 | public static function validate(): int |
@@ -100,33 +100,33 @@ discard block |
||
| 100 | 100 | $validate = 0; |
| 101 | 101 | foreach ( (self::$validators[self::$model]->getRules(self::$data['role'])) as $key => $value) { |
| 102 | 102 | |
| 103 | - foreach (json_decode(self::$data['data']) as $keyy => $valuee) { |
|
| 103 | + foreach (json_decode(self::$data['data']) as $keyy => $valuee) { |
|
| 104 | 104 | |
| 105 | 105 | $v = $valuee; |
| 106 | 106 | |
| 107 | - if(is_array($valuee)){ |
|
| 108 | - $v = null; |
|
| 109 | - foreach ($valuee as $vvv) { |
|
| 110 | - $v .= $vvv; |
|
| 111 | - } |
|
| 107 | + if(is_array($valuee)){ |
|
| 108 | + $v = null; |
|
| 109 | + foreach ($valuee as $vvv) { |
|
| 110 | + $v .= $vvv; |
|
| 111 | + } |
|
| 112 | 112 | } |
| 113 | 113 | |
| 114 | - $valuee = $v; |
|
| 114 | + $valuee = $v; |
|
| 115 | 115 | |
| 116 | - if(!array_key_exists($keyy, (self::$validators[self::$model]->getRules(self::$data['role'])) )){ |
|
| 116 | + if(!array_key_exists($keyy, (self::$validators[self::$model]->getRules(self::$data['role'])) )){ |
|
| 117 | 117 | throw new Exception("O campo '{$keyy}' não é esperado para está operação."); |
| 118 | 118 | } |
| 119 | 119 | |
| 120 | - if($keyy===$key){ |
|
| 120 | + if($keyy===$key){ |
|
| 121 | 121 | |
| 122 | 122 | $validate++; |
| 123 | 123 | |
| 124 | - foreach ($value as $subkey => $subvalue) { |
|
| 124 | + foreach ($value as $subkey => $subvalue) { |
|
| 125 | 125 | $function = "check_{$subkey}"; |
| 126 | 126 | self::$function($keyy,$subvalue); |
| 127 | - } |
|
| 128 | - } |
|
| 129 | - } |
|
| 127 | + } |
|
| 128 | + } |
|
| 129 | + } |
|
| 130 | 130 | } |
| 131 | 131 | return $validate; |
| 132 | 132 | } |
@@ -144,7 +144,7 @@ discard block |
||
| 144 | 144 | |
| 145 | 145 | self::existRole(self::$model); |
| 146 | 146 | |
| 147 | - foreach ( self::$validators[self::$model]->getRules($request['role']) as $field => $r) { |
|
| 147 | + foreach ( self::$validators[self::$model]->getRules($request['role']) as $field => $r) { |
|
| 148 | 148 | |
| 149 | 149 | $response .= ("{$field}:".json_encode(array_reverse($r))).','; |
| 150 | 150 | |
@@ -155,6 +155,6 @@ discard block |
||
| 155 | 155 | $response = str_replace('{"','',$response); |
| 156 | 156 | $response = str_replace('":',':',$response); |
| 157 | 157 | |
| 158 | - return $response; |
|
| 159 | - } |
|
| 158 | + return $response; |
|
| 159 | + } |
|
| 160 | 160 | } |
@@ -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,42 +16,42 @@ 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 includeValidations() |
| 46 | 46 | { |
| 47 | - if( file_exists(VALIDATOR_CONFIG['path'] . ucfirst(self::$data['provider']) . '.php') ){ |
|
| 48 | - require_once(VALIDATOR_CONFIG['path'] . ucfirst(self::$data['provider']) . '.php'); |
|
| 47 | + if (file_exists(VALIDATOR_CONFIG['path'].ucfirst(self::$data['provider']).'.php')) { |
|
| 48 | + require_once(VALIDATOR_CONFIG['path'].ucfirst(self::$data['provider']).'.php'); |
|
| 49 | 49 | } |
| 50 | 50 | } |
| 51 | 51 | |
| 52 | 52 | private static function getClass(string $class) |
| 53 | 53 | { |
| 54 | - if(!class_exists($class)){ |
|
| 54 | + if (!class_exists($class)) { |
|
| 55 | 55 | throw new Exception("Form ID {$class} inválido."); |
| 56 | 56 | } |
| 57 | 57 | |
@@ -60,7 +60,7 @@ discard block |
||
| 60 | 60 | |
| 61 | 61 | private static function existRole($rules) |
| 62 | 62 | { |
| 63 | - if(empty(self::$validators[$rules]->getRules(self::$data['role']))){ |
|
| 63 | + if (empty(self::$validators[$rules]->getRules(self::$data['role']))) { |
|
| 64 | 64 | throw new Exception('Não existe regras para validar este formulário.'); |
| 65 | 65 | } |
| 66 | 66 | } |
@@ -82,13 +82,13 @@ discard block |
||
| 82 | 82 | |
| 83 | 83 | $tests = 0; |
| 84 | 84 | |
| 85 | - foreach ( (self::$validators[self::$model]->getRules($datas['role'])) as $key => $value) { |
|
| 86 | - $tests = (@$value['required'] === true ) ? $tests+1 : $tests; |
|
| 85 | + foreach ((self::$validators[self::$model]->getRules($datas['role'])) as $key => $value) { |
|
| 86 | + $tests = (@$value['required']===true) ? $tests+1 : $tests; |
|
| 87 | 87 | } |
| 88 | 88 | |
| 89 | 89 | $testeds = self::validate(); |
| 90 | 90 | |
| 91 | - if($tests > $testeds){ |
|
| 91 | + if ($tests>$testeds) { |
|
| 92 | 92 | throw new Exception('Alguma informação necessária não pode ser validada.'); |
| 93 | 93 | } |
| 94 | 94 | |
@@ -98,13 +98,13 @@ discard block |
||
| 98 | 98 | public static function validate(): int |
| 99 | 99 | { |
| 100 | 100 | $validate = 0; |
| 101 | - foreach ( (self::$validators[self::$model]->getRules(self::$data['role'])) as $key => $value) { |
|
| 101 | + foreach ((self::$validators[self::$model]->getRules(self::$data['role'])) as $key => $value) { |
|
| 102 | 102 | |
| 103 | 103 | foreach (json_decode(self::$data['data']) as $keyy => $valuee) { |
| 104 | 104 | |
| 105 | 105 | $v = $valuee; |
| 106 | 106 | |
| 107 | - if(is_array($valuee)){ |
|
| 107 | + if (is_array($valuee)) { |
|
| 108 | 108 | $v = null; |
| 109 | 109 | foreach ($valuee as $vvv) { |
| 110 | 110 | $v .= $vvv; |
@@ -113,17 +113,17 @@ discard block |
||
| 113 | 113 | |
| 114 | 114 | $valuee = $v; |
| 115 | 115 | |
| 116 | - if(!array_key_exists($keyy, (self::$validators[self::$model]->getRules(self::$data['role'])) )){ |
|
| 116 | + if (!array_key_exists($keyy, (self::$validators[self::$model]->getRules(self::$data['role'])))) { |
|
| 117 | 117 | throw new Exception("O campo '{$keyy}' não é esperado para está operação."); |
| 118 | 118 | } |
| 119 | 119 | |
| 120 | - if($keyy===$key){ |
|
| 120 | + if ($keyy===$key) { |
|
| 121 | 121 | |
| 122 | 122 | $validate++; |
| 123 | 123 | |
| 124 | 124 | foreach ($value as $subkey => $subvalue) { |
| 125 | 125 | $function = "check_{$subkey}"; |
| 126 | - self::$function($keyy,$subvalue); |
|
| 126 | + self::$function($keyy, $subvalue); |
|
| 127 | 127 | } |
| 128 | 128 | } |
| 129 | 129 | } |
@@ -140,20 +140,20 @@ discard block |
||
| 140 | 140 | |
| 141 | 141 | self::includeValidations(); |
| 142 | 142 | |
| 143 | - self::$model = get_class( self::getClass('HnrAzevedo\\Validator\\'.ucfirst($request['provider'])) ); |
|
| 143 | + self::$model = get_class(self::getClass('HnrAzevedo\\Validator\\'.ucfirst($request['provider']))); |
|
| 144 | 144 | |
| 145 | 145 | self::existRole(self::$model); |
| 146 | 146 | |
| 147 | - foreach ( self::$validators[self::$model]->getRules($request['role']) as $field => $r) { |
|
| 147 | + foreach (self::$validators[self::$model]->getRules($request['role']) as $field => $r) { |
|
| 148 | 148 | |
| 149 | 149 | $response .= ("{$field}:".json_encode(array_reverse($r))).','; |
| 150 | 150 | |
| 151 | 151 | } |
| 152 | 152 | |
| 153 | - $response = '{'.substr($response,0,-1).'}'; |
|
| 154 | - $response = str_replace(',"',',',$response); |
|
| 155 | - $response = str_replace('{"','',$response); |
|
| 156 | - $response = str_replace('":',':',$response); |
|
| 153 | + $response = '{'.substr($response, 0, -1).'}'; |
|
| 154 | + $response = str_replace(',"', ',', $response); |
|
| 155 | + $response = str_replace('{"', '', $response); |
|
| 156 | + $response = str_replace('":', ':', $response); |
|
| 157 | 157 | |
| 158 | 158 | return $response; |
| 159 | 159 | } |
@@ -4,50 +4,50 @@ discard block |
||
| 4 | 4 | |
| 5 | 5 | use Exception; |
| 6 | 6 | |
| 7 | -Trait Check{ |
|
| 7 | +Trait Check { |
|
| 8 | 8 | protected static array $data = []; |
| 9 | 9 | protected static array $validators = []; |
| 10 | 10 | protected static string $model = ''; |
| 11 | 11 | |
| 12 | 12 | protected static function check_minlength(string $param, $value) |
| 13 | 13 | { |
| 14 | - if(self::check_required($param) || strlen($value > 0)){ |
|
| 15 | - if(strlen($value)===0){ |
|
| 16 | - throw new Exception("O campo '{$param}' é obrigatório.",1); |
|
| 14 | + if (self::check_required($param) || strlen($value>0)) { |
|
| 15 | + if (strlen($value)===0) { |
|
| 16 | + throw new Exception("O campo '{$param}' é obrigatório.", 1); |
|
| 17 | 17 | } |
| 18 | 18 | |
| 19 | - if($value < intval(self::$validators[self::$model]->getRules(self::$data['role'])[$param]['minlength'])) { |
|
| 20 | - throw new Exception("{$param} não atingiu o mínimo de caracteres esperado.",1); |
|
| 19 | + if ($value<intval(self::$validators[self::$model]->getRules(self::$data['role'])[$param]['minlength'])) { |
|
| 20 | + throw new Exception("{$param} não atingiu o mínimo de caracteres esperado.", 1); |
|
| 21 | 21 | } |
| 22 | 22 | } |
| 23 | 23 | } |
| 24 | 24 | |
| 25 | 25 | protected static function check_regex(string $param, $value) |
| 26 | 26 | { |
| 27 | - if(self::check_required($param) || strlen($value > 0)){ |
|
| 28 | - if(!@preg_match(self::$validators[self::$model]->getRules(self::$data['role'])[$param]['regex'], json_decode(self::$data['data'])->$param)){ |
|
| 29 | - throw new Exception("{$param} inválido(a).",1); |
|
| 27 | + if (self::check_required($param) || strlen($value>0)) { |
|
| 28 | + if (!@preg_match(self::$validators[self::$model]->getRules(self::$data['role'])[$param]['regex'], json_decode(self::$data['data'])->$param)) { |
|
| 29 | + throw new Exception("{$param} inválido(a).", 1); |
|
| 30 | 30 | } |
| 31 | 31 | } |
| 32 | 32 | } |
| 33 | 33 | |
| 34 | 34 | protected static function check_index(string $param, $value) |
| 35 | 35 | { |
| 36 | - if(self::check_required($param) || strlen($value > 0)){ |
|
| 36 | + if (self::check_required($param) || strlen($value>0)) { |
|
| 37 | 37 | |
| 38 | 38 | } |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 | protected static function check_equals(string $param, $value) |
| 42 | 42 | { |
| 43 | - if(self::check_required($param) || strlen($value > 0)){ |
|
| 43 | + if (self::check_required($param) || strlen($value>0)) { |
|
| 44 | 44 | |
| 45 | - if(!array_key_exists($param,json_decode(self::$data['data'],true))){ |
|
| 46 | - throw new Exception("O servidor não encontrou a informação '{$value}' para ser comparada a '{$param}'.",1); |
|
| 45 | + if (!array_key_exists($param, json_decode(self::$data['data'], true))) { |
|
| 46 | + throw new Exception("O servidor não encontrou a informação '{$value}' para ser comparada a '{$param}'.", 1); |
|
| 47 | 47 | } |
| 48 | 48 | |
| 49 | - if(json_decode(self::$data['data'])->$param != json_decode(self::$data['data'],true)[$value]){ |
|
| 50 | - throw new Exception(ucfirst($param).' está diferente de '.ucfirst($value),1); |
|
| 49 | + if (json_decode(self::$data['data'])->$param!=json_decode(self::$data['data'], true)[$value]) { |
|
| 50 | + throw new Exception(ucfirst($param).' está diferente de '.ucfirst($value), 1); |
|
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | } |
@@ -55,16 +55,16 @@ discard block |
||
| 55 | 55 | |
| 56 | 56 | protected static function check_maxlength(string $param, $value) |
| 57 | 57 | { |
| 58 | - if(self::check_required($param) || strlen($value > 0)){ |
|
| 59 | - if($value > intval(self::$validators[self::$model]->getRules(self::$data['role'])[$param]['maxlength'])) { |
|
| 60 | - throw new Exception("{$param} ultrapassou o limite de caracteres permitidos.",1); |
|
| 58 | + if (self::check_required($param) || strlen($value>0)) { |
|
| 59 | + if ($value>intval(self::$validators[self::$model]->getRules(self::$data['role'])[$param]['maxlength'])) { |
|
| 60 | + throw new Exception("{$param} ultrapassou o limite de caracteres permitidos.", 1); |
|
| 61 | 61 | } |
| 62 | 62 | } |
| 63 | 63 | } |
| 64 | 64 | |
| 65 | 65 | protected static function check_type(string $param, $value) |
| 66 | 66 | { |
| 67 | - if(self::check_required($param) || strlen($value > 0)){ |
|
| 67 | + if (self::check_required($param) || strlen($value>0)) { |
|
| 68 | 68 | /* |
| 69 | 69 | var_dump($value); |
| 70 | 70 | switch ($value) { |
@@ -83,7 +83,7 @@ discard block |
||
| 83 | 83 | |
| 84 | 84 | protected static function check_required(string $param): bool |
| 85 | 85 | { |
| 86 | - return (array_key_exists('required',self::$validators[self::$model]->getRules(self::$data['role'])[$param]) && self::$validators[self::$model]->getRules(self::$data['role'])[$param]['required']); |
|
| 86 | + return (array_key_exists('required', self::$validators[self::$model]->getRules(self::$data['role'])[$param]) && self::$validators[self::$model]->getRules(self::$data['role'])[$param]['required']); |
|
| 87 | 87 | } |
| 88 | 88 | |
| 89 | 89 | } |
| 90 | 90 | \ No newline at end of file |
@@ -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([ |
@@ -26,7 +26,7 @@ discard block |
||
| 26 | 26 | $json = Validator::toJson($data); |
| 27 | 27 | |
| 28 | 28 | |
| 29 | -}catch(Exception $er){ |
|
| 29 | +}catch (Exception $er) { |
|
| 30 | 30 | |
| 31 | 31 | die($er->getCode().' - '.$er->getMessage()); |
| 32 | 32 | |
@@ -26,7 +26,7 @@ |
||
| 26 | 26 | $json = Validator::toJson($data); |
| 27 | 27 | |
| 28 | 28 | |
| 29 | -}catch(Exception $er){ |
|
| 29 | +} catch(Exception $er){ |
|
| 30 | 30 | |
| 31 | 31 | die($er->getCode().' - '.$er->getMessage()); |
| 32 | 32 | |
@@ -9,12 +9,12 @@ |
||
| 9 | 9 | |
| 10 | 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('password2',['equals'=>'password','required'=>true]) |
|
| 15 | - ->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('password2',['equals'=>'password','required'=>true]) |
|
| 15 | + ->addField('remember',['index'=>3,'minlength'=>2,'maxlength'=>2,'required'=>false]); |
|
| 16 | 16 | |
| 17 | - return $rules; |
|
| 17 | + return $rules; |
|
| 18 | 18 | }); |
| 19 | 19 | |
| 20 | 20 | return $this; |
@@ -2,17 +2,17 @@ |
||
| 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('password2',['equals'=>'password','required'=>true]) |
|
| 15 | - ->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('password2', ['equals'=>'password', 'required'=>true]) |
|
| 15 | + ->addField('remember', ['index'=>3, 'minlength'=>2, 'maxlength'=>2, 'required'=>false]); |
|
| 16 | 16 | |
| 17 | 17 | return $rules; |
| 18 | 18 | }); |