@@ -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,22 +100,22 @@ 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 | - if(!array_key_exists($keyy, (self::$validators[self::$model]->getRules(self::$data['role'])) )){ |
|
105 | + if(!array_key_exists($keyy, (self::$validators[self::$model]->getRules(self::$data['role'])) )){ |
|
106 | 106 | throw new Exception("O campo '{$keyy}' não é esperado para está operação."); |
107 | 107 | } |
108 | 108 | |
109 | - if($keyy===$key){ |
|
109 | + if($keyy===$key){ |
|
110 | 110 | |
111 | 111 | $validate++; |
112 | 112 | |
113 | - foreach ($value as $subkey => $subvalue) { |
|
113 | + foreach ($value as $subkey => $subvalue) { |
|
114 | 114 | $function = "check_{$subkey}"; |
115 | 115 | self::$function($keyy,$subvalue); |
116 | - } |
|
117 | - } |
|
118 | - } |
|
116 | + } |
|
117 | + } |
|
118 | + } |
|
119 | 119 | } |
120 | 120 | return $validate; |
121 | 121 | } |
@@ -133,7 +133,7 @@ discard block |
||
133 | 133 | |
134 | 134 | self::existRole(self::$model); |
135 | 135 | |
136 | - foreach ( self::$validators[self::$model]->getRules($request['role']) as $field => $r) { |
|
136 | + foreach ( self::$validators[self::$model]->getRules($request['role']) as $field => $r) { |
|
137 | 137 | |
138 | 138 | $response .= ("{$field}:".json_encode(array_reverse($r))).','; |
139 | 139 | |
@@ -144,6 +144,6 @@ discard block |
||
144 | 144 | $response = str_replace('{"','',$response); |
145 | 145 | $response = str_replace('":',':',$response); |
146 | 146 | |
147 | - return $response; |
|
148 | - } |
|
147 | + return $response; |
|
148 | + } |
|
149 | 149 | } |
@@ -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,21 +98,21 @@ 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 | - if(!array_key_exists($keyy, (self::$validators[self::$model]->getRules(self::$data['role'])) )){ |
|
105 | + if (!array_key_exists($keyy, (self::$validators[self::$model]->getRules(self::$data['role'])))) { |
|
106 | 106 | throw new Exception("O campo '{$keyy}' não é esperado para está operação."); |
107 | 107 | } |
108 | 108 | |
109 | - if($keyy===$key){ |
|
109 | + if ($keyy===$key) { |
|
110 | 110 | |
111 | 111 | $validate++; |
112 | 112 | |
113 | 113 | foreach ($value as $subkey => $subvalue) { |
114 | 114 | $function = "check_{$subkey}"; |
115 | - self::$function($keyy,$subvalue); |
|
115 | + self::$function($keyy, $subvalue); |
|
116 | 116 | } |
117 | 117 | } |
118 | 118 | } |
@@ -129,20 +129,20 @@ discard block |
||
129 | 129 | |
130 | 130 | self::includeValidations(); |
131 | 131 | |
132 | - self::$model = get_class( self::getClass('HnrAzevedo\\Validator\\'.ucfirst($request['provider'])) ); |
|
132 | + self::$model = get_class(self::getClass('HnrAzevedo\\Validator\\'.ucfirst($request['provider']))); |
|
133 | 133 | |
134 | 134 | self::existRole(self::$model); |
135 | 135 | |
136 | - foreach ( self::$validators[self::$model]->getRules($request['role']) as $field => $r) { |
|
136 | + foreach (self::$validators[self::$model]->getRules($request['role']) as $field => $r) { |
|
137 | 137 | |
138 | 138 | $response .= ("{$field}:".json_encode(array_reverse($r))).','; |
139 | 139 | |
140 | 140 | } |
141 | 141 | |
142 | - $response = '{'.substr($response,0,-1).'}'; |
|
143 | - $response = str_replace(',"',',',$response); |
|
144 | - $response = str_replace('{"','',$response); |
|
145 | - $response = str_replace('":',':',$response); |
|
142 | + $response = '{'.substr($response, 0, -1).'}'; |
|
143 | + $response = str_replace(',"', ',', $response); |
|
144 | + $response = str_replace('{"', '', $response); |
|
145 | + $response = str_replace('":', ':', $response); |
|
146 | 146 | |
147 | 147 | return $response; |
148 | 148 | } |
@@ -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::toNext($param,$value)){ |
|
15 | - if(strlen($value)===0){ |
|
16 | - throw new Exception("O campo '{$param}' é obrigatório.",1); |
|
14 | + if (self::toNext($param, $value)) { |
|
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::toNext($param,$value)){ |
|
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::toNext($param, $value)) { |
|
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::toNext($param,$value)){ |
|
36 | + if (self::toNext($param, $value)) { |
|
37 | 37 | |
38 | 38 | } |
39 | 39 | } |
40 | 40 | |
41 | 41 | protected static function check_equals(string $param, $value) |
42 | 42 | { |
43 | - if(self::toNext($param,$value)){ |
|
43 | + if (self::toNext($param, $value)) { |
|
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::toNext($param,$value)){ |
|
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::toNext($param, $value)) { |
|
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::toNext($param,$value)){ |
|
67 | + if (self::toNext($param, $value)) { |
|
68 | 68 | /* |
69 | 69 | var_dump($value); |
70 | 70 | switch ($value) { |
@@ -83,12 +83,12 @@ 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 | protected static function toNext(string $param, $value) |
90 | 90 | { |
91 | - return (self::check_required($param) || strlen($value > 0)); |
|
91 | + return (self::check_required($param) || strlen($value>0)); |
|
92 | 92 | } |
93 | 93 | |
94 | 94 | } |
95 | 95 | \ No newline at end of file |