@@ -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 | } |
@@ -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,33 +100,33 @@ 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 | 105 | $v = $valuee; |
| 106 | 106 | |
| 107 | - if(is_array($valuee)){ |
|
| 108 | - $v = null; |
|
| 109 | - foreach ($valuee as $vvv) { |
|
| 110 | - $v .= $vvv; |
|
| 111 | - } |
|
| 107 | + if(is_array($valuee)){ |
|
| 108 | + $v = null; |
|
| 109 | + foreach ($valuee as $vvv) { |
|
| 110 | + $v .= $vvv; |
|
| 111 | + } |
|
| 112 | 112 | } |
| 113 | 113 | |
| 114 | - $valuee = $v; |
|
| 114 | + $valuee = $v; |
|
| 115 | 115 | |
| 116 | - if(!array_key_exists($keyy, (self::$validators[self::$model]->getRules(self::$data['role'])) )){ |
|
| 116 | + if(!array_key_exists($keyy, (self::$validators[self::$model]->getRules(self::$data['role'])) )){ |
|
| 117 | 117 | throw new Exception("O campo '{$keyy}' não é esperado para está operação."); |
| 118 | 118 | } |
| 119 | 119 | |
| 120 | - if($keyy===$key){ |
|
| 120 | + if($keyy===$key){ |
|
| 121 | 121 | |
| 122 | 122 | $validate++; |
| 123 | 123 | |
| 124 | - foreach ($value as $subkey => $subvalue) { |
|
| 124 | + foreach ($value as $subkey => $subvalue) { |
|
| 125 | 125 | $function = "check_{$subkey}"; |
| 126 | 126 | self::$function($keyy,$subvalue); |
| 127 | - } |
|
| 128 | - } |
|
| 129 | - } |
|
| 127 | + } |
|
| 128 | + } |
|
| 129 | + } |
|
| 130 | 130 | } |
| 131 | 131 | return $validate; |
| 132 | 132 | } |
@@ -144,7 +144,7 @@ discard block |
||
| 144 | 144 | |
| 145 | 145 | self::existRole(self::$model); |
| 146 | 146 | |
| 147 | - foreach ( self::$validators[self::$model]->getRules($request['role']) as $field => $r) { |
|
| 147 | + foreach ( self::$validators[self::$model]->getRules($request['role']) as $field => $r) { |
|
| 148 | 148 | |
| 149 | 149 | $response .= ("{$field}:".json_encode(array_reverse($r))).','; |
| 150 | 150 | |
@@ -155,6 +155,6 @@ discard block |
||
| 155 | 155 | $response = str_replace('{"','',$response); |
| 156 | 156 | $response = str_replace('":',':',$response); |
| 157 | 157 | |
| 158 | - return $response; |
|
| 159 | - } |
|
| 158 | + return $response; |
|
| 159 | + } |
|
| 160 | 160 | } |
@@ -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; |