@@ -24,10 +24,10 @@ discard block |
||
| 24 | 24 | |
| 25 | 25 | protected function data($field = null, ?array $values = null) |
| 26 | 26 | { |
| 27 | - if(null !== $values){ |
|
| 27 | + if (null!==$values) { |
|
| 28 | 28 | $this->data[$field] = $values; |
| 29 | 29 | } |
| 30 | - if(null !== $field){ |
|
| 30 | + if (null!==$field) { |
|
| 31 | 31 | return $this->data[$field]; |
| 32 | 32 | } |
| 33 | 33 | return $this->data; |
@@ -35,7 +35,7 @@ discard block |
||
| 35 | 35 | |
| 36 | 36 | protected function validator(string $model, ?object $callback = null) |
| 37 | 37 | { |
| 38 | - if(null !== $callback){ |
|
| 38 | + if (null!==$callback) { |
|
| 39 | 39 | $this->validators[$model] = $callback; |
| 40 | 40 | } |
| 41 | 41 | return $this->validators[$model]; |
@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | |
| 44 | 44 | protected function model(?string $model = null): string |
| 45 | 45 | { |
| 46 | - if(null !== $model){ |
|
| 46 | + if (null!==$model) { |
|
| 47 | 47 | $this->model = $model; |
| 48 | 48 | } |
| 49 | 49 | return $this->model; |
@@ -77,7 +77,7 @@ discard block |
||
| 77 | 77 | self::getInstance()->errors[] = $er->getMessage(); |
| 78 | 78 | } |
| 79 | 79 | |
| 80 | - return self::checkErrors(); |
|
| 80 | + return self::checkErrors(); |
|
| 81 | 81 | } |
| 82 | 82 | |
| 83 | 83 | public static function checkErrors(): bool |
@@ -89,21 +89,21 @@ discard block |
||
| 89 | 89 | { |
| 90 | 90 | foreach ( (self::getInstance()->validator(self::getInstance()->model)->getRules(self::getInstance()->data['ROLE'])) as $key => $value) { |
| 91 | 91 | |
| 92 | - foreach (self::getInstance()->data as $keyy => $valuee) { |
|
| 92 | + foreach (self::getInstance()->data as $keyy => $valuee) { |
|
| 93 | 93 | |
| 94 | - self::getInstance()->checkExpected($keyy); |
|
| 94 | + self::getInstance()->checkExpected($keyy); |
|
| 95 | 95 | |
| 96 | - if($keyy===$key){ |
|
| 96 | + if($keyy===$key){ |
|
| 97 | 97 | |
| 98 | 98 | unset(self::getInstance()->required[$key]); |
| 99 | 99 | |
| 100 | - foreach ($value as $subkey => $subvalue) { |
|
| 100 | + foreach ($value as $subkey => $subvalue) { |
|
| 101 | 101 | $function = "check".ucfirst($subkey); |
| 102 | 102 | self::getInstance()->testMethod($function); |
| 103 | 103 | self::getInstance()->$function($keyy, $subvalue); |
| 104 | - } |
|
| 105 | - } |
|
| 106 | - } |
|
| 104 | + } |
|
| 105 | + } |
|
| 106 | + } |
|
| 107 | 107 | } |
| 108 | 108 | } |
| 109 | 109 | |
@@ -141,7 +141,7 @@ discard block |
||
| 141 | 141 | |
| 142 | 142 | self::getInstance()->existRole(self::getInstance()->model()); |
| 143 | 143 | |
| 144 | - foreach ( self::getInstance()->validator(self::getInstance()->model())->getRules($request['ROLE']) as $field => $r) { |
|
| 144 | + foreach ( self::getInstance()->validator(self::getInstance()->model())->getRules($request['ROLE']) as $field => $r) { |
|
| 145 | 145 | $r = self::getInstance()->replaceRegex($r); |
| 146 | 146 | $response .= ("{$field}:".json_encode(array_reverse($r))).','; |
| 147 | 147 | } |
@@ -27,7 +27,7 @@ discard block |
||
| 27 | 27 | |
| 28 | 28 | private static function getClass(string $class) |
| 29 | 29 | { |
| 30 | - if(!class_exists($class)){ |
|
| 30 | + if (!class_exists($class)) { |
|
| 31 | 31 | throw new \RuntimeException("Form ID {$class} inválido"); |
| 32 | 32 | } |
| 33 | 33 | |
@@ -38,21 +38,21 @@ discard block |
||
| 38 | 38 | |
| 39 | 39 | private function existRole($rules) |
| 40 | 40 | { |
| 41 | - if(empty(self::getInstance()->validator($rules)->getRules(self::getInstance()->data['ROLE']))){ |
|
| 41 | + if (empty(self::getInstance()->validator($rules)->getRules(self::getInstance()->data['ROLE']))) { |
|
| 42 | 42 | throw new \RuntimeException('Não existe regras para validar este formulário'); |
| 43 | 43 | } |
| 44 | 44 | } |
| 45 | 45 | |
| 46 | 46 | public function checkDatas(array $data): void |
| 47 | 47 | { |
| 48 | - if(!isset($data['PROVIDER']) || !isset($data['ROLE'])){ |
|
| 48 | + if (!isset($data['PROVIDER']) || !isset($data['ROLE'])) { |
|
| 49 | 49 | throw new \RuntimeException('The server did not receive the information needed to retrieve the requested validator'); |
| 50 | 50 | } |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | public static function execute(array $data): bool |
| 54 | 54 | { |
| 55 | - try{ |
|
| 55 | + try { |
|
| 56 | 56 | self::getInstance()->checkDatas($data); |
| 57 | 57 | |
| 58 | 58 | self::getInstance()->data = $data; |
@@ -63,8 +63,8 @@ discard block |
||
| 63 | 63 | |
| 64 | 64 | self::getInstance()->existRole(self::getInstance()->model); |
| 65 | 65 | |
| 66 | - foreach ( (self::getInstance()->validator(self::getInstance()->model)->getRules($data['ROLE'])) as $key => $value) { |
|
| 67 | - if(@$value['required'] === true){ |
|
| 66 | + foreach ((self::getInstance()->validator(self::getInstance()->model)->getRules($data['ROLE'])) as $key => $value) { |
|
| 67 | + if (@$value['required']===true) { |
|
| 68 | 68 | self::getInstance()->required[$key] = $value; |
| 69 | 69 | } |
| 70 | 70 | } |
@@ -73,7 +73,7 @@ discard block |
||
| 73 | 73 | |
| 74 | 74 | self::getInstance()->validate(); |
| 75 | 75 | self::getInstance()->checkRequireds(); |
| 76 | - }catch(\Exception $er){ |
|
| 76 | + }catch (\Exception $er) { |
|
| 77 | 77 | self::getInstance()->errors[] = $er->getMessage(); |
| 78 | 78 | } |
| 79 | 79 | |
@@ -82,18 +82,18 @@ discard block |
||
| 82 | 82 | |
| 83 | 83 | public static function checkErrors(): bool |
| 84 | 84 | { |
| 85 | - return (count(self::getInstance()->errors) === 0); |
|
| 85 | + return (count(self::getInstance()->errors)===0); |
|
| 86 | 86 | } |
| 87 | 87 | |
| 88 | 88 | public function validate(): void |
| 89 | 89 | { |
| 90 | - foreach ( (self::getInstance()->validator(self::getInstance()->model)->getRules(self::getInstance()->data['ROLE'])) as $key => $value) { |
|
| 90 | + foreach ((self::getInstance()->validator(self::getInstance()->model)->getRules(self::getInstance()->data['ROLE'])) as $key => $value) { |
|
| 91 | 91 | |
| 92 | 92 | foreach (self::getInstance()->data as $keyy => $valuee) { |
| 93 | 93 | |
| 94 | 94 | self::getInstance()->checkExpected($keyy); |
| 95 | 95 | |
| 96 | - if($keyy===$key){ |
|
| 96 | + if ($keyy===$key) { |
|
| 97 | 97 | |
| 98 | 98 | unset(self::getInstance()->required[$key]); |
| 99 | 99 | |
@@ -109,7 +109,7 @@ discard block |
||
| 109 | 109 | |
| 110 | 110 | private function checkExpected(string $keyy): void |
| 111 | 111 | { |
| 112 | - if(!array_key_exists($keyy, (self::getInstance()->validator(self::getInstance()->model)->getRules(self::getInstance()->data['ROLE'])) ) && !in_array($keyy, self::getInstance()->defaultData)){ |
|
| 112 | + if (!array_key_exists($keyy, (self::getInstance()->validator(self::getInstance()->model)->getRules(self::getInstance()->data['ROLE']))) && !in_array($keyy, self::getInstance()->defaultData)) { |
|
| 113 | 113 | throw new \RuntimeException("O campo '{$keyy}' não é esperado para está operação"); |
| 114 | 114 | } |
| 115 | 115 | } |
@@ -121,7 +121,7 @@ discard block |
||
| 121 | 121 | |
| 122 | 122 | public function testMethod($method): void |
| 123 | 123 | { |
| 124 | - if(!method_exists(static::class, $method)){ |
|
| 124 | + if (!method_exists(static::class, $method)) { |
|
| 125 | 125 | throw new \RuntimeException("{$method} não é uma validação válida"); |
| 126 | 126 | } |
| 127 | 127 | } |
@@ -141,17 +141,17 @@ discard block |
||
| 141 | 141 | |
| 142 | 142 | self::getInstance()->existRole(self::getInstance()->model()); |
| 143 | 143 | |
| 144 | - foreach ( self::getInstance()->validator(self::getInstance()->model())->getRules($request['ROLE']) as $field => $r) { |
|
| 144 | + foreach (self::getInstance()->validator(self::getInstance()->model())->getRules($request['ROLE']) as $field => $r) { |
|
| 145 | 145 | $r = self::getInstance()->replaceRegex($r); |
| 146 | 146 | $response .= ("{$field}:".json_encode(array_reverse($r))).','; |
| 147 | 147 | } |
| 148 | 148 | |
| 149 | - return '{'.substr($response,0,-1).'}'; |
|
| 149 | + return '{'.substr($response, 0, -1).'}'; |
|
| 150 | 150 | } |
| 151 | 151 | |
| 152 | 152 | private function replaceRegex(array $rules): array |
| 153 | 153 | { |
| 154 | - if(array_key_exists('regex', $rules)){ |
|
| 154 | + if (array_key_exists('regex', $rules)) { |
|
| 155 | 155 | $rules['regex'] = substr($rules['regex'], 1, -2); |
| 156 | 156 | } |
| 157 | 157 | return $rules; |
@@ -9,18 +9,18 @@ discard block |
||
| 9 | 9 | |
| 10 | 10 | protected function checkMinlength(string $param, $value): void |
| 11 | 11 | { |
| 12 | - if(self::getInstance()->toNext($param, $value)){ |
|
| 12 | + if (self::getInstance()->toNext($param, $value)) { |
|
| 13 | 13 | |
| 14 | 14 | $realval = (is_array(self::getInstance()->data($param))) ? self::getInstance()->data($param) : [self::getInstance()->data($param)]; |
| 15 | 15 | |
| 16 | - foreach($realval as $val){ |
|
| 17 | - if(strlen($val) === 0) { |
|
| 16 | + foreach ($realval as $val) { |
|
| 17 | + if (strlen($val)===0) { |
|
| 18 | 18 | self::getInstance()->error([ |
| 19 | 19 | $param => ' é obrigatório' |
| 20 | 20 | ]); |
| 21 | 21 | continue; |
| 22 | 22 | } |
| 23 | - if($value > strlen($val)) { |
|
| 23 | + if ($value>strlen($val)) { |
|
| 24 | 24 | self::getInstance()->error([ |
| 25 | 25 | $param => 'não atingiu o mínimo de caracteres esperado' |
| 26 | 26 | ]); |
@@ -31,12 +31,12 @@ discard block |
||
| 31 | 31 | |
| 32 | 32 | protected function checkRegex(string $param, $value): void |
| 33 | 33 | { |
| 34 | - if(self::getInstance()->toNext($param, $value)){ |
|
| 34 | + if (self::getInstance()->toNext($param, $value)) { |
|
| 35 | 35 | |
| 36 | 36 | $realval = (is_array(self::getInstance()->data($param))) ? self::getInstance()->data($param) : [self::getInstance()->data($param)]; |
| 37 | 37 | |
| 38 | - foreach($realval as $val){ |
|
| 39 | - if(!preg_match(self::getInstance()->validator(self::getInstance()->model())->getRules(self::getInstance()->data('ROLE'))[$param]['regex'], $val)){ |
|
| 38 | + foreach ($realval as $val) { |
|
| 39 | + if (!preg_match(self::getInstance()->validator(self::getInstance()->model())->getRules(self::getInstance()->data('ROLE'))[$param]['regex'], $val)) { |
|
| 40 | 40 | self::getInstance()->error([ |
| 41 | 41 | $param => 'inválido(a)' |
| 42 | 42 | ]); |
@@ -47,9 +47,9 @@ discard block |
||
| 47 | 47 | |
| 48 | 48 | protected function checkMincount(string $param, $value): void |
| 49 | 49 | { |
| 50 | - if(self::getInstance()->toNext($param, $value)){ |
|
| 50 | + if (self::getInstance()->toNext($param, $value)) { |
|
| 51 | 51 | $array = self::getInstance()->testArray($param, self::getInstance()->data($param)); |
| 52 | - if(count($array) < $value){ |
|
| 52 | + if (count($array)<$value) { |
|
| 53 | 53 | self::getInstance()->error([ |
| 54 | 54 | $param => 'não atingiu o mínimo esperado' |
| 55 | 55 | ]); |
@@ -59,9 +59,9 @@ discard block |
||
| 59 | 59 | |
| 60 | 60 | protected function checkMaxcount(string $param, $value): void |
| 61 | 61 | { |
| 62 | - if(self::getInstance()->toNext($param, $value)){ |
|
| 62 | + if (self::getInstance()->toNext($param, $value)) { |
|
| 63 | 63 | $array = self::getInstance()->testArray($param, self::getInstance()->data($param)); |
| 64 | - if(count($array) > $value){ |
|
| 64 | + if (count($array)>$value) { |
|
| 65 | 65 | self::getInstance()->error([ |
| 66 | 66 | $param => 'ultrapassou o esperado' |
| 67 | 67 | ]); |
@@ -71,15 +71,15 @@ discard block |
||
| 71 | 71 | |
| 72 | 72 | protected function checkEquals(string $param, $value): void |
| 73 | 73 | { |
| 74 | - if(self::getInstance()->toNext($param, $value)){ |
|
| 74 | + if (self::getInstance()->toNext($param, $value)) { |
|
| 75 | 75 | |
| 76 | - if(!array_key_exists($param, self::getInstance()->data())){ |
|
| 76 | + if (!array_key_exists($param, self::getInstance()->data())) { |
|
| 77 | 77 | self::getInstance()->error([ |
| 78 | 78 | $param => "O servidor não encontrou a informação '{$value}' para ser comparada" |
| 79 | 79 | ]); |
| 80 | 80 | } |
| 81 | 81 | |
| 82 | - if(self::getInstance()->data($param) != self::getInstance()->data($value)){ |
|
| 82 | + if (self::getInstance()->data($param)!=self::getInstance()->data($value)) { |
|
| 83 | 83 | self::getInstance()->error([ |
| 84 | 84 | $param => 'está diferente de '.ucfirst($value) |
| 85 | 85 | ]); |
@@ -90,13 +90,13 @@ discard block |
||
| 90 | 90 | |
| 91 | 91 | protected function checkMaxlength(string $param, $value) |
| 92 | 92 | { |
| 93 | - if(self::getInstance()->toNext($param, $value)){ |
|
| 93 | + if (self::getInstance()->toNext($param, $value)) { |
|
| 94 | 94 | |
| 95 | 95 | $realval = (is_array(self::getInstance()->data($param))) ? self::getInstance()->data($param) : [self::getInstance()->data($param)]; |
| 96 | 96 | |
| 97 | - foreach($realval as $val){ |
|
| 97 | + foreach ($realval as $val) { |
|
| 98 | 98 | |
| 99 | - if($value < strlen($val)) { |
|
| 99 | + if ($value<strlen($val)) { |
|
| 100 | 100 | self::getInstance()->error([ |
| 101 | 101 | $param => 'ultrapassou o máximo de caracteres esperado' |
| 102 | 102 | ]); |
@@ -108,11 +108,11 @@ discard block |
||
| 108 | 108 | |
| 109 | 109 | protected function checkType(string $param, $value) |
| 110 | 110 | { |
| 111 | - if(self::getInstance()->toNext($param, $value)){ |
|
| 111 | + if (self::getInstance()->toNext($param, $value)) { |
|
| 112 | 112 | |
| 113 | 113 | switch ($value) { |
| 114 | 114 | case 'date': |
| 115 | - if(!self::getInstance()->validateDate(self::getInstance()->data($param) , 'd/m/Y')){ |
|
| 115 | + if (!self::getInstance()->validateDate(self::getInstance()->data($param), 'd/m/Y')) { |
|
| 116 | 116 | self::getInstance()->error([ |
| 117 | 117 | $param => 'não é uma data válida' |
| 118 | 118 | ]); |
@@ -124,9 +124,9 @@ discard block |
||
| 124 | 124 | |
| 125 | 125 | protected function checkFilter(string $param, $value) |
| 126 | 126 | { |
| 127 | - if(self::getInstance()->toNext($param, $value)){ |
|
| 127 | + if (self::getInstance()->toNext($param, $value)) { |
|
| 128 | 128 | |
| 129 | - if(!filter_var(self::getInstance()->data($param), $value)){ |
|
| 129 | + if (!filter_var(self::getInstance()->data($param), $value)) { |
|
| 130 | 130 | self::getInstance()->error([ |
| 131 | 131 | $param => 'não passou pela filtragem de dados' |
| 132 | 132 | ]); |