Passed
Branch master (5146d9)
by Henri
04:44 queued 03:28
created
src/Validator.php 2 patches
Indentation   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -62,7 +62,7 @@  discard block
 block discarded – undo
62 62
 
63 63
     public static function checkDatas()
64 64
     {
65
-		self::existData();
65
+        self::existData();
66 66
         self::jsonData();
67 67
         self::hasProvider();
68 68
         self::hasRole();
@@ -78,9 +78,9 @@  discard block
 block discarded – undo
78 78
             
79 79
         self::$model = self::getClass($model);
80 80
 
81
-		self::existRole(self::$model);
81
+        self::existRole(self::$model);
82 82
             
83
-		foreach ( (self::$validators[self::$model]->getRules($datas['role'])) as $key => $value) {
83
+        foreach ( (self::$validators[self::$model]->getRules($datas['role'])) as $key => $value) {
84 84
             if(@$value['required'] === true){
85 85
                 self::$required[$key] = $value;
86 86
             }
@@ -92,7 +92,7 @@  discard block
 block discarded – undo
92 92
         
93 93
         self::checkRequireds();
94 94
 				
95
-		return self::checkErrors();
95
+        return self::checkErrors();
96 96
     }
97 97
 
98 98
     public static function checkErrors(): bool
@@ -104,23 +104,23 @@  discard block
 block discarded – undo
104 104
     {
105 105
         foreach ( (self::$validators[self::$model]->getRules(self::$data['role'])) as $key => $value) {
106 106
 
107
-			foreach (json_decode(self::$data['data']) as $keyy => $valuee) {
107
+            foreach (json_decode(self::$data['data']) as $keyy => $valuee) {
108 108
 
109
-				if(!array_key_exists($keyy, (self::$validators[self::$model]->getRules(self::$data['role'])) )){
109
+                if(!array_key_exists($keyy, (self::$validators[self::$model]->getRules(self::$data['role'])) )){
110 110
                     throw new Exception("O campo '{$keyy}' não é esperado para está operação.");
111 111
                 }
112 112
 
113
-				if($keyy===$key){
113
+                if($keyy===$key){
114 114
 
115 115
                     unset(self::$required[$key]);
116 116
 
117
-					foreach ($value as $subkey => $subvalue) {
117
+                    foreach ($value as $subkey => $subvalue) {
118 118
                         $function = "check".ucfirst($subkey);
119 119
                         self::testMethod($function);
120 120
                         self::$function($keyy,$subvalue);
121
-					}
122
-				}
123
-			}
121
+                    }
122
+                }
123
+            }
124 124
         }
125 125
     }
126 126
 
@@ -149,7 +149,7 @@  discard block
 block discarded – undo
149 149
 
150 150
         self::existRole(self::$model);
151 151
 
152
-		foreach ( self::$validators[self::$model]->getRules($request['role'])  as $field => $r) {
152
+        foreach ( self::$validators[self::$model]->getRules($request['role'])  as $field => $r) {
153 153
             $r = self::replaceRegex($r);
154 154
             $response .= ("{$field}:".json_encode(array_reverse($r))).',';
155 155
         }
Please login to merge, or discard this patch.
Spacing   +20 added lines, -20 removed lines patch added patch discarded remove patch
@@ -5,10 +5,10 @@  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
     use Check;
10 10
 
11
-    public static function add(object $model,callable $return): void
11
+    public static function add(object $model, callable $return): void
12 12
     {
13 13
         self::$model = get_class($model);
14 14
         self::$validators[self::$model] = $return($Rules = new Rules($model));
@@ -16,35 +16,35 @@  discard block
 block discarded – undo
16 16
     
17 17
     private static function existData()
18 18
     {
19
-        if(!array_key_exists('data', self::$data)){
19
+        if (!array_key_exists('data', self::$data)) {
20 20
             throw new Exception('Informações cruciais não foram recebidas.');
21 21
         }
22 22
     }
23 23
 
24 24
     private static function jsonData()
25 25
     {
26
-        if(json_decode(self::$data['data']) === null){
26
+        if (json_decode(self::$data['data'])===null) {
27 27
             throw new Exception('O servidor recebeu as informações no formato esperado.');
28 28
         }
29 29
     }
30 30
 
31 31
     private static function hasProvider()
32 32
     {
33
-        if(!array_key_exists('provider',self::$data)){
33
+        if (!array_key_exists('provider', self::$data)) {
34 34
             throw new Exception('O servidor não recebeu o ID do formulário.');
35 35
         }
36 36
     }
37 37
 
38 38
     private static function hasRole()
39 39
     {
40
-        if(!array_key_exists('role',self::$data)){
40
+        if (!array_key_exists('role', self::$data)) {
41 41
             throw new Exception('O servidor não conseguiu identificar a finalidade deste formulário.');
42 42
         }
43 43
     }
44 44
 
45 45
     private static function getClass(string $class)
46 46
     {
47
-        if(!class_exists($class)){
47
+        if (!class_exists($class)) {
48 48
             throw new Exception("Form ID {$class} inválido.");
49 49
         }
50 50
 
@@ -55,7 +55,7 @@  discard block
 block discarded – undo
55 55
 
56 56
     private static function existRole($rules)
57 57
     {
58
-        if(empty(self::$validators[$rules]->getRules(self::$data['role']))){
58
+        if (empty(self::$validators[$rules]->getRules(self::$data['role']))) {
59 59
             throw new Exception('Não existe regras para validar este formulário.');
60 60
         }
61 61
     }
@@ -80,8 +80,8 @@  discard block
 block discarded – undo
80 80
 
81 81
 		self::existRole(self::$model);
82 82
             
83
-		foreach ( (self::$validators[self::$model]->getRules($datas['role'])) as $key => $value) {
84
-            if(@$value['required'] === true){
83
+		foreach ((self::$validators[self::$model]->getRules($datas['role'])) as $key => $value) {
84
+            if (@$value['required']===true) {
85 85
                 self::$required[$key] = $value;
86 86
             }
87 87
         }
@@ -97,27 +97,27 @@  discard block
 block discarded – undo
97 97
 
98 98
     public static function checkErrors(): bool
99 99
     {
100
-        return (count(self::$errors) === 0);
100
+        return (count(self::$errors)===0);
101 101
     }
102 102
     
103 103
     public static function validate()
104 104
     {
105
-        foreach ( (self::$validators[self::$model]->getRules(self::$data['role'])) as $key => $value) {
105
+        foreach ((self::$validators[self::$model]->getRules(self::$data['role'])) as $key => $value) {
106 106
 
107 107
 			foreach (json_decode(self::$data['data']) as $keyy => $valuee) {
108 108
 
109
-				if(!array_key_exists($keyy, (self::$validators[self::$model]->getRules(self::$data['role'])) )){
109
+				if (!array_key_exists($keyy, (self::$validators[self::$model]->getRules(self::$data['role'])))) {
110 110
                     throw new Exception("O campo '{$keyy}' não é esperado para está operação.");
111 111
                 }
112 112
 
113
-				if($keyy===$key){
113
+				if ($keyy===$key) {
114 114
 
115 115
                     unset(self::$required[$key]);
116 116
 
117 117
 					foreach ($value as $subkey => $subvalue) {
118 118
                         $function = "check".ucfirst($subkey);
119 119
                         self::testMethod($function);
120
-                        self::$function($keyy,$subvalue);
120
+                        self::$function($keyy, $subvalue);
121 121
 					}
122 122
 				}
123 123
 			}
@@ -131,7 +131,7 @@  discard block
 block discarded – undo
131 131
 
132 132
     public static function testMethod($method)
133 133
     {
134
-        if(!method_exists(static::class, $method)){
134
+        if (!method_exists(static::class, $method)) {
135 135
             throw new Exception("{$method} não é uma validação válida.");
136 136
         }
137 137
     }
@@ -149,18 +149,18 @@  discard block
 block discarded – undo
149 149
 
150 150
         self::existRole(self::$model);
151 151
 
152
-		foreach ( self::$validators[self::$model]->getRules($request['role'])  as $field => $r) {
152
+		foreach (self::$validators[self::$model]->getRules($request['role'])  as $field => $r) {
153 153
             $r = self::replaceRegex($r);
154 154
             $response .= ("{$field}:".json_encode(array_reverse($r))).',';
155 155
         }
156 156
 
157
-        return '{'.substr($response,0,-1).'}';
157
+        return '{'.substr($response, 0, -1).'}';
158 158
     }
159 159
     
160 160
     private static function replaceRegex(array $rules): array
161 161
     {
162
-        if(array_key_exists('regex',$rules)){ 
163
-            $rules['regex'] = substr($rules['regex'],1,-2);
162
+        if (array_key_exists('regex', $rules)) { 
163
+            $rules['regex'] = substr($rules['regex'], 1, -2);
164 164
         }
165 165
         return $rules;
166 166
     }
Please login to merge, or discard this patch.
examples/index.php 2 patches
Spacing   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -12,7 +12,7 @@  discard block
 block discarded – undo
12 12
         'password' => 123456,
13 13
         'password2' => 123456,
14 14
         'phones' => json_encode([
15
-            '949164770','949164771','949164772'
15
+            '949164770', '949164771', '949164772'
16 16
         ]),
17 17
         'birth' => '28/09/1996' 
18 18
     ]),
@@ -20,16 +20,16 @@  discard block
 block discarded – undo
20 20
     'role' => 'login'
21 21
 ];
22 22
 
23
-try{
23
+try {
24 24
 
25 25
     /* Checks whether the passed data is valid for the selected function */
26 26
     $valid = Validator::execute($data);
27 27
     
28
-    if(!$valid){
28
+    if (!$valid) {
29 29
         $errors = [];
30
-        foreach(Validator::getErrors() as $err => $message){
30
+        foreach (Validator::getErrors() as $err => $message) {
31 31
             $errors[] = [
32
-                'input' => array_keys($message)[0],                 // Return name input error
32
+                'input' => array_keys($message)[0], // Return name input error
33 33
                 'message' => $message[array_keys($message)[0]]      // Return message error
34 34
             ];
35 35
         }
@@ -40,7 +40,7 @@  discard block
 block discarded – undo
40 40
 
41 41
     var_dump($json);
42 42
 
43
-}catch(Exception $er){
43
+}catch (Exception $er) {
44 44
 
45 45
     die("Code Error: {$er->getCode()}, Line: {$er->getLine()}, File: {$er->getFile()}, Message: {$er->getMessage()}.");
46 46
 
Please login to merge, or discard this patch.
Braces   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -40,7 +40,7 @@
 block discarded – undo
40 40
 
41 41
     var_dump($json);
42 42
 
43
-}catch(Exception $er){
43
+} catch(Exception $er){
44 44
 
45 45
     die("Code Error: {$er->getCode()}, Line: {$er->getLine()}, File: {$er->getFile()}, Message: {$er->getMessage()}.");
46 46
 
Please login to merge, or discard this patch.