@@ -12,23 +12,23 @@ discard block |
||
12 | 12 | public static function add(object $model,object $return): void |
13 | 13 | { |
14 | 14 | self::$validators[get_class($model)] = $return($Rules = new Rules($model)); |
15 | - } |
|
15 | + } |
|
16 | 16 | |
17 | 17 | public static function execute(array $datas): bool |
18 | 18 | { |
19 | - if(!array_key_exists('data', $datas)){ |
|
19 | + if(!array_key_exists('data', $datas)){ |
|
20 | 20 | throw new Exception('Informações cruciais não foram recebidas.'); |
21 | 21 | } |
22 | 22 | |
23 | - if(gettype($datas['data']) != 'string' || @json_decode($datas['data']) === null){ |
|
23 | + if(gettype($datas['data']) != 'string' || @json_decode($datas['data']) === null){ |
|
24 | 24 | throw new Exception('O servidor recebeu as informações no formato esperado.'); |
25 | 25 | } |
26 | 26 | |
27 | - if(!array_key_exists('provider',$datas) || is_null($datas['provider'])){ |
|
27 | + if(!array_key_exists('provider',$datas) || is_null($datas['provider'])){ |
|
28 | 28 | throw new Exception('O servidor não recebeu o ID do formulário.'); |
29 | 29 | } |
30 | 30 | |
31 | - if(!array_key_exists('role',$datas) || is_null($datas['role'])){ |
|
31 | + if(!array_key_exists('role',$datas) || is_null($datas['role'])){ |
|
32 | 32 | throw new Exception('O servidor não conseguiu identificar a finalidade deste formulário.'); |
33 | 33 | } |
34 | 34 | |
@@ -44,57 +44,57 @@ discard block |
||
44 | 44 | |
45 | 45 | $class = 'HnrAzevedo\\Validator\\'.ucfirst($datas['provider']); |
46 | 46 | |
47 | - if(!class_exists($class)){ |
|
47 | + if(!class_exists($class)){ |
|
48 | 48 | throw new Exception("Form ID {$class} inválido."); |
49 | 49 | } |
50 | 50 | |
51 | 51 | $rules = new $class(); |
52 | 52 | |
53 | - if(get_class(self::$validators[get_class($rules)]) !== 'HnrAzevedo\Validator\Rules'){ |
|
53 | + if(get_class(self::$validators[get_class($rules)]) !== 'HnrAzevedo\Validator\Rules'){ |
|
54 | 54 | throw new Exception('Ocorreu algum erro e o servidor não pode identificar o responsável por validar o formulário submetido.'); |
55 | 55 | } |
56 | 56 | |
57 | - if(empty(self::$validators[get_class($rules)]->getRules($role))){ |
|
57 | + if(empty(self::$validators[get_class($rules)]->getRules($role))){ |
|
58 | 58 | throw new Exception('Não existe regras para validar este formulário.'); |
59 | 59 | } |
60 | 60 | |
61 | - $validators = self::$validators[get_class($rules)]->getRules($role); |
|
61 | + $validators = self::$validators[get_class($rules)]->getRules($role); |
|
62 | 62 | |
63 | 63 | $tests = 0; |
64 | 64 | |
65 | - foreach ($validators as $key => $value) { |
|
66 | - $tests = (array_key_exists('required',$value) and $value['required']===true) ? $tests+1 : $tests; |
|
67 | - } |
|
65 | + foreach ($validators as $key => $value) { |
|
66 | + $tests = (array_key_exists('required',$value) and $value['required']===true) ? $tests+1 : $tests; |
|
67 | + } |
|
68 | 68 | |
69 | - $testeds = 0; |
|
69 | + $testeds = 0; |
|
70 | 70 | |
71 | - foreach ($validators as $key => $value) { |
|
71 | + foreach ($validators as $key => $value) { |
|
72 | 72 | |
73 | - foreach ($data as $keyy => $valuee) { |
|
73 | + foreach ($data as $keyy => $valuee) { |
|
74 | 74 | |
75 | 75 | $v = $valuee; |
76 | 76 | |
77 | - if(is_array($valuee)){ |
|
78 | - $v = null; |
|
79 | - foreach ($valuee as $vvv) { |
|
80 | - $v .= $vvv; |
|
81 | - } |
|
77 | + if(is_array($valuee)){ |
|
78 | + $v = null; |
|
79 | + foreach ($valuee as $vvv) { |
|
80 | + $v .= $vvv; |
|
81 | + } |
|
82 | 82 | } |
83 | 83 | |
84 | - $valuee = $v; |
|
84 | + $valuee = $v; |
|
85 | 85 | |
86 | - if(!array_key_exists($keyy, $validators)){ |
|
86 | + if(!array_key_exists($keyy, $validators)){ |
|
87 | 87 | throw new Exception("O campo '{$keyy}' não é esperado para está operação."); |
88 | 88 | } |
89 | 89 | |
90 | - if($keyy===$key){ |
|
90 | + if($keyy===$key){ |
|
91 | 91 | |
92 | 92 | $testeds++; |
93 | 93 | |
94 | - foreach ($value as $subkey => $subvalue) { |
|
94 | + foreach ($value as $subkey => $subvalue) { |
|
95 | 95 | |
96 | - switch ($subkey) { |
|
97 | - case 'minlength': |
|
96 | + switch ($subkey) { |
|
97 | + case 'minlength': |
|
98 | 98 | if(array_key_exists('required', $value)){ |
99 | 99 | if($value['required'] or strlen($valuee)!==0){ |
100 | 100 | if(strlen($valuee)===0){ |
@@ -124,9 +124,9 @@ discard block |
||
124 | 124 | } |
125 | 125 | } |
126 | 126 | } |
127 | - break; |
|
127 | + break; |
|
128 | 128 | |
129 | - case 'maxlength': |
|
129 | + case 'maxlength': |
|
130 | 130 | if(array_key_exists('required', $value)){ |
131 | 131 | if($value['required'] or strlen($valuee)!==0){ |
132 | 132 | if(strlen($valuee)>(int)$subvalue){ |
@@ -136,7 +136,7 @@ discard block |
||
136 | 136 | } |
137 | 137 | break; |
138 | 138 | |
139 | - case 'regex': |
|
139 | + case 'regex': |
|
140 | 140 | if(array_key_exists('required', $value)){ |
141 | 141 | if($value['required'] or strlen($valuee)!==0){ |
142 | 142 | if(!@preg_match($subvalue,$valuee)){ |
@@ -146,7 +146,7 @@ discard block |
||
146 | 146 | } |
147 | 147 | break; |
148 | 148 | |
149 | - case 'equals': |
|
149 | + case 'equals': |
|
150 | 150 | $equals = false; |
151 | 151 | foreach ($data as $ke => $sub) { |
152 | 152 | if($ke===$subvalue){ |
@@ -159,18 +159,18 @@ discard block |
||
159 | 159 | throw new Exception("O servidor não encontrou a informação '{$subvalue}' para ser comparada a '{$key}'.",1); |
160 | 160 | } |
161 | 161 | break; |
162 | - } |
|
163 | - } |
|
164 | - } |
|
165 | - } |
|
162 | + } |
|
163 | + } |
|
164 | + } |
|
165 | + } |
|
166 | 166 | } |
167 | 167 | |
168 | - if($tests>$testeds){ |
|
168 | + if($tests>$testeds){ |
|
169 | 169 | throw new Exception('Alguma informação necessária não pode ser validada.'); |
170 | 170 | } |
171 | 171 | |
172 | - return true; |
|
173 | - } |
|
172 | + return true; |
|
173 | + } |
|
174 | 174 | |
175 | 175 | public static function toJson(array $request): string |
176 | 176 | { |
@@ -184,38 +184,38 @@ discard block |
||
184 | 184 | |
185 | 185 | $class = 'HnrAzevedo\\Validator\\'.ucfirst($request['provider']); |
186 | 186 | |
187 | - if(!class_exists($class)){ |
|
187 | + if(!class_exists($class)){ |
|
188 | 188 | throw new Exception("Form ID {$class} inválido."); |
189 | 189 | } |
190 | 190 | |
191 | - $model = new $class(); |
|
191 | + $model = new $class(); |
|
192 | 192 | |
193 | - if(get_class(self::$validators[get_class($model)]) !== 'HnrAzevedo\Validator\Rules'){ |
|
193 | + if(get_class(self::$validators[get_class($model)]) !== 'HnrAzevedo\Validator\Rules'){ |
|
194 | 194 | throw new Exception('Ocorreu algum erro e o servidor não pode identificar o responsável por validar o formulário submetido.'); |
195 | 195 | } |
196 | 196 | |
197 | - if(empty(self::$validators[get_class($model)]->getRules($request['role']))){ |
|
197 | + if(empty(self::$validators[get_class($model)]->getRules($request['role']))){ |
|
198 | 198 | throw new Exception('Formulário desativado: Não existem regras para validar este formulário.'); |
199 | 199 | } |
200 | 200 | |
201 | - $validators = self::$validators[get_class($model)]->getRules($request['role']); |
|
201 | + $validators = self::$validators[get_class($model)]->getRules($request['role']); |
|
202 | 202 | |
203 | 203 | /* For function to validate information in javascript */ |
204 | 204 | $response = '{'; |
205 | - foreach ($validators as $field => $rules) { |
|
206 | - $response .= $field.':{'; |
|
207 | - foreach(array_reverse($rules) as $rule => $value){ |
|
208 | - $value = (gettype($value)==='string') ? '\''.$value.'\'' : $value; |
|
209 | - if(gettype($value)==='boolean'){ |
|
205 | + foreach ($validators as $field => $rules) { |
|
206 | + $response .= $field.':{'; |
|
207 | + foreach(array_reverse($rules) as $rule => $value){ |
|
208 | + $value = (gettype($value)==='string') ? '\''.$value.'\'' : $value; |
|
209 | + if(gettype($value)==='boolean'){ |
|
210 | 210 | $value = ($value) ? 'true' : 'false'; |
211 | 211 | } |
212 | - $value = ($rule=='regex') ? str_replace('\\','\\\\','\''.substr($value,2,strlen($value)-4).'\'') : $value; |
|
213 | - $response .= $rule.':'.$value.','; |
|
214 | - } |
|
215 | - $response .='},'; |
|
212 | + $value = ($rule=='regex') ? str_replace('\\','\\\\','\''.substr($value,2,strlen($value)-4).'\'') : $value; |
|
213 | + $response .= $rule.':'.$value.','; |
|
214 | + } |
|
215 | + $response .='},'; |
|
216 | 216 | } |
217 | 217 | |
218 | 218 | $response = substr(str_replace(',}','}',$response),0,-1).'}'; |
219 | - return $response; |
|
220 | - } |
|
219 | + return $response; |
|
220 | + } |
|
221 | 221 | } |
@@ -5,30 +5,30 @@ discard block |
||
5 | 5 | use HnrAzevedo\Validator\Rules; |
6 | 6 | use Exception; |
7 | 7 | |
8 | -Class Validator{ |
|
8 | +Class Validator { |
|
9 | 9 | |
10 | 10 | private static array $validators = array(); |
11 | 11 | |
12 | - public static function add(object $model,object $return): void |
|
12 | + public static function add(object $model, object $return): void |
|
13 | 13 | { |
14 | 14 | self::$validators[get_class($model)] = $return($Rules = new Rules($model)); |
15 | 15 | } |
16 | 16 | |
17 | 17 | public static function execute(array $datas): bool |
18 | 18 | { |
19 | - if(!array_key_exists('data', $datas)){ |
|
19 | + if (!array_key_exists('data', $datas)) { |
|
20 | 20 | throw new Exception('Informações cruciais não foram recebidas.'); |
21 | 21 | } |
22 | 22 | |
23 | - if(gettype($datas['data']) != 'string' || @json_decode($datas['data']) === null){ |
|
23 | + if (gettype($datas['data'])!='string' || @json_decode($datas['data'])===null) { |
|
24 | 24 | throw new Exception('O servidor recebeu as informações no formato esperado.'); |
25 | 25 | } |
26 | 26 | |
27 | - if(!array_key_exists('provider',$datas) || is_null($datas['provider'])){ |
|
27 | + if (!array_key_exists('provider', $datas) || is_null($datas['provider'])) { |
|
28 | 28 | throw new Exception('O servidor não recebeu o ID do formulário.'); |
29 | 29 | } |
30 | 30 | |
31 | - if(!array_key_exists('role',$datas) || is_null($datas['role'])){ |
|
31 | + if (!array_key_exists('role', $datas) || is_null($datas['role'])) { |
|
32 | 32 | throw new Exception('O servidor não conseguiu identificar a finalidade deste formulário.'); |
33 | 33 | } |
34 | 34 | |
@@ -38,23 +38,23 @@ discard block |
||
38 | 38 | |
39 | 39 | $file = (VALIDATOR_CONFIG['path'].ucfirst($datas['provider']).'.php'); |
40 | 40 | |
41 | - if(file_exists($file)){ |
|
41 | + if (file_exists($file)) { |
|
42 | 42 | require_once($file); |
43 | 43 | } |
44 | 44 | |
45 | 45 | $class = 'HnrAzevedo\\Validator\\'.ucfirst($datas['provider']); |
46 | 46 | |
47 | - if(!class_exists($class)){ |
|
47 | + if (!class_exists($class)) { |
|
48 | 48 | throw new Exception("Form ID {$class} inválido."); |
49 | 49 | } |
50 | 50 | |
51 | 51 | $rules = new $class(); |
52 | 52 | |
53 | - if(get_class(self::$validators[get_class($rules)]) !== 'HnrAzevedo\Validator\Rules'){ |
|
53 | + if (get_class(self::$validators[get_class($rules)])!=='HnrAzevedo\Validator\Rules') { |
|
54 | 54 | throw new Exception('Ocorreu algum erro e o servidor não pode identificar o responsável por validar o formulário submetido.'); |
55 | 55 | } |
56 | 56 | |
57 | - if(empty(self::$validators[get_class($rules)]->getRules($role))){ |
|
57 | + if (empty(self::$validators[get_class($rules)]->getRules($role))) { |
|
58 | 58 | throw new Exception('Não existe regras para validar este formulário.'); |
59 | 59 | } |
60 | 60 | |
@@ -63,7 +63,7 @@ discard block |
||
63 | 63 | $tests = 0; |
64 | 64 | |
65 | 65 | foreach ($validators as $key => $value) { |
66 | - $tests = (array_key_exists('required',$value) and $value['required']===true) ? $tests+1 : $tests; |
|
66 | + $tests = (array_key_exists('required', $value) and $value['required']===true) ? $tests+1 : $tests; |
|
67 | 67 | } |
68 | 68 | |
69 | 69 | $testeds = 0; |
@@ -74,7 +74,7 @@ discard block |
||
74 | 74 | |
75 | 75 | $v = $valuee; |
76 | 76 | |
77 | - if(is_array($valuee)){ |
|
77 | + if (is_array($valuee)) { |
|
78 | 78 | $v = null; |
79 | 79 | foreach ($valuee as $vvv) { |
80 | 80 | $v .= $vvv; |
@@ -83,11 +83,11 @@ discard block |
||
83 | 83 | |
84 | 84 | $valuee = $v; |
85 | 85 | |
86 | - if(!array_key_exists($keyy, $validators)){ |
|
86 | + if (!array_key_exists($keyy, $validators)) { |
|
87 | 87 | throw new Exception("O campo '{$keyy}' não é esperado para está operação."); |
88 | 88 | } |
89 | 89 | |
90 | - if($keyy===$key){ |
|
90 | + if ($keyy===$key) { |
|
91 | 91 | |
92 | 92 | $testeds++; |
93 | 93 | |
@@ -95,30 +95,30 @@ discard block |
||
95 | 95 | |
96 | 96 | switch ($subkey) { |
97 | 97 | case 'minlength': |
98 | - if(array_key_exists('required', $value)){ |
|
99 | - if($value['required'] or strlen($valuee)!==0){ |
|
100 | - if(strlen($valuee)===0){ |
|
101 | - throw new Exception("O campo '{$key}' é obrigatório.",1); |
|
98 | + if (array_key_exists('required', $value)) { |
|
99 | + if ($value['required'] or strlen($valuee)!==0) { |
|
100 | + if (strlen($valuee)===0) { |
|
101 | + throw new Exception("O campo '{$key}' é obrigatório.", 1); |
|
102 | 102 | } |
103 | 103 | |
104 | - if(strlen($valuee) < (int) $subvalue){ |
|
105 | - throw new Exception("{$key} não atingiu o mínimo de caracteres esperado.",1); |
|
104 | + if (strlen($valuee)<(int) $subvalue) { |
|
105 | + throw new Exception("{$key} não atingiu o mínimo de caracteres esperado.", 1); |
|
106 | 106 | } |
107 | 107 | } |
108 | 108 | } |
109 | 109 | break; |
110 | 110 | |
111 | 111 | case 'type': |
112 | - if(array_key_exists('required', $value)){ |
|
113 | - if($value['required'] or strlen($valuee)!==0){ |
|
112 | + if (array_key_exists('required', $value)) { |
|
113 | + if ($value['required'] or strlen($valuee)!==0) { |
|
114 | 114 | switch ($subvalue) { |
115 | 115 | case 'date': |
116 | 116 | $date = explode('/', $valuee); |
117 | - if(count($date) != 3){ |
|
118 | - throw new Exception('Data inválida.',1); |
|
117 | + if (count($date)!=3) { |
|
118 | + throw new Exception('Data inválida.', 1); |
|
119 | 119 | } |
120 | - if(!@checkdate(intval($date[1]), intval($date[0]), intval($date[2]) )){ |
|
121 | - throw new Exception('Data inválida.',1); |
|
120 | + if (!@checkdate(intval($date[1]), intval($date[0]), intval($date[2]))) { |
|
121 | + throw new Exception('Data inválida.', 1); |
|
122 | 122 | } |
123 | 123 | break; |
124 | 124 | } |
@@ -127,20 +127,20 @@ discard block |
||
127 | 127 | break; |
128 | 128 | |
129 | 129 | case 'maxlength': |
130 | - if(array_key_exists('required', $value)){ |
|
131 | - if($value['required'] or strlen($valuee)!==0){ |
|
132 | - if(strlen($valuee)>(int)$subvalue){ |
|
133 | - throw new Exception("{$key} ultrapassou o limite de caracteres permitidos.",1); |
|
130 | + if (array_key_exists('required', $value)) { |
|
131 | + if ($value['required'] or strlen($valuee)!==0) { |
|
132 | + if (strlen($valuee)>(int) $subvalue) { |
|
133 | + throw new Exception("{$key} ultrapassou o limite de caracteres permitidos.", 1); |
|
134 | 134 | } |
135 | 135 | } |
136 | 136 | } |
137 | 137 | break; |
138 | 138 | |
139 | 139 | case 'regex': |
140 | - if(array_key_exists('required', $value)){ |
|
141 | - if($value['required'] or strlen($valuee)!==0){ |
|
142 | - if(!@preg_match($subvalue,$valuee)){ |
|
143 | - throw new Exception("{$key} inválido(a).",1); |
|
140 | + if (array_key_exists('required', $value)) { |
|
141 | + if ($value['required'] or strlen($valuee)!==0) { |
|
142 | + if (!@preg_match($subvalue, $valuee)) { |
|
143 | + throw new Exception("{$key} inválido(a).", 1); |
|
144 | 144 | } |
145 | 145 | } |
146 | 146 | } |
@@ -149,14 +149,14 @@ discard block |
||
149 | 149 | case 'equals': |
150 | 150 | $equals = false; |
151 | 151 | foreach ($data as $ke => $sub) { |
152 | - if($ke===$subvalue){ |
|
153 | - $equals=true; |
|
154 | - if($valuee!==$sub) |
|
155 | - throw new \Exception(ucfirst($key).' está diferente de '.ucfirst($ke),1); |
|
152 | + if ($ke===$subvalue) { |
|
153 | + $equals = true; |
|
154 | + if ($valuee!==$sub) |
|
155 | + throw new \Exception(ucfirst($key).' está diferente de '.ucfirst($ke), 1); |
|
156 | 156 | } |
157 | 157 | } |
158 | - if(!$equals){ |
|
159 | - throw new Exception("O servidor não encontrou a informação '{$subvalue}' para ser comparada a '{$key}'.",1); |
|
158 | + if (!$equals) { |
|
159 | + throw new Exception("O servidor não encontrou a informação '{$subvalue}' para ser comparada a '{$key}'.", 1); |
|
160 | 160 | } |
161 | 161 | break; |
162 | 162 | } |
@@ -165,7 +165,7 @@ discard block |
||
165 | 165 | } |
166 | 166 | } |
167 | 167 | |
168 | - if($tests>$testeds){ |
|
168 | + if ($tests>$testeds) { |
|
169 | 169 | throw new Exception('Alguma informação necessária não pode ser validada.'); |
170 | 170 | } |
171 | 171 | |
@@ -177,24 +177,24 @@ discard block |
||
177 | 177 | $file = (VALIDATOR_CONFIG['path'].ucfirst($request['provider']).'.php'); |
178 | 178 | $file = __DIR__.'/../../../../'.$file; |
179 | 179 | |
180 | - if(file_exists($file)){ |
|
180 | + if (file_exists($file)) { |
|
181 | 181 | require_once($file); |
182 | 182 | } |
183 | 183 | |
184 | 184 | |
185 | 185 | $class = 'HnrAzevedo\\Validator\\'.ucfirst($request['provider']); |
186 | 186 | |
187 | - if(!class_exists($class)){ |
|
187 | + if (!class_exists($class)) { |
|
188 | 188 | throw new Exception("Form ID {$class} inválido."); |
189 | 189 | } |
190 | 190 | |
191 | 191 | $model = new $class(); |
192 | 192 | |
193 | - if(get_class(self::$validators[get_class($model)]) !== 'HnrAzevedo\Validator\Rules'){ |
|
193 | + if (get_class(self::$validators[get_class($model)])!=='HnrAzevedo\Validator\Rules') { |
|
194 | 194 | throw new Exception('Ocorreu algum erro e o servidor não pode identificar o responsável por validar o formulário submetido.'); |
195 | 195 | } |
196 | 196 | |
197 | - if(empty(self::$validators[get_class($model)]->getRules($request['role']))){ |
|
197 | + if (empty(self::$validators[get_class($model)]->getRules($request['role']))) { |
|
198 | 198 | throw new Exception('Formulário desativado: Não existem regras para validar este formulário.'); |
199 | 199 | } |
200 | 200 | |
@@ -204,18 +204,18 @@ discard block |
||
204 | 204 | $response = '{'; |
205 | 205 | foreach ($validators as $field => $rules) { |
206 | 206 | $response .= $field.':{'; |
207 | - foreach(array_reverse($rules) as $rule => $value){ |
|
207 | + foreach (array_reverse($rules) as $rule => $value) { |
|
208 | 208 | $value = (gettype($value)==='string') ? '\''.$value.'\'' : $value; |
209 | - if(gettype($value)==='boolean'){ |
|
209 | + if (gettype($value)==='boolean') { |
|
210 | 210 | $value = ($value) ? 'true' : 'false'; |
211 | 211 | } |
212 | - $value = ($rule=='regex') ? str_replace('\\','\\\\','\''.substr($value,2,strlen($value)-4).'\'') : $value; |
|
212 | + $value = ($rule=='regex') ? str_replace('\\', '\\\\', '\''.substr($value, 2, strlen($value)-4).'\'') : $value; |
|
213 | 213 | $response .= $rule.':'.$value.','; |
214 | 214 | } |
215 | - $response .='},'; |
|
215 | + $response .= '},'; |
|
216 | 216 | } |
217 | 217 | |
218 | - $response = substr(str_replace(',}','}',$response),0,-1).'}'; |
|
218 | + $response = substr(str_replace(',}', '}', $response), 0, -1).'}'; |
|
219 | 219 | return $response; |
220 | 220 | } |
221 | 221 | } |