@@ -2,7 +2,7 @@ discard block |
||
| 2 | 2 | |
| 3 | 3 | namespace HnrAzevedo\Validator; |
| 4 | 4 | |
| 5 | -Trait Check{ |
|
| 5 | +Trait Check { |
|
| 6 | 6 | protected static array $data = []; |
| 7 | 7 | protected static array $validators = []; |
| 8 | 8 | protected static string $model = ''; |
@@ -11,12 +11,12 @@ discard block |
||
| 11 | 11 | |
| 12 | 12 | protected static function check_minlength(string $param, $value) |
| 13 | 13 | { |
| 14 | - if(self::toNext($param,$value)){ |
|
| 14 | + if (self::toNext($param, $value)) { |
|
| 15 | 15 | |
| 16 | 16 | $realval = (is_array(json_decode(self::$data['data'])->$param)) ? json_decode(self::$data['data'])->$param : [json_decode(self::$data['data'])->$param]; |
| 17 | 17 | |
| 18 | - foreach($realval as $val){ |
|
| 19 | - if($value > strlen($val)) { |
|
| 18 | + foreach ($realval as $val) { |
|
| 19 | + if ($value>strlen($val)) { |
|
| 20 | 20 | self::$errors[] = "{$param} não atingiu o mínimo de caracteres esperado."; |
| 21 | 21 | } |
| 22 | 22 | } |
@@ -25,25 +25,25 @@ discard block |
||
| 25 | 25 | |
| 26 | 26 | protected static function check_errors(): bool |
| 27 | 27 | { |
| 28 | - return (count(self::$errors) === 0); |
|
| 28 | + return (count(self::$errors)===0); |
|
| 29 | 29 | } |
| 30 | 30 | |
| 31 | 31 | protected static function check_requireds() |
| 32 | 32 | { |
| 33 | - if(count(self::$required) > 0){ |
|
| 34 | - self::$errors[] = 'As seguintes informações não poderam ser validadas: '.implode(', ',array_keys(self::$required)).'.'; |
|
| 33 | + if (count(self::$required)>0) { |
|
| 34 | + self::$errors[] = 'As seguintes informações não poderam ser validadas: '.implode(', ', array_keys(self::$required)).'.'; |
|
| 35 | 35 | } |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | protected static function check_regex(string $param, $value) |
| 39 | 39 | { |
| 40 | - if(self::toNext($param,$value)){ |
|
| 40 | + if (self::toNext($param, $value)) { |
|
| 41 | 41 | |
| 42 | 42 | $realval = (is_array(json_decode(self::$data['data'])->$param)) ? json_decode(self::$data['data'])->$param : [json_decode(self::$data['data'])->$param]; |
| 43 | 43 | |
| 44 | - foreach($realval as $val){ |
|
| 44 | + foreach ($realval as $val) { |
|
| 45 | 45 | |
| 46 | - if(!preg_match(self::$validators[self::$model]->getRules(self::$data['role'])[$param]['regex'], $val)){ |
|
| 46 | + if (!preg_match(self::$validators[self::$model]->getRules(self::$data['role'])[$param]['regex'], $val)) { |
|
| 47 | 47 | self::$errors[] = "{$param} inválido(a)."; |
| 48 | 48 | } |
| 49 | 49 | |
@@ -53,9 +53,9 @@ discard block |
||
| 53 | 53 | |
| 54 | 54 | protected static function check_mincount(string $param, $value) |
| 55 | 55 | { |
| 56 | - if(self::toNext($param,$value)){ |
|
| 56 | + if (self::toNext($param, $value)) { |
|
| 57 | 57 | $array = self::testArray($param, json_decode(self::$data['data'])->$param); |
| 58 | - if(count($array) < $value){ |
|
| 58 | + if (count($array)<$value) { |
|
| 59 | 59 | self::$errors[] = "{$param} não atingiu o mínimo esperado."; |
| 60 | 60 | } |
| 61 | 61 | } |
@@ -63,9 +63,9 @@ discard block |
||
| 63 | 63 | |
| 64 | 64 | protected static function check_maxcount(string $param, $value) |
| 65 | 65 | { |
| 66 | - if(self::toNext($param,$value)){ |
|
| 66 | + if (self::toNext($param, $value)) { |
|
| 67 | 67 | $array = self::testArray($param, json_decode(self::$data['data'])->$param); |
| 68 | - if(count($array) > $value){ |
|
| 68 | + if (count($array)>$value) { |
|
| 69 | 69 | self::$errors[] = "{$param} ultrapassou o esperado."; |
| 70 | 70 | } |
| 71 | 71 | } |
@@ -73,7 +73,7 @@ discard block |
||
| 73 | 73 | |
| 74 | 74 | protected static function testArray(string $param, $value): ?array |
| 75 | 75 | { |
| 76 | - if(!is_array($value)){ |
|
| 76 | + if (!is_array($value)) { |
|
| 77 | 77 | self::$errors[] = "Era esperado um informação em array para {$param}."; |
| 78 | 78 | } |
| 79 | 79 | return $value; |
@@ -81,13 +81,13 @@ discard block |
||
| 81 | 81 | |
| 82 | 82 | protected static function check_equals(string $param, $value) |
| 83 | 83 | { |
| 84 | - if(self::toNext($param,$value)){ |
|
| 84 | + if (self::toNext($param, $value)) { |
|
| 85 | 85 | |
| 86 | - if(!array_key_exists($param,json_decode(self::$data['data'],true))){ |
|
| 86 | + if (!array_key_exists($param, json_decode(self::$data['data'], true))) { |
|
| 87 | 87 | self::$errors[] = "O servidor não encontrou a informação '{$value}' para ser comparada a '{$param}'."; |
| 88 | 88 | } |
| 89 | 89 | |
| 90 | - if(json_decode(self::$data['data'])->$param != json_decode(self::$data['data'],true)[$value]){ |
|
| 90 | + if (json_decode(self::$data['data'])->$param!=json_decode(self::$data['data'], true)[$value]) { |
|
| 91 | 91 | self::$errors[] = ucfirst($param).' está diferente de '.ucfirst($value); |
| 92 | 92 | } |
| 93 | 93 | |
@@ -96,13 +96,13 @@ discard block |
||
| 96 | 96 | |
| 97 | 97 | protected static function check_maxlength(string $param, $value) |
| 98 | 98 | { |
| 99 | - if(self::toNext($param,$value)){ |
|
| 99 | + if (self::toNext($param, $value)) { |
|
| 100 | 100 | |
| 101 | 101 | $realval = (is_array(json_decode(self::$data['data'])->$param)) ? json_decode(self::$data['data'])->$param : [json_decode(self::$data['data'])->$param]; |
| 102 | 102 | |
| 103 | - foreach($realval as $val){ |
|
| 103 | + foreach ($realval as $val) { |
|
| 104 | 104 | |
| 105 | - if($value < strlen($val)) { |
|
| 105 | + if ($value<strlen($val)) { |
|
| 106 | 106 | self::$errors[] = "{$param} ultrapassou o máximo de caracteres esperado."; |
| 107 | 107 | } |
| 108 | 108 | |
@@ -112,11 +112,11 @@ discard block |
||
| 112 | 112 | |
| 113 | 113 | protected static function check_type(string $param, $value) |
| 114 | 114 | { |
| 115 | - if(self::toNext($param,$value)){ |
|
| 115 | + if (self::toNext($param, $value)) { |
|
| 116 | 116 | |
| 117 | 117 | switch ($value) { |
| 118 | 118 | case 'date': |
| 119 | - if(!self::validateDate(json_decode(self::$data['data'])->$param , 'd/m/Y')){ |
|
| 119 | + if (!self::validateDate(json_decode(self::$data['data'])->$param, 'd/m/Y')) { |
|
| 120 | 120 | self::$errors[] = "{$param} não é uma data válida."; |
| 121 | 121 | } |
| 122 | 122 | break; |
@@ -126,9 +126,9 @@ discard block |
||
| 126 | 126 | |
| 127 | 127 | protected static function check_filter(string $param, $value) |
| 128 | 128 | { |
| 129 | - if(self::toNext($param,$value)){ |
|
| 129 | + if (self::toNext($param, $value)) { |
|
| 130 | 130 | |
| 131 | - if(!filter_var(json_decode(self::$data['data'])->$param, $value)){ |
|
| 131 | + if (!filter_var(json_decode(self::$data['data'])->$param, $value)) { |
|
| 132 | 132 | self::$errors[] = "{$param} não passou pela filtragem de dados."; |
| 133 | 133 | } |
| 134 | 134 | |
@@ -138,17 +138,17 @@ discard block |
||
| 138 | 138 | public static function validateDate($date, $format = 'Y-m-d H:i:s') |
| 139 | 139 | { |
| 140 | 140 | $d = \DateTime::createFromFormat($format, $date); |
| 141 | - return $d && $d->format($format) == $date; |
|
| 141 | + return $d && $d->format($format)==$date; |
|
| 142 | 142 | } |
| 143 | 143 | |
| 144 | 144 | protected static function check_required(string $param): bool |
| 145 | 145 | { |
| 146 | - return (array_key_exists('required',self::$validators[self::$model]->getRules(self::$data['role'])[$param]) && self::$validators[self::$model]->getRules(self::$data['role'])[$param]['required']); |
|
| 146 | + return (array_key_exists('required', self::$validators[self::$model]->getRules(self::$data['role'])[$param]) && self::$validators[self::$model]->getRules(self::$data['role'])[$param]['required']); |
|
| 147 | 147 | } |
| 148 | 148 | |
| 149 | 149 | protected static function toNext(string $param, $value) |
| 150 | 150 | { |
| 151 | - return (self::check_required($param) || strlen($value > 0)); |
|
| 151 | + return (self::check_required($param) || strlen($value>0)); |
|
| 152 | 152 | } |
| 153 | 153 | |
| 154 | 154 | } |
| 155 | 155 | \ No newline at end of file |
@@ -67,7 +67,7 @@ discard block |
||
| 67 | 67 | |
| 68 | 68 | public static function checkDatas() |
| 69 | 69 | { |
| 70 | - self::existData(); |
|
| 70 | + self::existData(); |
|
| 71 | 71 | self::jsonData(); |
| 72 | 72 | self::hasProvider(); |
| 73 | 73 | self::hasRole(); |
@@ -83,9 +83,9 @@ discard block |
||
| 83 | 83 | |
| 84 | 84 | self::$model = get_class(self::getClass('HnrAzevedo\\Validator\\'.ucfirst(self::$data['provider']))); |
| 85 | 85 | |
| 86 | - self::existRole(self::$model); |
|
| 86 | + self::existRole(self::$model); |
|
| 87 | 87 | |
| 88 | - foreach ( (self::$validators[self::$model]->getRules($datas['role'])) as $key => $value) { |
|
| 88 | + foreach ( (self::$validators[self::$model]->getRules($datas['role'])) as $key => $value) { |
|
| 89 | 89 | if(@$value['required'] === true){ |
| 90 | 90 | self::$required[$key] = $value; |
| 91 | 91 | } |
@@ -95,30 +95,30 @@ discard block |
||
| 95 | 95 | |
| 96 | 96 | self::check_requireds(); |
| 97 | 97 | |
| 98 | - return self::check_errors(); |
|
| 98 | + return self::check_errors(); |
|
| 99 | 99 | } |
| 100 | 100 | |
| 101 | 101 | public static function validate() |
| 102 | 102 | { |
| 103 | 103 | foreach ( (self::$validators[self::$model]->getRules(self::$data['role'])) as $key => $value) { |
| 104 | 104 | |
| 105 | - foreach (json_decode(self::$data['data']) as $keyy => $valuee) { |
|
| 105 | + foreach (json_decode(self::$data['data']) as $keyy => $valuee) { |
|
| 106 | 106 | |
| 107 | - if(!array_key_exists($keyy, (self::$validators[self::$model]->getRules(self::$data['role'])) )){ |
|
| 107 | + if(!array_key_exists($keyy, (self::$validators[self::$model]->getRules(self::$data['role'])) )){ |
|
| 108 | 108 | throw new Exception("O campo '{$keyy}' não é esperado para está operação."); |
| 109 | 109 | } |
| 110 | 110 | |
| 111 | - if($keyy===$key){ |
|
| 111 | + if($keyy===$key){ |
|
| 112 | 112 | |
| 113 | 113 | unset(self::$required[$key]); |
| 114 | 114 | |
| 115 | - foreach ($value as $subkey => $subvalue) { |
|
| 115 | + foreach ($value as $subkey => $subvalue) { |
|
| 116 | 116 | $function = "check_{$subkey}"; |
| 117 | 117 | self::testMethod($function); |
| 118 | 118 | self::$function($keyy,$subvalue); |
| 119 | - } |
|
| 120 | - } |
|
| 121 | - } |
|
| 119 | + } |
|
| 120 | + } |
|
| 121 | + } |
|
| 122 | 122 | } |
| 123 | 123 | } |
| 124 | 124 | |
@@ -147,7 +147,7 @@ discard block |
||
| 147 | 147 | |
| 148 | 148 | self::existRole(self::$model); |
| 149 | 149 | |
| 150 | - foreach ( self::$validators[self::$model]->getRules($request['role']) as $field => $r) { |
|
| 150 | + foreach ( self::$validators[self::$model]->getRules($request['role']) as $field => $r) { |
|
| 151 | 151 | |
| 152 | 152 | $response .= ("{$field}:".json_encode(array_reverse($r))).','; |
| 153 | 153 | |
@@ -158,6 +158,6 @@ discard block |
||
| 158 | 158 | $response = str_replace('{"','',$response); |
| 159 | 159 | $response = str_replace('":',':',$response); |
| 160 | 160 | |
| 161 | - return $response; |
|
| 162 | - } |
|
| 161 | + return $response; |
|
| 162 | + } |
|
| 163 | 163 | } |
@@ -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 | } |
@@ -85,8 +85,8 @@ discard block |
||
| 85 | 85 | |
| 86 | 86 | self::existRole(self::$model); |
| 87 | 87 | |
| 88 | - foreach ( (self::$validators[self::$model]->getRules($datas['role'])) as $key => $value) { |
|
| 89 | - if(@$value['required'] === true){ |
|
| 88 | + foreach ((self::$validators[self::$model]->getRules($datas['role'])) as $key => $value) { |
|
| 89 | + if (@$value['required']===true) { |
|
| 90 | 90 | self::$required[$key] = $value; |
| 91 | 91 | } |
| 92 | 92 | } |
@@ -100,22 +100,22 @@ discard block |
||
| 100 | 100 | |
| 101 | 101 | public static function validate() |
| 102 | 102 | { |
| 103 | - foreach ( (self::$validators[self::$model]->getRules(self::$data['role'])) as $key => $value) { |
|
| 103 | + foreach ((self::$validators[self::$model]->getRules(self::$data['role'])) as $key => $value) { |
|
| 104 | 104 | |
| 105 | 105 | foreach (json_decode(self::$data['data']) as $keyy => $valuee) { |
| 106 | 106 | |
| 107 | - if(!array_key_exists($keyy, (self::$validators[self::$model]->getRules(self::$data['role'])) )){ |
|
| 107 | + if (!array_key_exists($keyy, (self::$validators[self::$model]->getRules(self::$data['role'])))) { |
|
| 108 | 108 | throw new Exception("O campo '{$keyy}' não é esperado para está operação."); |
| 109 | 109 | } |
| 110 | 110 | |
| 111 | - if($keyy===$key){ |
|
| 111 | + if ($keyy===$key) { |
|
| 112 | 112 | |
| 113 | 113 | unset(self::$required[$key]); |
| 114 | 114 | |
| 115 | 115 | foreach ($value as $subkey => $subvalue) { |
| 116 | 116 | $function = "check_{$subkey}"; |
| 117 | 117 | self::testMethod($function); |
| 118 | - self::$function($keyy,$subvalue); |
|
| 118 | + self::$function($keyy, $subvalue); |
|
| 119 | 119 | } |
| 120 | 120 | } |
| 121 | 121 | } |
@@ -129,7 +129,7 @@ discard block |
||
| 129 | 129 | |
| 130 | 130 | public static function testMethod($method) |
| 131 | 131 | { |
| 132 | - if(!method_exists(static::class, $method)){ |
|
| 132 | + if (!method_exists(static::class, $method)) { |
|
| 133 | 133 | throw new Exception("{$method} não é uma validação válida."); |
| 134 | 134 | } |
| 135 | 135 | } |
@@ -143,20 +143,20 @@ discard block |
||
| 143 | 143 | |
| 144 | 144 | self::includeValidations(); |
| 145 | 145 | |
| 146 | - self::$model = get_class( self::getClass('HnrAzevedo\\Validator\\'.ucfirst($request['provider'])) ); |
|
| 146 | + self::$model = get_class(self::getClass('HnrAzevedo\\Validator\\'.ucfirst($request['provider']))); |
|
| 147 | 147 | |
| 148 | 148 | self::existRole(self::$model); |
| 149 | 149 | |
| 150 | - foreach ( self::$validators[self::$model]->getRules($request['role']) as $field => $r) { |
|
| 150 | + foreach (self::$validators[self::$model]->getRules($request['role']) as $field => $r) { |
|
| 151 | 151 | |
| 152 | 152 | $response .= ("{$field}:".json_encode(array_reverse($r))).','; |
| 153 | 153 | |
| 154 | 154 | } |
| 155 | 155 | |
| 156 | - $response = '{'.substr($response,0,-1).'}'; |
|
| 157 | - $response = str_replace(',"',',',$response); |
|
| 158 | - $response = str_replace('{"','',$response); |
|
| 159 | - $response = str_replace('":',':',$response); |
|
| 156 | + $response = '{'.substr($response, 0, -1).'}'; |
|
| 157 | + $response = str_replace(',"', ',', $response); |
|
| 158 | + $response = str_replace('{"', '', $response); |
|
| 159 | + $response = str_replace('":', ':', $response); |
|
| 160 | 160 | |
| 161 | 161 | return $response; |
| 162 | 162 | } |
@@ -9,30 +9,30 @@ |
||
| 9 | 9 | |
| 10 | 10 | public function __construct(object $model) |
| 11 | 11 | { |
| 12 | - $this->form['model'] = ucfirst(get_class($model)); |
|
| 13 | - } |
|
| 12 | + $this->form['model'] = ucfirst(get_class($model)); |
|
| 13 | + } |
|
| 14 | 14 | |
| 15 | 15 | public function setAction(string $action): Rules |
| 16 | 16 | { |
| 17 | - $this->action = $action; |
|
| 18 | - return $this; |
|
| 19 | - } |
|
| 17 | + $this->action = $action; |
|
| 18 | + return $this; |
|
| 19 | + } |
|
| 20 | 20 | |
| 21 | 21 | public function addField(string $field, array $test): Rules |
| 22 | 22 | { |
| 23 | - if(empty($this->action)){ |
|
| 23 | + if(empty($this->action)){ |
|
| 24 | 24 | self::$errors[] = "Form action not registered."; |
| 25 | 25 | } |
| 26 | 26 | |
| 27 | - if(empty($this->form[$this->action][$field])){ |
|
| 27 | + if(empty($this->form[$this->action][$field])){ |
|
| 28 | 28 | $this->form[$this->action][$field] = $test; |
| 29 | 29 | } |
| 30 | 30 | |
| 31 | - return $this; |
|
| 32 | - } |
|
| 31 | + return $this; |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | 34 | public function getRules(string $action): ?array |
| 35 | 35 | { |
| 36 | - return (array_key_exists($action, $this->form)) ? $this->form[$action] : null; |
|
| 37 | - } |
|
| 36 | + return (array_key_exists($action, $this->form)) ? $this->form[$action] : null; |
|
| 37 | + } |
|
| 38 | 38 | } |
@@ -2,7 +2,7 @@ discard block |
||
| 2 | 2 | |
| 3 | 3 | namespace HnrAzevedo\Validator; |
| 4 | 4 | |
| 5 | -Class Rules{ |
|
| 5 | +Class Rules { |
|
| 6 | 6 | private string $action; |
| 7 | 7 | |
| 8 | 8 | private array $form = array(); |
@@ -20,11 +20,11 @@ discard block |
||
| 20 | 20 | |
| 21 | 21 | public function addField(string $field, array $test): Rules |
| 22 | 22 | { |
| 23 | - if(empty($this->action)){ |
|
| 23 | + if (empty($this->action)) { |
|
| 24 | 24 | self::$errors[] = "Form action not registered."; |
| 25 | 25 | } |
| 26 | 26 | |
| 27 | - if(empty($this->form[$this->action][$field])){ |
|
| 27 | + if (empty($this->form[$this->action][$field])) { |
|
| 28 | 28 | $this->form[$this->action][$field] = $test; |
| 29 | 29 | } |
| 30 | 30 | |