@@ -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 | } |
@@ -9,12 +9,12 @@ |
||
| 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('password2',['equals'=>'password','required'=>true]) |
|
| 15 | - ->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('password2',['equals'=>'password','required'=>true]) |
|
| 15 | + ->addField('remember',['index'=>3,'minlength'=>2,'maxlength'=>2,'required'=>false]); |
|
| 16 | 16 | |
| 17 | - return $rules; |
|
| 17 | + return $rules; |
|
| 18 | 18 | }); |
| 19 | 19 | |
| 20 | 20 | return $this; |
@@ -69,7 +69,7 @@ discard block |
||
| 69 | 69 | { |
| 70 | 70 | self::$data = $datas; |
| 71 | 71 | |
| 72 | - self::existData(); |
|
| 72 | + self::existData(); |
|
| 73 | 73 | self::jsonData(); |
| 74 | 74 | self::hasProvider(); |
| 75 | 75 | self::hasRole(); |
@@ -78,21 +78,21 @@ discard block |
||
| 78 | 78 | |
| 79 | 79 | self::$model = get_class(self::getClass('HnrAzevedo\\Validator\\'.ucfirst(self::$data['provider']))); |
| 80 | 80 | |
| 81 | - self::existRole(self::$model); |
|
| 81 | + self::existRole(self::$model); |
|
| 82 | 82 | |
| 83 | 83 | $tests = 0; |
| 84 | 84 | |
| 85 | - foreach ( (self::$validators[self::$model]->getRules($datas['role'])) as $key => $value) { |
|
| 86 | - $tests = (@$value['required'] === true ) ? $tests+1 : $tests; |
|
| 85 | + foreach ( (self::$validators[self::$model]->getRules($datas['role'])) as $key => $value) { |
|
| 86 | + $tests = (@$value['required'] === true ) ? $tests+1 : $tests; |
|
| 87 | 87 | } |
| 88 | 88 | |
| 89 | - $testeds = self::validate(); |
|
| 89 | + $testeds = self::validate(); |
|
| 90 | 90 | |
| 91 | - if($tests > $testeds){ |
|
| 91 | + if($tests > $testeds){ |
|
| 92 | 92 | throw new Exception('Alguma informação necessária não pode ser validada.'); |
| 93 | 93 | } |
| 94 | 94 | |
| 95 | - return true; |
|
| 95 | + return true; |
|
| 96 | 96 | } |
| 97 | 97 | |
| 98 | 98 | public static function validate(): int |
@@ -100,22 +100,22 @@ discard block |
||
| 100 | 100 | $validate = 0; |
| 101 | 101 | foreach ( (self::$validators[self::$model]->getRules(self::$data['role'])) as $key => $value) { |
| 102 | 102 | |
| 103 | - foreach (json_decode(self::$data['data']) as $keyy => $valuee) { |
|
| 103 | + foreach (json_decode(self::$data['data']) as $keyy => $valuee) { |
|
| 104 | 104 | |
| 105 | - if(!array_key_exists($keyy, (self::$validators[self::$model]->getRules(self::$data['role'])) )){ |
|
| 105 | + if(!array_key_exists($keyy, (self::$validators[self::$model]->getRules(self::$data['role'])) )){ |
|
| 106 | 106 | throw new Exception("O campo '{$keyy}' não é esperado para está operação."); |
| 107 | 107 | } |
| 108 | 108 | |
| 109 | - if($keyy===$key){ |
|
| 109 | + if($keyy===$key){ |
|
| 110 | 110 | |
| 111 | 111 | $validate++; |
| 112 | 112 | |
| 113 | - foreach ($value as $subkey => $subvalue) { |
|
| 113 | + foreach ($value as $subkey => $subvalue) { |
|
| 114 | 114 | $function = "check_{$subkey}"; |
| 115 | 115 | self::$function($keyy,$subvalue); |
| 116 | - } |
|
| 117 | - } |
|
| 118 | - } |
|
| 116 | + } |
|
| 117 | + } |
|
| 118 | + } |
|
| 119 | 119 | } |
| 120 | 120 | return $validate; |
| 121 | 121 | } |
@@ -133,7 +133,7 @@ discard block |
||
| 133 | 133 | |
| 134 | 134 | self::existRole(self::$model); |
| 135 | 135 | |
| 136 | - foreach ( self::$validators[self::$model]->getRules($request['role']) as $field => $r) { |
|
| 136 | + foreach ( self::$validators[self::$model]->getRules($request['role']) as $field => $r) { |
|
| 137 | 137 | |
| 138 | 138 | $response .= ("{$field}:".json_encode(array_reverse($r))).','; |
| 139 | 139 | |
@@ -144,6 +144,6 @@ discard block |
||
| 144 | 144 | $response = str_replace('{"','',$response); |
| 145 | 145 | $response = str_replace('":',':',$response); |
| 146 | 146 | |
| 147 | - return $response; |
|
| 148 | - } |
|
| 147 | + return $response; |
|
| 148 | + } |
|
| 149 | 149 | } |