@@ -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,18 +11,18 @@ 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[] = [ |
| 21 | 21 | $param => 'é obrigatório.' |
| 22 | 22 | ]; |
| 23 | 23 | continue; |
| 24 | 24 | } |
| 25 | - if($value > strlen($val)) { |
|
| 25 | + if ($value>strlen($val)) { |
|
| 26 | 26 | self::$errors[] = [ |
| 27 | 27 | $param => 'não atingiu o mínimo de caracteres esperado.' |
| 28 | 28 | ]; |
@@ -33,22 +33,22 @@ discard block |
||
| 33 | 33 | |
| 34 | 34 | protected static function check_requireds() |
| 35 | 35 | { |
| 36 | - if(count(self::$required) > 0){ |
|
| 36 | + if (count(self::$required)>0) { |
|
| 37 | 37 | self::$errors[] = [ |
| 38 | - 'As seguintes informações não poderam ser validadas: '.implode(', ',array_keys(self::$required)).'.' |
|
| 38 | + 'As seguintes informações não poderam ser validadas: '.implode(', ', array_keys(self::$required)).'.' |
|
| 39 | 39 | ]; |
| 40 | 40 | } |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | protected static function check_regex(string $param, $value) |
| 44 | 44 | { |
| 45 | - if(self::toNext($param,$value)){ |
|
| 45 | + if (self::toNext($param, $value)) { |
|
| 46 | 46 | |
| 47 | 47 | $realval = (is_array(json_decode(self::$data['data'])->$param)) ? json_decode(self::$data['data'])->$param : [json_decode(self::$data['data'])->$param]; |
| 48 | 48 | |
| 49 | - foreach($realval as $val){ |
|
| 49 | + foreach ($realval as $val) { |
|
| 50 | 50 | |
| 51 | - if(!preg_match(self::$validators[self::$model]->getRules(self::$data['role'])[$param]['regex'], $val)){ |
|
| 51 | + if (!preg_match(self::$validators[self::$model]->getRules(self::$data['role'])[$param]['regex'], $val)) { |
|
| 52 | 52 | self::$errors[] = [ |
| 53 | 53 | $param => 'inválido(a).' |
| 54 | 54 | ]; |
@@ -60,9 +60,9 @@ discard block |
||
| 60 | 60 | |
| 61 | 61 | protected static function check_mincount(string $param, $value) |
| 62 | 62 | { |
| 63 | - if(self::toNext($param,$value)){ |
|
| 63 | + if (self::toNext($param, $value)) { |
|
| 64 | 64 | $array = self::testArray($param, json_decode(self::$data['data'])->$param); |
| 65 | - if(count($array) < $value){ |
|
| 65 | + if (count($array)<$value) { |
|
| 66 | 66 | self::$errors[] = [ |
| 67 | 67 | $param => 'não atingiu o mínimo esperado.' |
| 68 | 68 | ]; |
@@ -72,9 +72,9 @@ discard block |
||
| 72 | 72 | |
| 73 | 73 | protected static function check_maxcount(string $param, $value) |
| 74 | 74 | { |
| 75 | - if(self::toNext($param,$value)){ |
|
| 75 | + if (self::toNext($param, $value)) { |
|
| 76 | 76 | $array = self::testArray($param, json_decode(self::$data['data'])->$param); |
| 77 | - if(count($array) > $value){ |
|
| 77 | + if (count($array)>$value) { |
|
| 78 | 78 | self::$errors[] = [ |
| 79 | 79 | $param => 'ultrapassou o esperado.' |
| 80 | 80 | ]; |
@@ -84,7 +84,7 @@ discard block |
||
| 84 | 84 | |
| 85 | 85 | protected static function testArray(string $param, $value): ?array |
| 86 | 86 | { |
| 87 | - if(!is_array($value)){ |
|
| 87 | + if (!is_array($value)) { |
|
| 88 | 88 | self::$errors[] = [ |
| 89 | 89 | $param => 'Era esperado um informação em array para está informação.' |
| 90 | 90 | ]; |
@@ -94,15 +94,15 @@ discard block |
||
| 94 | 94 | |
| 95 | 95 | protected static function check_equals(string $param, $value) |
| 96 | 96 | { |
| 97 | - if(self::toNext($param,$value)){ |
|
| 97 | + if (self::toNext($param, $value)) { |
|
| 98 | 98 | |
| 99 | - if(!array_key_exists($param,json_decode(self::$data['data'],true))){ |
|
| 99 | + if (!array_key_exists($param, json_decode(self::$data['data'], true))) { |
|
| 100 | 100 | self::$errors[] = [ |
| 101 | 101 | $param => "O servidor não encontrou a informação '{$value}' para ser comparada." |
| 102 | 102 | ]; |
| 103 | 103 | } |
| 104 | 104 | |
| 105 | - if(json_decode(self::$data['data'])->$param != json_decode(self::$data['data'],true)[$value]){ |
|
| 105 | + if (json_decode(self::$data['data'])->$param!=json_decode(self::$data['data'], true)[$value]) { |
|
| 106 | 106 | self::$errors[] = [ |
| 107 | 107 | $param => 'está diferente de '.ucfirst($value) |
| 108 | 108 | ]; |
@@ -113,13 +113,13 @@ discard block |
||
| 113 | 113 | |
| 114 | 114 | protected static function check_maxlength(string $param, $value) |
| 115 | 115 | { |
| 116 | - if(self::toNext($param,$value)){ |
|
| 116 | + if (self::toNext($param, $value)) { |
|
| 117 | 117 | |
| 118 | 118 | $realval = (is_array(json_decode(self::$data['data'])->$param)) ? json_decode(self::$data['data'])->$param : [json_decode(self::$data['data'])->$param]; |
| 119 | 119 | |
| 120 | - foreach($realval as $val){ |
|
| 120 | + foreach ($realval as $val) { |
|
| 121 | 121 | |
| 122 | - if($value < strlen($val)) { |
|
| 122 | + if ($value<strlen($val)) { |
|
| 123 | 123 | self::$errors[] = [ |
| 124 | 124 | $param => 'ultrapassou o máximo de caracteres esperado.' |
| 125 | 125 | ]; |
@@ -131,11 +131,11 @@ discard block |
||
| 131 | 131 | |
| 132 | 132 | protected static function check_type(string $param, $value) |
| 133 | 133 | { |
| 134 | - if(self::toNext($param,$value)){ |
|
| 134 | + if (self::toNext($param, $value)) { |
|
| 135 | 135 | |
| 136 | 136 | switch ($value) { |
| 137 | 137 | case 'date': |
| 138 | - if(!self::validateDate(json_decode(self::$data['data'])->$param , 'd/m/Y')){ |
|
| 138 | + if (!self::validateDate(json_decode(self::$data['data'])->$param, 'd/m/Y')) { |
|
| 139 | 139 | self::$errors[] = [ |
| 140 | 140 | $param => 'não é uma data válida.' |
| 141 | 141 | ]; |
@@ -147,9 +147,9 @@ discard block |
||
| 147 | 147 | |
| 148 | 148 | protected static function check_filter(string $param, $value) |
| 149 | 149 | { |
| 150 | - if(self::toNext($param,$value)){ |
|
| 150 | + if (self::toNext($param, $value)) { |
|
| 151 | 151 | |
| 152 | - if(!filter_var(json_decode(self::$data['data'])->$param, $value)){ |
|
| 152 | + if (!filter_var(json_decode(self::$data['data'])->$param, $value)) { |
|
| 153 | 153 | self::$errors[] = [ |
| 154 | 154 | $param => 'não passou pela filtragem de dados.' |
| 155 | 155 | ]; |
@@ -161,17 +161,17 @@ discard block |
||
| 161 | 161 | public static function validateDate($date, $format = 'Y-m-d H:i:s') |
| 162 | 162 | { |
| 163 | 163 | $d = \DateTime::createFromFormat($format, $date); |
| 164 | - return $d && $d->format($format) == $date; |
|
| 164 | + return $d && $d->format($format)==$date; |
|
| 165 | 165 | } |
| 166 | 166 | |
| 167 | 167 | protected static function check_required(string $param): bool |
| 168 | 168 | { |
| 169 | - return (array_key_exists('required',self::$validators[self::$model]->getRules(self::$data['role'])[$param]) && self::$validators[self::$model]->getRules(self::$data['role'])[$param]['required']); |
|
| 169 | + return (array_key_exists('required', self::$validators[self::$model]->getRules(self::$data['role'])[$param]) && self::$validators[self::$model]->getRules(self::$data['role'])[$param]['required']); |
|
| 170 | 170 | } |
| 171 | 171 | |
| 172 | 172 | protected static function toNext(string $param, $value) |
| 173 | 173 | { |
| 174 | - return (self::check_required($param) || strlen($value > 0)); |
|
| 174 | + return (self::check_required($param) || strlen($value>0)); |
|
| 175 | 175 | } |
| 176 | 176 | |
| 177 | 177 | } |