@@ -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; |
@@ -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 | } |
@@ -12,25 +12,25 @@ 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 | - $field_error = null; |
|
| 19 | + $field_error = null; |
|
| 20 | 20 | |
| 21 | - if(!array_key_exists('data', $datas)){ |
|
| 21 | + if(!array_key_exists('data', $datas)){ |
|
| 22 | 22 | throw new Exception('Informações cruciais não foram recebidas.'); |
| 23 | 23 | } |
| 24 | 24 | |
| 25 | - if(gettype($datas['data']) != 'string' || @json_decode($datas['data']) === null){ |
|
| 25 | + if(gettype($datas['data']) != 'string' || @json_decode($datas['data']) === null){ |
|
| 26 | 26 | throw new Exception('O servidor recebeu as informações no formato esperado.'); |
| 27 | 27 | } |
| 28 | 28 | |
| 29 | - if(!array_key_exists('provider',$datas) || is_null($datas['provider'])){ |
|
| 29 | + if(!array_key_exists('provider',$datas) || is_null($datas['provider'])){ |
|
| 30 | 30 | throw new Exception('O servidor não recebeu o ID do formulário.'); |
| 31 | 31 | } |
| 32 | 32 | |
| 33 | - if(!array_key_exists('role',$datas) || is_null($datas['role'])){ |
|
| 33 | + if(!array_key_exists('role',$datas) || is_null($datas['role'])){ |
|
| 34 | 34 | throw new Exception('O servidor não conseguiu identificar a finalidade deste formulário.'); |
| 35 | 35 | } |
| 36 | 36 | |
@@ -46,59 +46,59 @@ discard block |
||
| 46 | 46 | |
| 47 | 47 | $class = 'HnrAzevedo\\Validator\\'.ucfirst($datas['provider']); |
| 48 | 48 | |
| 49 | - if(!class_exists($class)){ |
|
| 49 | + if(!class_exists($class)){ |
|
| 50 | 50 | throw new Exception("Form ID {$class} inválido."); |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | $rules = new $class(); |
| 54 | 54 | |
| 55 | - if(get_class(self::$validators[get_class($rules)]) !== 'HnrAzevedo\Validator\Rules'){ |
|
| 55 | + if(get_class(self::$validators[get_class($rules)]) !== 'HnrAzevedo\Validator\Rules'){ |
|
| 56 | 56 | throw new Exception('Ocorreu algum erro e o servidor não pode identificar o responsável por validar o formulário submetido.'); |
| 57 | 57 | } |
| 58 | 58 | |
| 59 | - if(empty(self::$validators[get_class($rules)]->getRules($role))){ |
|
| 59 | + if(empty(self::$validators[get_class($rules)]->getRules($role))){ |
|
| 60 | 60 | throw new Exception('Não existe regras para validar este formulário.'); |
| 61 | 61 | } |
| 62 | 62 | |
| 63 | - $validators = self::$validators[get_class($rules)]->getRules($role); |
|
| 63 | + $validators = self::$validators[get_class($rules)]->getRules($role); |
|
| 64 | 64 | |
| 65 | 65 | $tests = 0; |
| 66 | 66 | |
| 67 | - foreach ($validators as $key => $value) { |
|
| 68 | - $tests = (array_key_exists('required',$value) and $value['required']===true) ? $tests+1 : $tests; |
|
| 69 | - } |
|
| 67 | + foreach ($validators as $key => $value) { |
|
| 68 | + $tests = (array_key_exists('required',$value) and $value['required']===true) ? $tests+1 : $tests; |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - $testeds = 0; |
|
| 71 | + $testeds = 0; |
|
| 72 | 72 | |
| 73 | - foreach ($validators as $key => $value) { |
|
| 73 | + foreach ($validators as $key => $value) { |
|
| 74 | 74 | |
| 75 | - foreach ($data as $keyy => $valuee) { |
|
| 75 | + foreach ($data as $keyy => $valuee) { |
|
| 76 | 76 | |
| 77 | 77 | $v = $valuee; |
| 78 | 78 | |
| 79 | - if(is_array($valuee)){ |
|
| 80 | - $v = null; |
|
| 81 | - foreach ($valuee as $vvv) { |
|
| 82 | - $v .= $vvv; |
|
| 83 | - } |
|
| 79 | + if(is_array($valuee)){ |
|
| 80 | + $v = null; |
|
| 81 | + foreach ($valuee as $vvv) { |
|
| 82 | + $v .= $vvv; |
|
| 83 | + } |
|
| 84 | 84 | } |
| 85 | 85 | |
| 86 | - $valuee = $v; |
|
| 86 | + $valuee = $v; |
|
| 87 | 87 | |
| 88 | - if(!array_key_exists($keyy, $validators)){ |
|
| 88 | + if(!array_key_exists($keyy, $validators)){ |
|
| 89 | 89 | throw new Exception("O campo '{$keyy}' não é esperado para está operação."); |
| 90 | 90 | } |
| 91 | 91 | |
| 92 | - if($keyy===$key){ |
|
| 92 | + if($keyy===$key){ |
|
| 93 | 93 | |
| 94 | 94 | $testeds++; |
| 95 | 95 | |
| 96 | - foreach ($value as $subkey => $subvalue) { |
|
| 96 | + foreach ($value as $subkey => $subvalue) { |
|
| 97 | 97 | |
| 98 | - $field_error = $key; |
|
| 98 | + $field_error = $key; |
|
| 99 | 99 | |
| 100 | - switch ($subkey) { |
|
| 101 | - case 'minlength': |
|
| 100 | + switch ($subkey) { |
|
| 101 | + case 'minlength': |
|
| 102 | 102 | if(array_key_exists('required', $value)){ |
| 103 | 103 | if($value['required'] or strlen($valuee)!==0){ |
| 104 | 104 | if(strlen($valuee)===0){ |
@@ -128,9 +128,9 @@ discard block |
||
| 128 | 128 | } |
| 129 | 129 | } |
| 130 | 130 | } |
| 131 | - break; |
|
| 131 | + break; |
|
| 132 | 132 | |
| 133 | - case 'maxlength': |
|
| 133 | + case 'maxlength': |
|
| 134 | 134 | if(array_key_exists('required', $value)){ |
| 135 | 135 | if($value['required'] or strlen($valuee)!==0){ |
| 136 | 136 | if(strlen($valuee)>(int)$subvalue){ |
@@ -140,7 +140,7 @@ discard block |
||
| 140 | 140 | } |
| 141 | 141 | break; |
| 142 | 142 | |
| 143 | - case 'regex': |
|
| 143 | + case 'regex': |
|
| 144 | 144 | if(array_key_exists('required', $value)){ |
| 145 | 145 | if($value['required'] or strlen($valuee)!==0){ |
| 146 | 146 | if(!@preg_match($subvalue,$valuee)){ |
@@ -150,7 +150,7 @@ discard block |
||
| 150 | 150 | } |
| 151 | 151 | break; |
| 152 | 152 | |
| 153 | - case 'equals': |
|
| 153 | + case 'equals': |
|
| 154 | 154 | $equals = false; |
| 155 | 155 | foreach ($data as $ke => $sub) { |
| 156 | 156 | if($ke===$subvalue){ |
@@ -163,18 +163,18 @@ discard block |
||
| 163 | 163 | throw new Exception("O servidor não encontrou a informação '{$subvalue}' para ser comparada a '{$key}'.",1); |
| 164 | 164 | } |
| 165 | 165 | break; |
| 166 | - } |
|
| 167 | - } |
|
| 168 | - } |
|
| 169 | - } |
|
| 166 | + } |
|
| 167 | + } |
|
| 168 | + } |
|
| 169 | + } |
|
| 170 | 170 | } |
| 171 | 171 | |
| 172 | - if($tests>$testeds){ |
|
| 172 | + if($tests>$testeds){ |
|
| 173 | 173 | throw new Exception('Alguma informação necessária não pode ser validada.'); |
| 174 | 174 | } |
| 175 | 175 | |
| 176 | - return true; |
|
| 177 | - } |
|
| 176 | + return true; |
|
| 177 | + } |
|
| 178 | 178 | |
| 179 | 179 | public static function toJson(array $request): string |
| 180 | 180 | { |
@@ -188,38 +188,38 @@ discard block |
||
| 188 | 188 | |
| 189 | 189 | $class = 'HnrAzevedo\\Validator\\'.ucfirst($request['provider']); |
| 190 | 190 | |
| 191 | - if(!class_exists($class)){ |
|
| 191 | + if(!class_exists($class)){ |
|
| 192 | 192 | throw new Exception("Form ID {$class} inválido."); |
| 193 | 193 | } |
| 194 | 194 | |
| 195 | - $model = new $class(); |
|
| 195 | + $model = new $class(); |
|
| 196 | 196 | |
| 197 | - if(get_class(self::$validators[get_class($model)]) !== 'HnrAzevedo\Validator\Rules'){ |
|
| 197 | + if(get_class(self::$validators[get_class($model)]) !== 'HnrAzevedo\Validator\Rules'){ |
|
| 198 | 198 | throw new Exception('Ocorreu algum erro e o servidor não pode identificar o responsável por validar o formulário submetido.'); |
| 199 | 199 | } |
| 200 | 200 | |
| 201 | - if(empty(self::$validators[get_class($model)]->getRules($request['role']))){ |
|
| 201 | + if(empty(self::$validators[get_class($model)]->getRules($request['role']))){ |
|
| 202 | 202 | throw new Exception('Formulário desativado: Não existem regras para validar este formulário.'); |
| 203 | 203 | } |
| 204 | 204 | |
| 205 | - $validators = self::$validators[get_class($model)]->getRules($request['role']); |
|
| 205 | + $validators = self::$validators[get_class($model)]->getRules($request['role']); |
|
| 206 | 206 | |
| 207 | 207 | /* For function to validate information in javascript */ |
| 208 | 208 | $response = '{'; |
| 209 | - foreach ($validators as $field => $rules) { |
|
| 210 | - $response .= $field.':{'; |
|
| 211 | - foreach(array_reverse($rules) as $rule => $value){ |
|
| 212 | - $value = (gettype($value)==='string') ? '\''.$value.'\'' : $value; |
|
| 213 | - if(gettype($value)==='boolean'){ |
|
| 209 | + foreach ($validators as $field => $rules) { |
|
| 210 | + $response .= $field.':{'; |
|
| 211 | + foreach(array_reverse($rules) as $rule => $value){ |
|
| 212 | + $value = (gettype($value)==='string') ? '\''.$value.'\'' : $value; |
|
| 213 | + if(gettype($value)==='boolean'){ |
|
| 214 | 214 | $value = ($value) ? 'true' : 'false'; |
| 215 | 215 | } |
| 216 | - $value = ($rule=='regex') ? str_replace('\\','\\\\','\''.substr($value,2,strlen($value)-4).'\'') : $value; |
|
| 217 | - $response .= $rule.':'.$value.','; |
|
| 218 | - } |
|
| 219 | - $response .='},'; |
|
| 216 | + $value = ($rule=='regex') ? str_replace('\\','\\\\','\''.substr($value,2,strlen($value)-4).'\'') : $value; |
|
| 217 | + $response .= $rule.':'.$value.','; |
|
| 218 | + } |
|
| 219 | + $response .='},'; |
|
| 220 | 220 | } |
| 221 | 221 | |
| 222 | 222 | $response = substr(str_replace(',}','}',$response),0,-1).'}'; |
| 223 | - return $response; |
|
| 224 | - } |
|
| 223 | + return $response; |
|
| 224 | + } |
|
| 225 | 225 | } |