@@ -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,23 +81,23 @@ 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 = (@$value['required'] === true ) ? $tests+1 : $tests; |
|
| 90 | + foreach ($validators as $key => $value) { |
|
| 91 | + $tests = (@$value['required'] === true ) ? $tests+1 : $tests; |
|
| 92 | 92 | } |
| 93 | 93 | |
| 94 | - $testeds = self::validate($validators); |
|
| 94 | + $testeds = self::validate($validators); |
|
| 95 | 95 | |
| 96 | - if($tests > $testeds){ |
|
| 96 | + if($tests > $testeds){ |
|
| 97 | 97 | throw new Exception('Alguma informação necessária não pode ser validada.'); |
| 98 | 98 | } |
| 99 | 99 | |
| 100 | - return true; |
|
| 100 | + return true; |
|
| 101 | 101 | } |
| 102 | 102 | |
| 103 | 103 | public static function validate(array $validators): int |
@@ -105,31 +105,31 @@ discard block |
||
| 105 | 105 | $validate = 0; |
| 106 | 106 | foreach ($validators as $key => $value) { |
| 107 | 107 | |
| 108 | - foreach (json_decode(self::$data['data']) as $keyy => $valuee) { |
|
| 108 | + foreach (json_decode(self::$data['data']) as $keyy => $valuee) { |
|
| 109 | 109 | |
| 110 | 110 | $v = $valuee; |
| 111 | 111 | |
| 112 | - if(is_array($valuee)){ |
|
| 113 | - $v = null; |
|
| 114 | - foreach ($valuee as $vvv) { |
|
| 115 | - $v .= $vvv; |
|
| 116 | - } |
|
| 112 | + if(is_array($valuee)){ |
|
| 113 | + $v = null; |
|
| 114 | + foreach ($valuee as $vvv) { |
|
| 115 | + $v .= $vvv; |
|
| 116 | + } |
|
| 117 | 117 | } |
| 118 | 118 | |
| 119 | - $valuee = $v; |
|
| 119 | + $valuee = $v; |
|
| 120 | 120 | |
| 121 | - if(!array_key_exists($keyy, $validators)){ |
|
| 121 | + if(!array_key_exists($keyy, $validators)){ |
|
| 122 | 122 | throw new Exception("O campo '{$keyy}' não é esperado para está operação."); |
| 123 | 123 | } |
| 124 | 124 | |
| 125 | - if($keyy===$key){ |
|
| 125 | + if($keyy===$key){ |
|
| 126 | 126 | |
| 127 | 127 | $validate++; |
| 128 | 128 | |
| 129 | - foreach ($value as $subkey => $subvalue) { |
|
| 129 | + foreach ($value as $subkey => $subvalue) { |
|
| 130 | 130 | |
| 131 | - switch ($subkey) { |
|
| 132 | - case 'minlength': |
|
| 131 | + switch ($subkey) { |
|
| 132 | + case 'minlength': |
|
| 133 | 133 | if(array_key_exists('required', $value)){ |
| 134 | 134 | if($value['required'] or strlen($valuee)!==0){ |
| 135 | 135 | if(strlen($valuee)===0){ |
@@ -159,9 +159,9 @@ discard block |
||
| 159 | 159 | } |
| 160 | 160 | } |
| 161 | 161 | } |
| 162 | - break; |
|
| 162 | + break; |
|
| 163 | 163 | |
| 164 | - case 'maxlength': |
|
| 164 | + case 'maxlength': |
|
| 165 | 165 | if(array_key_exists('required', $value)){ |
| 166 | 166 | if($value['required'] or strlen($valuee)!==0){ |
| 167 | 167 | if(strlen($valuee)>(int)$subvalue){ |
@@ -171,7 +171,7 @@ discard block |
||
| 171 | 171 | } |
| 172 | 172 | break; |
| 173 | 173 | |
| 174 | - case 'regex': |
|
| 174 | + case 'regex': |
|
| 175 | 175 | if(array_key_exists('required', $value)){ |
| 176 | 176 | if($value['required'] or strlen($valuee)!==0){ |
| 177 | 177 | if(!@preg_match($subvalue,$valuee)){ |
@@ -181,7 +181,7 @@ discard block |
||
| 181 | 181 | } |
| 182 | 182 | break; |
| 183 | 183 | |
| 184 | - case 'equals': |
|
| 184 | + case 'equals': |
|
| 185 | 185 | $equals = false; |
| 186 | 186 | foreach (self::$data as $ke => $sub) { |
| 187 | 187 | if($ke===$subvalue){ |
@@ -195,10 +195,10 @@ discard block |
||
| 195 | 195 | throw new Exception("O servidor não encontrou a informação '{$subvalue}' para ser comparada a '{$key}'.",1); |
| 196 | 196 | } |
| 197 | 197 | break; |
| 198 | - } |
|
| 199 | - } |
|
| 200 | - } |
|
| 201 | - } |
|
| 198 | + } |
|
| 199 | + } |
|
| 200 | + } |
|
| 201 | + } |
|
| 202 | 202 | } |
| 203 | 203 | return $validate; |
| 204 | 204 | } |
@@ -211,29 +211,29 @@ discard block |
||
| 211 | 211 | |
| 212 | 212 | $rules = self::getClass('HnrAzevedo\\Validator\\'.ucfirst($request['provider'])); |
| 213 | 213 | |
| 214 | - self::existRole($rules); |
|
| 214 | + self::existRole($rules); |
|
| 215 | 215 | |
| 216 | 216 | /* For function to validate information in javascript */ |
| 217 | 217 | $response = '{'; |
| 218 | 218 | |
| 219 | - foreach ( self::$validators[get_class($rules)]->getRules($request['role']) as $field => $r) { |
|
| 219 | + foreach ( self::$validators[get_class($rules)]->getRules($request['role']) as $field => $r) { |
|
| 220 | 220 | $response .= $field.':{'; |
| 221 | 221 | |
| 222 | - foreach(array_reverse($r) as $rule => $value){ |
|
| 222 | + foreach(array_reverse($r) as $rule => $value){ |
|
| 223 | 223 | $value = (gettype($value)==='string') ? '\''.$value.'\'' : $value; |
| 224 | 224 | |
| 225 | - if(gettype($value)==='boolean'){ |
|
| 225 | + if(gettype($value)==='boolean'){ |
|
| 226 | 226 | $value = ($value) ? 'true' : 'false'; |
| 227 | 227 | } |
| 228 | 228 | |
| 229 | 229 | $value = ($rule=='regex') ? str_replace('\\','\\\\','\''.substr($value,2,strlen($value)-4).'\'') : $value; |
| 230 | 230 | |
| 231 | - $response .= $rule.':'.$value.','; |
|
| 231 | + $response .= $rule.':'.$value.','; |
|
| 232 | 232 | } |
| 233 | 233 | |
| 234 | - $response .='},'; |
|
| 234 | + $response .='},'; |
|
| 235 | 235 | } |
| 236 | 236 | |
| 237 | - return substr(str_replace(',}','}',$response),0,-1).'}'; |
|
| 238 | - } |
|
| 237 | + return substr(str_replace(',}','}',$response),0,-1).'}'; |
|
| 238 | + } |
|
| 239 | 239 | } |