@@ -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,16 +11,16 @@ 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(strlen($val) === 0) { |
|
| 18 | + foreach ($realval as $val) { |
|
| 19 | + if (strlen($val)===0) { |
|
| 20 | 20 | self::$errors[] = "{$param} é obrigatório."; |
| 21 | 21 | continue; |
| 22 | 22 | } |
| 23 | - if($value > strlen($val)) { |
|
| 23 | + if ($value>strlen($val)) { |
|
| 24 | 24 | self::$errors[] = "{$param} não atingiu o mínimo de caracteres esperado."; |
| 25 | 25 | } |
| 26 | 26 | } |
@@ -29,20 +29,20 @@ discard block |
||
| 29 | 29 | |
| 30 | 30 | protected static function check_requireds() |
| 31 | 31 | { |
| 32 | - if(count(self::$required) > 0){ |
|
| 33 | - self::$errors[] = 'As seguintes informações não poderam ser validadas: '.implode(', ',array_keys(self::$required)).'.'; |
|
| 32 | + if (count(self::$required)>0) { |
|
| 33 | + self::$errors[] = 'As seguintes informações não poderam ser validadas: '.implode(', ', array_keys(self::$required)).'.'; |
|
| 34 | 34 | } |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | 37 | protected static function check_regex(string $param, $value) |
| 38 | 38 | { |
| 39 | - if(self::toNext($param,$value)){ |
|
| 39 | + if (self::toNext($param, $value)) { |
|
| 40 | 40 | |
| 41 | 41 | $realval = (is_array(json_decode(self::$data['data'])->$param)) ? json_decode(self::$data['data'])->$param : [json_decode(self::$data['data'])->$param]; |
| 42 | 42 | |
| 43 | - foreach($realval as $val){ |
|
| 43 | + foreach ($realval as $val) { |
|
| 44 | 44 | |
| 45 | - if(!preg_match(self::$validators[self::$model]->getRules(self::$data['role'])[$param]['regex'], $val)){ |
|
| 45 | + if (!preg_match(self::$validators[self::$model]->getRules(self::$data['role'])[$param]['regex'], $val)) { |
|
| 46 | 46 | self::$errors[] = "{$param} inválido(a)."; |
| 47 | 47 | } |
| 48 | 48 | |
@@ -52,9 +52,9 @@ discard block |
||
| 52 | 52 | |
| 53 | 53 | protected static function check_mincount(string $param, $value) |
| 54 | 54 | { |
| 55 | - if(self::toNext($param,$value)){ |
|
| 55 | + if (self::toNext($param, $value)) { |
|
| 56 | 56 | $array = self::testArray($param, json_decode(self::$data['data'])->$param); |
| 57 | - if(count($array) < $value){ |
|
| 57 | + if (count($array)<$value) { |
|
| 58 | 58 | self::$errors[] = "{$param} não atingiu o mínimo esperado."; |
| 59 | 59 | } |
| 60 | 60 | } |
@@ -62,9 +62,9 @@ discard block |
||
| 62 | 62 | |
| 63 | 63 | protected static function check_maxcount(string $param, $value) |
| 64 | 64 | { |
| 65 | - if(self::toNext($param,$value)){ |
|
| 65 | + if (self::toNext($param, $value)) { |
|
| 66 | 66 | $array = self::testArray($param, json_decode(self::$data['data'])->$param); |
| 67 | - if(count($array) > $value){ |
|
| 67 | + if (count($array)>$value) { |
|
| 68 | 68 | self::$errors[] = "{$param} ultrapassou o esperado."; |
| 69 | 69 | } |
| 70 | 70 | } |
@@ -72,7 +72,7 @@ discard block |
||
| 72 | 72 | |
| 73 | 73 | protected static function testArray(string $param, $value): ?array |
| 74 | 74 | { |
| 75 | - if(!is_array($value)){ |
|
| 75 | + if (!is_array($value)) { |
|
| 76 | 76 | self::$errors[] = "Era esperado um informação em array para {$param}."; |
| 77 | 77 | } |
| 78 | 78 | return $value; |
@@ -80,13 +80,13 @@ discard block |
||
| 80 | 80 | |
| 81 | 81 | protected static function check_equals(string $param, $value) |
| 82 | 82 | { |
| 83 | - if(self::toNext($param,$value)){ |
|
| 83 | + if (self::toNext($param, $value)) { |
|
| 84 | 84 | |
| 85 | - if(!array_key_exists($param,json_decode(self::$data['data'],true))){ |
|
| 85 | + if (!array_key_exists($param, json_decode(self::$data['data'], true))) { |
|
| 86 | 86 | self::$errors[] = "O servidor não encontrou a informação '{$value}' para ser comparada a '{$param}'."; |
| 87 | 87 | } |
| 88 | 88 | |
| 89 | - if(json_decode(self::$data['data'])->$param != json_decode(self::$data['data'],true)[$value]){ |
|
| 89 | + if (json_decode(self::$data['data'])->$param!=json_decode(self::$data['data'], true)[$value]) { |
|
| 90 | 90 | self::$errors[] = ucfirst($param).' está diferente de '.ucfirst($value); |
| 91 | 91 | } |
| 92 | 92 | |
@@ -95,13 +95,13 @@ discard block |
||
| 95 | 95 | |
| 96 | 96 | protected static function check_maxlength(string $param, $value) |
| 97 | 97 | { |
| 98 | - if(self::toNext($param,$value)){ |
|
| 98 | + if (self::toNext($param, $value)) { |
|
| 99 | 99 | |
| 100 | 100 | $realval = (is_array(json_decode(self::$data['data'])->$param)) ? json_decode(self::$data['data'])->$param : [json_decode(self::$data['data'])->$param]; |
| 101 | 101 | |
| 102 | - foreach($realval as $val){ |
|
| 102 | + foreach ($realval as $val) { |
|
| 103 | 103 | |
| 104 | - if($value < strlen($val)) { |
|
| 104 | + if ($value<strlen($val)) { |
|
| 105 | 105 | self::$errors[] = "{$param} ultrapassou o máximo de caracteres esperado."; |
| 106 | 106 | } |
| 107 | 107 | |
@@ -111,11 +111,11 @@ discard block |
||
| 111 | 111 | |
| 112 | 112 | protected static function check_type(string $param, $value) |
| 113 | 113 | { |
| 114 | - if(self::toNext($param,$value)){ |
|
| 114 | + if (self::toNext($param, $value)) { |
|
| 115 | 115 | |
| 116 | 116 | switch ($value) { |
| 117 | 117 | case 'date': |
| 118 | - if(!self::validateDate(json_decode(self::$data['data'])->$param , 'd/m/Y')){ |
|
| 118 | + if (!self::validateDate(json_decode(self::$data['data'])->$param, 'd/m/Y')) { |
|
| 119 | 119 | self::$errors[] = "{$param} não é uma data válida."; |
| 120 | 120 | } |
| 121 | 121 | break; |
@@ -125,9 +125,9 @@ discard block |
||
| 125 | 125 | |
| 126 | 126 | protected static function check_filter(string $param, $value) |
| 127 | 127 | { |
| 128 | - if(self::toNext($param,$value)){ |
|
| 128 | + if (self::toNext($param, $value)) { |
|
| 129 | 129 | |
| 130 | - if(!filter_var(json_decode(self::$data['data'])->$param, $value)){ |
|
| 130 | + if (!filter_var(json_decode(self::$data['data'])->$param, $value)) { |
|
| 131 | 131 | self::$errors[] = "{$param} não passou pela filtragem de dados."; |
| 132 | 132 | } |
| 133 | 133 | |
@@ -137,17 +137,17 @@ discard block |
||
| 137 | 137 | public static function validateDate($date, $format = 'Y-m-d H:i:s') |
| 138 | 138 | { |
| 139 | 139 | $d = \DateTime::createFromFormat($format, $date); |
| 140 | - return $d && $d->format($format) == $date; |
|
| 140 | + return $d && $d->format($format)==$date; |
|
| 141 | 141 | } |
| 142 | 142 | |
| 143 | 143 | protected static function check_required(string $param): bool |
| 144 | 144 | { |
| 145 | - return (array_key_exists('required',self::$validators[self::$model]->getRules(self::$data['role'])[$param]) && self::$validators[self::$model]->getRules(self::$data['role'])[$param]['required']); |
|
| 145 | + 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 | 146 | } |
| 147 | 147 | |
| 148 | 148 | protected static function toNext(string $param, $value) |
| 149 | 149 | { |
| 150 | - return (self::check_required($param) || strlen($value > 0)); |
|
| 150 | + return (self::check_required($param) || strlen($value>0)); |
|
| 151 | 151 | } |
| 152 | 152 | |
| 153 | 153 | } |