@@ -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 | } |
@@ -70,7 +70,7 @@ discard block |
||
| 70 | 70 | { |
| 71 | 71 | self::$data = $datas; |
| 72 | 72 | |
| 73 | - self::existData(); |
|
| 73 | + self::existData(); |
|
| 74 | 74 | self::jsonData(); |
| 75 | 75 | self::hasProvider(); |
| 76 | 76 | self::hasRole(); |
@@ -81,45 +81,45 @@ discard block |
||
| 81 | 81 | |
| 82 | 82 | $rules = self::getClass('HnrAzevedo\\Validator\\'.ucfirst(self::$data['provider'])); |
| 83 | 83 | |
| 84 | - self::existRole($rules); |
|
| 84 | + self::existRole($rules); |
|
| 85 | 85 | |
| 86 | - $validators = self::$validators[get_class($rules)]->getRules($datas['role']); |
|
| 86 | + $validators = self::$validators[get_class($rules)]->getRules($datas['role']); |
|
| 87 | 87 | |
| 88 | 88 | $tests = 0; |
| 89 | 89 | |
| 90 | - foreach ($validators as $key => $value) { |
|
| 91 | - $tests = (array_key_exists('required',$value) and $value['required']===true) ? $tests+1 : $tests; |
|
| 92 | - } |
|
| 90 | + foreach ($validators as $key => $value) { |
|
| 91 | + $tests = (array_key_exists('required',$value) and $value['required']===true) ? $tests+1 : $tests; |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - $testeds = 0; |
|
| 94 | + $testeds = 0; |
|
| 95 | 95 | |
| 96 | - foreach ($validators as $key => $value) { |
|
| 96 | + foreach ($validators as $key => $value) { |
|
| 97 | 97 | |
| 98 | - foreach ($data as $keyy => $valuee) { |
|
| 98 | + foreach ($data as $keyy => $valuee) { |
|
| 99 | 99 | |
| 100 | 100 | $v = $valuee; |
| 101 | 101 | |
| 102 | - if(is_array($valuee)){ |
|
| 103 | - $v = null; |
|
| 104 | - foreach ($valuee as $vvv) { |
|
| 105 | - $v .= $vvv; |
|
| 106 | - } |
|
| 102 | + if(is_array($valuee)){ |
|
| 103 | + $v = null; |
|
| 104 | + foreach ($valuee as $vvv) { |
|
| 105 | + $v .= $vvv; |
|
| 106 | + } |
|
| 107 | 107 | } |
| 108 | 108 | |
| 109 | - $valuee = $v; |
|
| 109 | + $valuee = $v; |
|
| 110 | 110 | |
| 111 | - if(!array_key_exists($keyy, $validators)){ |
|
| 111 | + if(!array_key_exists($keyy, $validators)){ |
|
| 112 | 112 | throw new Exception("O campo '{$keyy}' não é esperado para está operação."); |
| 113 | 113 | } |
| 114 | 114 | |
| 115 | - if($keyy===$key){ |
|
| 115 | + if($keyy===$key){ |
|
| 116 | 116 | |
| 117 | 117 | $testeds++; |
| 118 | 118 | |
| 119 | - foreach ($value as $subkey => $subvalue) { |
|
| 119 | + foreach ($value as $subkey => $subvalue) { |
|
| 120 | 120 | |
| 121 | - switch ($subkey) { |
|
| 122 | - case 'minlength': |
|
| 121 | + switch ($subkey) { |
|
| 122 | + case 'minlength': |
|
| 123 | 123 | if(array_key_exists('required', $value)){ |
| 124 | 124 | if($value['required'] or strlen($valuee)!==0){ |
| 125 | 125 | if(strlen($valuee)===0){ |
@@ -149,9 +149,9 @@ discard block |
||
| 149 | 149 | } |
| 150 | 150 | } |
| 151 | 151 | } |
| 152 | - break; |
|
| 152 | + break; |
|
| 153 | 153 | |
| 154 | - case 'maxlength': |
|
| 154 | + case 'maxlength': |
|
| 155 | 155 | if(array_key_exists('required', $value)){ |
| 156 | 156 | if($value['required'] or strlen($valuee)!==0){ |
| 157 | 157 | if(strlen($valuee)>(int)$subvalue){ |
@@ -161,7 +161,7 @@ discard block |
||
| 161 | 161 | } |
| 162 | 162 | break; |
| 163 | 163 | |
| 164 | - case 'regex': |
|
| 164 | + case 'regex': |
|
| 165 | 165 | if(array_key_exists('required', $value)){ |
| 166 | 166 | if($value['required'] or strlen($valuee)!==0){ |
| 167 | 167 | if(!@preg_match($subvalue,$valuee)){ |
@@ -171,7 +171,7 @@ discard block |
||
| 171 | 171 | } |
| 172 | 172 | break; |
| 173 | 173 | |
| 174 | - case 'equals': |
|
| 174 | + case 'equals': |
|
| 175 | 175 | $equals = false; |
| 176 | 176 | foreach ($data as $ke => $sub) { |
| 177 | 177 | if($ke===$subvalue){ |
@@ -184,18 +184,18 @@ discard block |
||
| 184 | 184 | throw new Exception("O servidor não encontrou a informação '{$subvalue}' para ser comparada a '{$key}'.",1); |
| 185 | 185 | } |
| 186 | 186 | break; |
| 187 | - } |
|
| 188 | - } |
|
| 189 | - } |
|
| 190 | - } |
|
| 187 | + } |
|
| 188 | + } |
|
| 189 | + } |
|
| 190 | + } |
|
| 191 | 191 | } |
| 192 | 192 | |
| 193 | - if($tests > $testeds){ |
|
| 193 | + if($tests > $testeds){ |
|
| 194 | 194 | throw new Exception('Alguma informação necessária não pode ser validada.'); |
| 195 | 195 | } |
| 196 | 196 | |
| 197 | - return true; |
|
| 198 | - } |
|
| 197 | + return true; |
|
| 198 | + } |
|
| 199 | 199 | |
| 200 | 200 | public static function toJson(array $request): string |
| 201 | 201 | { |
@@ -205,29 +205,29 @@ discard block |
||
| 205 | 205 | |
| 206 | 206 | $rules = self::getClass('HnrAzevedo\\Validator\\'.ucfirst($request['provider'])); |
| 207 | 207 | |
| 208 | - self::existRole($rules); |
|
| 208 | + self::existRole($rules); |
|
| 209 | 209 | |
| 210 | 210 | /* For function to validate information in javascript */ |
| 211 | 211 | $response = '{'; |
| 212 | 212 | |
| 213 | - foreach ( self::$validators[get_class($rules)]->getRules($request['role']) as $field => $r) { |
|
| 213 | + foreach ( self::$validators[get_class($rules)]->getRules($request['role']) as $field => $r) { |
|
| 214 | 214 | $response .= $field.':{'; |
| 215 | 215 | |
| 216 | - foreach(array_reverse($r) as $rule => $value){ |
|
| 216 | + foreach(array_reverse($r) as $rule => $value){ |
|
| 217 | 217 | $value = (gettype($value)==='string') ? '\''.$value.'\'' : $value; |
| 218 | 218 | |
| 219 | - if(gettype($value)==='boolean'){ |
|
| 219 | + if(gettype($value)==='boolean'){ |
|
| 220 | 220 | $value = ($value) ? 'true' : 'false'; |
| 221 | 221 | } |
| 222 | 222 | |
| 223 | 223 | $value = ($rule=='regex') ? str_replace('\\','\\\\','\''.substr($value,2,strlen($value)-4).'\'') : $value; |
| 224 | 224 | |
| 225 | - $response .= $rule.':'.$value.','; |
|
| 225 | + $response .= $rule.':'.$value.','; |
|
| 226 | 226 | } |
| 227 | 227 | |
| 228 | - $response .='},'; |
|
| 228 | + $response .='},'; |
|
| 229 | 229 | } |
| 230 | 230 | |
| 231 | - return substr(str_replace(',}','}',$response),0,-1).'}'; |
|
| 232 | - } |
|
| 231 | + return substr(str_replace(',}','}',$response),0,-1).'}'; |
|
| 232 | + } |
|
| 233 | 233 | } |