@@ -9,11 +9,11 @@ |
||
9 | 9 | |
10 | 10 | Validator::add($this, function(Rules $rules){ |
11 | 11 | $rules->setAction('login') |
12 | - ->addField('email',['minlength'=>1,'regex'=>'/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/','required'=>true]) |
|
13 | - ->addField('password',['index'=>2,'minlength'=>6,'maxlength'=>20,'required'=>true]) |
|
14 | - ->addField('remember',['index'=>3,'minlength'=>2,'maxlength'=>2,'required'=>false]); |
|
12 | + ->addField('email',['minlength'=>1,'regex'=>'/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/','required'=>true]) |
|
13 | + ->addField('password',['index'=>2,'minlength'=>6,'maxlength'=>20,'required'=>true]) |
|
14 | + ->addField('remember',['index'=>3,'minlength'=>2,'maxlength'=>2,'required'=>false]); |
|
15 | 15 | |
16 | - return $rules; |
|
16 | + return $rules; |
|
17 | 17 | }); |
18 | 18 | |
19 | 19 | return $this; |
@@ -12,23 +12,23 @@ discard block |
||
12 | 12 | public static function add(object $model,callable $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 | } |
@@ -11,30 +11,30 @@ |
||
11 | 11 | |
12 | 12 | public function __construct(object $model) |
13 | 13 | { |
14 | - $this->form['model'] = ucfirst(get_class($model)); |
|
15 | - } |
|
14 | + $this->form['model'] = ucfirst(get_class($model)); |
|
15 | + } |
|
16 | 16 | |
17 | 17 | public function setAction(string $action): Rules |
18 | 18 | { |
19 | - $this->action = $action; |
|
20 | - return $this; |
|
21 | - } |
|
19 | + $this->action = $action; |
|
20 | + return $this; |
|
21 | + } |
|
22 | 22 | |
23 | 23 | public function addField(string $field, array $test): Rules |
24 | 24 | { |
25 | - if(empty($this->action)){ |
|
25 | + if(empty($this->action)){ |
|
26 | 26 | throw new Exception("Form action not registered."); |
27 | 27 | } |
28 | 28 | |
29 | - if(empty($this->form[$this->action][$field])){ |
|
29 | + if(empty($this->form[$this->action][$field])){ |
|
30 | 30 | $this->form[$this->action][$field] = $test; |
31 | 31 | } |
32 | 32 | |
33 | - return $this; |
|
34 | - } |
|
33 | + return $this; |
|
34 | + } |
|
35 | 35 | |
36 | 36 | public function getRules(string $action): ?array |
37 | 37 | { |
38 | - return (array_key_exists($action, $this->form)) ? $this->form[$action] : null; |
|
39 | - } |
|
38 | + return (array_key_exists($action, $this->form)) ? $this->form[$action] : null; |
|
39 | + } |
|
40 | 40 | } |