Passed
Branch master (cd9e8c)
by Henri
02:20 queued 01:06
created
examples/Validations/User.php 2 patches
Indentation   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -9,11 +9,11 @@
 block discarded – undo
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;
Please login to merge, or discard this patch.
Spacing   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -2,16 +2,16 @@
 block discarded – undo
2 2
 
3 3
 namespace HnrAzevedo\Validator;
4 4
 
5
-Class User{
5
+Class User {
6 6
 
7 7
     public function __construct()
8 8
     {
9 9
 
10
-        Validator::add($this, function(Rules $rules){
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 16
 			return $rules;
17 17
         });
Please login to merge, or discard this patch.
examples/index.php 2 patches
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -7,7 +7,7 @@  discard block
 block discarded – undo
7 7
 
8 8
 /* NOTE: in case of error an exception is thrown */
9 9
 
10
-try{
10
+try {
11 11
     /* Required format for validation */
12 12
     $data = [
13 13
         'data' => json_encode([
@@ -24,7 +24,7 @@  discard block
 block discarded – undo
24 24
     /* Transforms validation to Json format to be validated on the client if desired */
25 25
     $json = Validator::toJson($data);
26 26
 
27
-}catch(Exception $er){
27
+}catch (Exception $er) {
28 28
 
29 29
     die($er->getCode().'  -  '.$er->getMessage());
30 30
 
Please login to merge, or discard this patch.
Braces   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -24,7 +24,7 @@
 block discarded – undo
24 24
     /* Transforms validation to Json format to be validated on the client if desired */
25 25
     $json = Validator::toJson($data);
26 26
 
27
-}catch(Exception $er){
27
+} catch(Exception $er){
28 28
 
29 29
     die($er->getCode().'  -  '.$er->getMessage());
30 30
 
Please login to merge, or discard this patch.
src/Rules.php 2 patches
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -4,7 +4,7 @@  discard block
 block discarded – undo
4 4
 
5 5
 use Exception;
6 6
 
7
-Class Rules{
7
+Class Rules {
8 8
     private string $action;
9 9
 
10 10
     private array $form = array();
@@ -22,11 +22,11 @@  discard block
 block discarded – undo
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
 
Please login to merge, or discard this patch.
Indentation   +11 added lines, -11 removed lines patch added patch discarded remove patch
@@ -11,30 +11,30 @@
 block discarded – undo
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
 }
Please login to merge, or discard this patch.
src/Validator.php 2 patches
Indentation   +31 added lines, -31 removed lines patch added patch discarded remove patch
@@ -70,7 +70,7 @@  discard block
 block discarded – undo
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();
@@ -79,23 +79,23 @@  discard block
 block discarded – undo
79 79
 
80 80
         $rules = self::getClass('HnrAzevedo\\Validator\\'.ucfirst(self::$data['provider']));
81 81
 
82
-		self::existRole($rules);
82
+        self::existRole($rules);
83 83
 
84
-		$validators = self::$validators[get_class($rules)]->getRules($datas['role']);
84
+        $validators = self::$validators[get_class($rules)]->getRules($datas['role']);
85 85
 
86 86
         $tests = 0;
87 87
             
88
-		foreach ($validators as $key => $value) {
89
-		    $tests = (@$value['required'] === true ) ? $tests+1 : $tests;
88
+        foreach ($validators as $key => $value) {
89
+            $tests = (@$value['required'] === true ) ? $tests+1 : $tests;
90 90
         }
91 91
 
92
-		$testeds = self::validate($validators);
92
+        $testeds = self::validate($validators);
93 93
             
94
-		if($tests > $testeds){
94
+        if($tests > $testeds){
95 95
             throw new Exception('Alguma informação necessária não pode ser validada.');
96 96
         }
97 97
 				
98
-		return true;
98
+        return true;
99 99
     }
100 100
     
101 101
     public static function validate(array $validators): int
@@ -103,31 +103,31 @@  discard block
 block discarded – undo
103 103
         $validate = 0;
104 104
         foreach ($validators as $key => $value) {
105 105
 
106
-			foreach (json_decode(self::$data['data']) as $keyy => $valuee) {
106
+            foreach (json_decode(self::$data['data']) as $keyy => $valuee) {
107 107
 
108 108
                 $v = $valuee;
109 109
                     
110
-				if(is_array($valuee)){
111
-					$v = null;
112
-					foreach ($valuee as $vvv) {
113
-						$v .= $vvv;
114
-					}
110
+                if(is_array($valuee)){
111
+                    $v = null;
112
+                    foreach ($valuee as $vvv) {
113
+                        $v .= $vvv;
114
+                    }
115 115
                 }
116 116
                     
117
-				$valuee = $v;
117
+                $valuee = $v;
118 118
 
119
-				if(!array_key_exists($keyy, $validators)){
119
+                if(!array_key_exists($keyy, $validators)){
120 120
                     throw new Exception("O campo '{$keyy}' não é esperado para está operação.");
121 121
                 }
122 122
 
123
-				if($keyy===$key){
123
+                if($keyy===$key){
124 124
 
125 125
                     $validate++;
126 126
                         
127
-					foreach ($value as $subkey => $subvalue) {
127
+                    foreach ($value as $subkey => $subvalue) {
128 128
 
129
-						switch ($subkey) {
130
-							case 'minlength':
129
+                        switch ($subkey) {
130
+                            case 'minlength':
131 131
                                 if(array_key_exists('required', $value)){
132 132
                                     if($value['required'] or strlen($valuee)!==0){
133 133
                                         if(strlen($valuee)===0){
@@ -157,9 +157,9 @@  discard block
 block discarded – undo
157 157
                                         }
158 158
                                     }
159 159
                                 }
160
-    						    break;
160
+                                break;
161 161
 
162
-							case 'maxlength':
162
+                            case 'maxlength':
163 163
                                 if(array_key_exists('required', $value)){
164 164
                                     if($value['required'] or strlen($valuee)!==0){
165 165
                                         if(strlen($valuee)>(int)$subvalue){
@@ -169,7 +169,7 @@  discard block
 block discarded – undo
169 169
                                 }
170 170
                                 break;
171 171
 
172
-							case 'regex':
172
+                            case 'regex':
173 173
                                 if(array_key_exists('required', $value)){
174 174
                                     if($value['required'] or strlen($valuee)!==0){
175 175
                                         if(!@preg_match($subvalue,$valuee)){
@@ -179,7 +179,7 @@  discard block
 block discarded – undo
179 179
                                 }
180 180
                                 break;
181 181
 
182
-							case 'equals':
182
+                            case 'equals':
183 183
                                 $equals = false;
184 184
                                 foreach (self::$data as $ke => $sub) {
185 185
                                     if($ke===$subvalue){
@@ -193,10 +193,10 @@  discard block
 block discarded – undo
193 193
                                     throw new Exception("O servidor não encontrou a informação '{$subvalue}' para ser comparada a '{$key}'.",1);
194 194
                                 }
195 195
                                 break;
196
-	    				}
197
-					}
198
-				}
199
-			}
196
+                        }
197
+                    }
198
+                }
199
+            }
200 200
         }
201 201
         return $validate;
202 202
     }
@@ -214,7 +214,7 @@  discard block
 block discarded – undo
214 214
 
215 215
         self::existRole($rules);
216 216
 
217
-		foreach ( self::$validators[get_class($rules)]->getRules($request['role'])  as $field => $r) {
217
+        foreach ( self::$validators[get_class($rules)]->getRules($request['role'])  as $field => $r) {
218 218
             
219 219
             $response .= ("{$field}:".json_encode(array_reverse($r))).',';
220 220
             
@@ -225,6 +225,6 @@  discard block
 block discarded – undo
225 225
         $response = str_replace('{"','',$response);
226 226
         $response = str_replace('":',':',$response);
227 227
 
228
-		return $response;
229
-	}
228
+        return $response;
229
+    }
230 230
 }
Please login to merge, or discard this patch.
Spacing   +46 added lines, -46 removed lines patch added patch discarded remove patch
@@ -5,54 +5,54 @@  discard block
 block discarded – undo
5 5
 use HnrAzevedo\Validator\Rules;
6 6
 use Exception;
7 7
 
8
-Class Validator{
8
+Class Validator {
9 9
     
10 10
     private static array $validators = array();
11 11
     private static array $data = [];
12 12
 
13
-    public static function add(object $model,callable $return): void
13
+    public static function add(object $model, callable $return): void
14 14
     {
15 15
         self::$validators[get_class($model)] = $return($Rules = new Rules($model));
16 16
     }
17 17
     
18 18
     private static function existData()
19 19
     {
20
-        if(!array_key_exists('data', self::$data)){
20
+        if (!array_key_exists('data', self::$data)) {
21 21
             throw new Exception('Informações cruciais não foram recebidas.');
22 22
         }
23 23
     }
24 24
 
25 25
     private static function jsonData()
26 26
     {
27
-        if(json_decode(self::$data['data']) === null){
27
+        if (json_decode(self::$data['data'])===null) {
28 28
             throw new Exception('O servidor recebeu as informações no formato esperado.');
29 29
         }
30 30
     }
31 31
 
32 32
     private static function hasProvider()
33 33
     {
34
-        if(!array_key_exists('provider',self::$data)){
34
+        if (!array_key_exists('provider', self::$data)) {
35 35
             throw new Exception('O servidor não recebeu o ID do formulário.');
36 36
         }
37 37
     }
38 38
 
39 39
     private static function hasRole()
40 40
     {
41
-        if(!array_key_exists('role',self::$data)){
41
+        if (!array_key_exists('role', self::$data)) {
42 42
             throw new Exception('O servidor não conseguiu identificar a finalidade deste formulário.');
43 43
         }
44 44
     }
45 45
 
46 46
     private static function includeValidations()
47 47
     {
48
-        if( file_exists(VALIDATOR_CONFIG['path'] . ucfirst(self::$data['provider']) . '.php') ){
49
-            require_once(VALIDATOR_CONFIG['path'] . ucfirst(self::$data['provider']) . '.php');
48
+        if (file_exists(VALIDATOR_CONFIG['path'].ucfirst(self::$data['provider']).'.php')) {
49
+            require_once(VALIDATOR_CONFIG['path'].ucfirst(self::$data['provider']).'.php');
50 50
         }
51 51
     }
52 52
 
53 53
     private static function getClass(string $class)
54 54
     {
55
-        if(!class_exists($class)){
55
+        if (!class_exists($class)) {
56 56
             throw new Exception("Form ID {$class} inválido.");
57 57
         }
58 58
 
@@ -61,7 +61,7 @@  discard block
 block discarded – undo
61 61
 
62 62
     private static function existRole($rules)
63 63
     {
64
-        if(empty(self::$validators[get_class($rules)]->getRules(self::$data['role']))){
64
+        if (empty(self::$validators[get_class($rules)]->getRules(self::$data['role']))) {
65 65
             throw new Exception('Não existe regras para validar este formulário.');
66 66
         }
67 67
     }
@@ -86,12 +86,12 @@  discard block
 block discarded – undo
86 86
         $tests = 0;
87 87
             
88 88
 		foreach ($validators as $key => $value) {
89
-		    $tests = (@$value['required'] === true ) ? $tests+1 : $tests;
89
+		    $tests = (@$value['required']===true) ? $tests+1 : $tests;
90 90
         }
91 91
 
92 92
 		$testeds = self::validate($validators);
93 93
             
94
-		if($tests > $testeds){
94
+		if ($tests>$testeds) {
95 95
             throw new Exception('Alguma informação necessária não pode ser validada.');
96 96
         }
97 97
 				
@@ -107,7 +107,7 @@  discard block
 block discarded – undo
107 107
 
108 108
                 $v = $valuee;
109 109
                     
110
-				if(is_array($valuee)){
110
+				if (is_array($valuee)) {
111 111
 					$v = null;
112 112
 					foreach ($valuee as $vvv) {
113 113
 						$v .= $vvv;
@@ -116,11 +116,11 @@  discard block
 block discarded – undo
116 116
                     
117 117
 				$valuee = $v;
118 118
 
119
-				if(!array_key_exists($keyy, $validators)){
119
+				if (!array_key_exists($keyy, $validators)) {
120 120
                     throw new Exception("O campo '{$keyy}' não é esperado para está operação.");
121 121
                 }
122 122
 
123
-				if($keyy===$key){
123
+				if ($keyy===$key) {
124 124
 
125 125
                     $validate++;
126 126
                         
@@ -128,30 +128,30 @@  discard block
 block discarded – undo
128 128
 
129 129
 						switch ($subkey) {
130 130
 							case 'minlength':
131
-                                if(array_key_exists('required', $value)){
132
-                                    if($value['required'] or strlen($valuee)!==0){
133
-                                        if(strlen($valuee)===0){
134
-                                            throw new Exception("O campo '{$key}' é obrigatório.",1);
131
+                                if (array_key_exists('required', $value)) {
132
+                                    if ($value['required'] or strlen($valuee)!==0) {
133
+                                        if (strlen($valuee)===0) {
134
+                                            throw new Exception("O campo '{$key}' é obrigatório.", 1);
135 135
                                         }
136 136
                                          
137
-                                        if(strlen($valuee) < (int) $subvalue){
138
-                                            throw new Exception("{$key} não atingiu o mínimo de caracteres esperado.",1);
137
+                                        if (strlen($valuee)<(int) $subvalue) {
138
+                                            throw new Exception("{$key} não atingiu o mínimo de caracteres esperado.", 1);
139 139
                                         }
140 140
                                     }
141 141
                                 }
142 142
                                 break;
143 143
 
144 144
                             case 'type':
145
-                                if(array_key_exists('required', $value)){
146
-                                    if($value['required'] or strlen($valuee)!==0){
145
+                                if (array_key_exists('required', $value)) {
146
+                                    if ($value['required'] or strlen($valuee)!==0) {
147 147
                                         switch ($subvalue) {
148 148
                                             case 'date':
149 149
                                                 $date = explode('/', $valuee);
150
-                                                if(count($date) != 3){
151
-                                                    throw new Exception('Data inválida.',1);
150
+                                                if (count($date)!=3) {
151
+                                                    throw new Exception('Data inválida.', 1);
152 152
                                                 }
153
-                                                if(! checkdate( intval($date[1]), intval($date[0]), intval($date[2]) )){
154
-                                                    throw new Exception('Data inválida.',1);
153
+                                                if (!checkdate(intval($date[1]), intval($date[0]), intval($date[2]))) {
154
+                                                    throw new Exception('Data inválida.', 1);
155 155
                                                 }
156 156
                                                 break;
157 157
                                         }
@@ -160,20 +160,20 @@  discard block
 block discarded – undo
160 160
     						    break;
161 161
 
162 162
 							case 'maxlength':
163
-                                if(array_key_exists('required', $value)){
164
-                                    if($value['required'] or strlen($valuee)!==0){
165
-                                        if(strlen($valuee)>(int)$subvalue){
166
-                                            throw new Exception("{$key} ultrapassou o limite de caracteres permitidos.",1);
163
+                                if (array_key_exists('required', $value)) {
164
+                                    if ($value['required'] or strlen($valuee)!==0) {
165
+                                        if (strlen($valuee)>(int) $subvalue) {
166
+                                            throw new Exception("{$key} ultrapassou o limite de caracteres permitidos.", 1);
167 167
                                         }
168 168
                                     }
169 169
                                 }
170 170
                                 break;
171 171
 
172 172
 							case 'regex':
173
-                                if(array_key_exists('required', $value)){
174
-                                    if($value['required'] or strlen($valuee)!==0){
175
-                                        if(!@preg_match($subvalue,$valuee)){
176
-                                            throw new Exception("{$key} inválido(a).",1);
173
+                                if (array_key_exists('required', $value)) {
174
+                                    if ($value['required'] or strlen($valuee)!==0) {
175
+                                        if (!@preg_match($subvalue, $valuee)) {
176
+                                            throw new Exception("{$key} inválido(a).", 1);
177 177
                                         }
178 178
                                     }
179 179
                                 }
@@ -182,15 +182,15 @@  discard block
 block discarded – undo
182 182
 							case 'equals':
183 183
                                 $equals = false;
184 184
                                 foreach (self::$data as $ke => $sub) {
185
-                                    if($ke===$subvalue){
186
-                                        $equals=true;
187
-                                        if($valuee !== $sub){
188
-                                            throw new Exception(ucfirst($key).' está diferente de '.ucfirst($ke),1);
185
+                                    if ($ke===$subvalue) {
186
+                                        $equals = true;
187
+                                        if ($valuee!==$sub) {
188
+                                            throw new Exception(ucfirst($key).' está diferente de '.ucfirst($ke), 1);
189 189
                                         }
190 190
                                     }
191 191
                                 }
192
-                                if(!$equals){
193
-                                    throw new Exception("O servidor não encontrou a informação '{$subvalue}' para ser comparada a '{$key}'.",1);
192
+                                if (!$equals) {
193
+                                    throw new Exception("O servidor não encontrou a informação '{$subvalue}' para ser comparada a '{$key}'.", 1);
194 194
                                 }
195 195
                                 break;
196 196
 	    				}
@@ -214,16 +214,16 @@  discard block
 block discarded – undo
214 214
 
215 215
         self::existRole($rules);
216 216
 
217
-		foreach ( self::$validators[get_class($rules)]->getRules($request['role'])  as $field => $r) {
217
+		foreach (self::$validators[get_class($rules)]->getRules($request['role'])  as $field => $r) {
218 218
             
219 219
             $response .= ("{$field}:".json_encode(array_reverse($r))).',';
220 220
             
221 221
         }
222 222
 
223
-        $response = '{'.substr($response,0,-1).'}';
224
-        $response = str_replace(',"',',',$response);
225
-        $response = str_replace('{"','',$response);
226
-        $response = str_replace('":',':',$response);
223
+        $response = '{'.substr($response, 0, -1).'}';
224
+        $response = str_replace(',"', ',', $response);
225
+        $response = str_replace('{"', '', $response);
226
+        $response = str_replace('":', ':', $response);
227 227
 
228 228
 		return $response;
229 229
 	}
Please login to merge, or discard this patch.