Passed
Push — master ( ae9656...8b853b )
by Henri
01:13
created
src/Datamanager.php 2 patches
Spacing   +24 added lines, -24 removed lines patch added patch discarded remove patch
@@ -14,7 +14,7 @@  discard block
 block discarded – undo
14 14
     private bool $full = false;
15 15
     private ?string $clause = null;
16 16
 
17
-    private array $where = [''=> ["1",'=',"1"] ];
17
+    private array $where = [''=> ["1", '=', "1"]];
18 18
     
19 19
 
20 20
     private function mountTable_Field(string $field, $value = null)
@@ -27,16 +27,16 @@  discard block
 block discarded – undo
27 27
         $type = $value;
28 28
         $maxlength = null;
29 29
 
30
-        if(strpos($value,'(')){
31
-            $type = (array_key_exists( substr($value,0,strpos($value,'(')) , ['varchar','char','text'])) ? 'string' : $type;
32
-            $type = (array_key_exists( substr($value,0,strpos($value,'(')) , ['tinyint','mediumint','smallint','bigtint','int'])) ? 'int' : $type;
33
-            $type = (array_key_exists( substr($value,0,strpos($value,'(')) , ['decimal','float','double','real'])) ? 'float' : $type;
30
+        if (strpos($value, '(')) {
31
+            $type = (array_key_exists(substr($value, 0, strpos($value, '(')), ['varchar', 'char', 'text'])) ? 'string' : $type;
32
+            $type = (array_key_exists(substr($value, 0, strpos($value, '(')), ['tinyint', 'mediumint', 'smallint', 'bigtint', 'int'])) ? 'int' : $type;
33
+            $type = (array_key_exists(substr($value, 0, strpos($value, '(')), ['decimal', 'float', 'double', 'real'])) ? 'float' : $type;
34 34
         }
35 35
 
36
-        $maxlength = (array_key_exists( $type , ['string','float','int'])) ? substr($value,(strpos($value,'(')+1),-1) : $maxlength;
37
-        $maxlength = (array_key_exists( $type , ['date'])) ? 10 : $maxlength;
38
-        $maxlength = (array_key_exists( $type , ['datetime'])) ? 19 : $maxlength;
39
-        $maxlength = (array_key_exists( $type , ['boolean'])) ? 1 : $maxlength;
36
+        $maxlength = (array_key_exists($type, ['string', 'float', 'int'])) ? substr($value, (strpos($value, '(') + 1), -1) : $maxlength;
37
+        $maxlength = (array_key_exists($type, ['date'])) ? 10 : $maxlength;
38
+        $maxlength = (array_key_exists($type, ['datetime'])) ? 19 : $maxlength;
39
+        $maxlength = (array_key_exists($type, ['boolean'])) ? 1 : $maxlength;
40 40
 
41 41
         $this->$field = ['maxlength' => $maxlength];
42 42
         $this->$field = ['type' => $type];
@@ -79,11 +79,11 @@  discard block
 block discarded – undo
79 79
 
80 80
     public function check_where_array(array $where)
81 81
     {
82
-        if(count($where) != 3){
83
-            throw new Exception("Condition where set incorrectly: ".implode(' ',$where));
82
+        if (count($where) != 3) {
83
+            throw new Exception("Condition where set incorrectly: ".implode(' ', $where));
84 84
         }
85 85
 
86
-        if(!array_key_exists($where[0],$this->data) && $this->full){
86
+        if (!array_key_exists($where[0], $this->data) && $this->full) {
87 87
             throw new Exception("{$where[0]} field does not exist in the table {$this->table}.");
88 88
         }
89 89
     }
@@ -91,14 +91,14 @@  discard block
 block discarded – undo
91 91
     private function mountRemove(): array
92 92
     {
93 93
         $return = ['data' => [], 'where' => []];
94
-        foreach($this->where as $clause => $condition){
95
-            if(strlen($clause) === 0){
94
+        foreach ($this->where as $clause => $condition) {
95
+            if (strlen($clause) === 0) {
96 96
                 $return['where'] .= " {$clause} {$condition[0]} {$condition[1]} :q_{$condition[0]} ";
97 97
                 $return['data'] .= "q_{$condition[0]}={$condition[2]}&";
98 98
                 continue;
99 99
             }
100 100
                 
101
-            foreach($condition as $value){
101
+            foreach ($condition as $value) {
102 102
                 $return['where'] .= " {$clause} {$value[0]} {$value[1]} :q_{$value[0]} ";
103 103
                 $return['data'] .= "q_{$value[0]}={$value[2]}&";
104 104
             }
@@ -111,11 +111,11 @@  discard block
 block discarded – undo
111 111
         $return = ['data' => []];
112 112
 
113 113
         foreach ($this->data as $key => $value) {
114
-            if(strstr($this->data[$key]['extra'],'auto_increment') && $key !== $this->primary){
114
+            if (strstr($this->data[$key]['extra'], 'auto_increment') && $key !== $this->primary) {
115 115
                 continue;
116 116
             }
117 117
 
118
-            if(($this->data[$key]['changed'] && $this->data[$key]['upgradeable']) || $this->primary === $key){
118
+            if (($this->data[$key]['changed'] && $this->data[$key]['upgradeable']) || $this->primary === $key) {
119 119
                 $return['data'][$key] = $this->data[$key]['value'];
120 120
             }
121 121
         }
@@ -127,7 +127,7 @@  discard block
 block discarded – undo
127 127
     {
128 128
         $this->transaction('begin');
129 129
 
130
-        try{
130
+        try {
131 131
             $this->update(
132 132
                 $this->mountSave()['data'],
133 133
                 "{$this->primary}=:{$this->primary}", 
@@ -137,7 +137,7 @@  discard block
 block discarded – undo
137 137
             $this->check_fail();
138 138
 
139 139
             $this->transaction('commit');
140
-        }catch(Exception $er){
140
+        } catch (Exception $er) {
141 141
             $this->transaction('rollback');
142 142
             throw $er;
143 143
         }
@@ -153,7 +153,7 @@  discard block
 block discarded – undo
153 153
 
154 154
             $key = (!$key) ? '' : " {$key} ";
155 155
 
156
-            if(is_array($value[0])){
156
+            if (is_array($value[0])) {
157 157
 
158 158
                 foreach ($value as $k => $v) {
159 159
                     $return['where'] .= " {$key} {$v[0]} {$v[1]} :q_{$v[0]} ";
@@ -172,21 +172,21 @@  discard block
 block discarded – undo
172 172
 
173 173
     private function mountSelect()
174 174
     {
175
-        $select = implode(',',array_keys($this->select));
175
+        $select = implode(',', array_keys($this->select));
176 176
 
177
-        $this->query = str_replace('*', $select,$this->query);
177
+        $this->query = str_replace('*', $select, $this->query);
178 178
     }
179 179
 
180 180
     private function mountLimit()
181 181
     {
182
-        if(!is_null($this->limit)){
182
+        if (!is_null($this->limit)) {
183 183
             $this->query .= " LIMIT {$this->limit}";
184 184
         }
185 185
     }
186 186
 
187 187
     private function mountOffset()
188 188
     {
189
-        if(!is_null($this->offset)){
189
+        if (!is_null($this->offset)) {
190 190
             $this->query .= " OFFSET {$this->offset}";
191 191
         }
192 192
     }
Please login to merge, or discard this patch.
Braces   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -137,7 +137,7 @@
 block discarded – undo
137 137
             $this->check_fail();
138 138
 
139 139
             $this->transaction('commit');
140
-        }catch(Exception $er){
140
+        } catch(Exception $er){
141 141
             $this->transaction('rollback');
142 142
             throw $er;
143 143
         }
Please login to merge, or discard this patch.
src/DataTrait.php 2 patches
Spacing   +29 added lines, -29 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
-trait DataTrait{
7
+trait DataTrait {
8 8
     protected ?string $table = null;
9 9
     protected ?string $primary = null;
10 10
 
@@ -29,16 +29,16 @@  discard block
 block discarded – undo
29 29
         return $this;
30 30
     }
31 31
 
32
-    public function __set(string $prop,$value): Datamanager
32
+    public function __set(string $prop, $value): Datamanager
33 33
     {
34 34
 
35
-        if(is_array($value)){
35
+        if (is_array($value)) {
36 36
             $attr = array_keys($value)[0];
37 37
             $this->data[$prop][$attr] = $value[$attr];
38 38
             return $this;
39 39
         }
40 40
 
41
-        if($this->full && !array_key_exists($prop,$this->data)){
41
+        if ($this->full && !array_key_exists($prop, $this->data)) {
42 42
             throw new Exception("{$prop} field does not exist in the table {$this->table}.");
43 43
         }
44 44
 
@@ -55,7 +55,7 @@  discard block
 block discarded – undo
55 55
 
56 56
     public function __get(string $field)
57 57
     {
58
-        if($this->full && !array_key_exists($field,$this->data)){
58
+        if ($this->full && !array_key_exists($field, $this->data)) {
59 59
             throw new Exception("{$field} field does not exist in the table {$this->table}.");
60 60
         }
61 61
 
@@ -72,7 +72,7 @@  discard block
 block discarded – undo
72 72
         $deniable = (is_array($deniable)) ? $deniable : [$deniable];
73 73
 
74 74
         foreach ($deniable as $field) {
75
-            if(!array_key_exists($field,$this->data)){
75
+            if (!array_key_exists($field, $this->data)) {
76 76
                 throw new Exception("{$field} field does not exist in the table {$this->table}.");
77 77
             }
78 78
 
@@ -92,11 +92,11 @@  discard block
 block discarded – undo
92 92
 
93 93
     public function orderBy(string $field, string $ord = 'ASC'): Datamanager
94 94
     {
95
-        if(!array_key_exists(str_replace(['asc','ASC','desc','DESC',' '],'',$field),$this->data) && $this->full){
95
+        if (!array_key_exists(str_replace(['asc', 'ASC', 'desc', 'DESC', ' '], '', $field), $this->data) && $this->full) {
96 96
             throw new Exception("{$field} field does not exist in the table {$this->table}.");
97 97
         }
98 98
 
99
-        if(strpos(strtolower($field),'asc') || strpos(strtolower($field),'desc')){
99
+        if (strpos(strtolower($field), 'asc') || strpos(strtolower($field), 'desc')) {
100 100
             $ord = '';
101 101
         }
102 102
 
@@ -111,7 +111,7 @@  discard block
 block discarded – undo
111 111
 
112 112
         foreach ($params as $field) {
113 113
 
114
-            if(!array_key_exists($field,$this->data) && $this->full){
114
+            if (!array_key_exists($field, $this->data) && $this->full) {
115 115
                 throw new Exception("{$field} field does not exist in the table {$this->table}.");
116 116
             }
117 117
 
@@ -124,11 +124,11 @@  discard block
 block discarded – undo
124 124
 
125 125
     public function where(array $where): Datamanager
126 126
     {
127
-        $this->where['AND'] = (array_key_exists('AND',$this->where)) ?? '';
127
+        $this->where['AND'] = (array_key_exists('AND', $this->where)) ?? '';
128 128
         $w = [];
129 129
         foreach ($where as $condition => $values) {
130 130
 
131
-            if(!is_array($values)){
131
+            if (!is_array($values)) {
132 132
                 $w['AND'][] = $values;
133 133
                 continue;
134 134
             }
@@ -139,7 +139,7 @@  discard block
 block discarded – undo
139 139
                        
140 140
         }
141 141
 
142
-        $this->where = array_merge($this->where,$w);
142
+        $this->where = array_merge($this->where, $w);
143 143
 
144 144
         return $this;
145 145
     }
@@ -152,7 +152,7 @@  discard block
 block discarded – undo
152 152
 
153 153
     public function offset(int $offset): Datamanager
154 154
     {
155
-        if(is_null($this->limit)){
155
+        if (is_null($this->limit)) {
156 156
             throw new Exception("The limit must be set before the offset.");
157 157
         }
158 158
 
@@ -182,7 +182,7 @@  discard block
 block discarded – undo
182 182
 
183 183
         foreach ($arrayValues as $key => $value) {
184 184
 
185
-            if(!array_key_exists($key,$this->data)){
185
+            if (!array_key_exists($key, $this->data)) {
186 186
                 throw new Exception("{$key} field does not exist in the table {$this->table}.");
187 187
             }
188 188
 
@@ -196,7 +196,7 @@  discard block
 block discarded – undo
196 196
         $string = '';
197 197
         foreach ($this->data as $key => $value) {
198 198
 
199
-            if(gettype($value)==='object'){
199
+            if (gettype($value) === 'object') {
200 200
                 $value = $value->getData()[$this->primary]['value'];
201 201
             }
202 202
 
@@ -207,19 +207,19 @@  discard block
 block discarded – undo
207 207
 
208 208
     public function remove(?bool $exec = false): Datamanager
209 209
     {
210
-        if(!$exec){
210
+        if (!$exec) {
211 211
             $this->clause = 'remove';    
212 212
             return $this;
213 213
         }
214 214
 
215 215
         $this->clause = null;
216 216
 
217
-        if(count($this->where) == 1){
217
+        if (count($this->where) == 1) {
218 218
             $this->removeById();
219 219
             return $this;
220 220
         }
221 221
 
222
-        $this->delete($this->mountRemove()['where'], substr( $this->mountRemove()['data'] ,0,-1) );
222
+        $this->delete($this->mountRemove()['where'], substr($this->mountRemove()['data'], 0, -1));
223 223
 
224 224
         $this->check_fail();
225 225
             
@@ -228,7 +228,7 @@  discard block
 block discarded – undo
228 228
 
229 229
     private function removeById(): bool
230 230
     {
231
-        $delete = $this->delete("{$this->primary}=:{$this->primary}","{$this->primary}={$this->getData()[$this->primary]['value']}");
231
+        $delete = $this->delete("{$this->primary}=:{$this->primary}", "{$this->primary}={$this->getData()[$this->primary]['value']}");
232 232
 
233 233
         $this->check_fail();
234 234
 
@@ -242,11 +242,11 @@  discard block
 block discarded – undo
242 242
         $data = [];
243 243
 
244 244
         foreach ($this->data as $key => $value) {
245
-            if(strstr($this->data[$key]['extra'],'auto_increment')){
245
+            if (strstr($this->data[$key]['extra'], 'auto_increment')) {
246 246
                 continue;
247 247
             }
248 248
 
249
-            if(strlen($value['value']) > $value['maxlength']){
249
+            if (strlen($value['value']) > $value['maxlength']) {
250 250
                 throw new Exception("The information provided for column {$key} of table {$this->table} exceeded that allowed.");
251 251
             }
252 252
 
@@ -256,7 +256,7 @@  discard block
 block discarded – undo
256 256
         }
257 257
 
258 258
         $this->transaction('begin');
259
-        try{
259
+        try {
260 260
            
261 261
             $id = $this->insert($data);
262 262
 
@@ -266,7 +266,7 @@  discard block
 block discarded – undo
266 266
             
267 267
             $this->transaction('commit');
268 268
 
269
-        }catch(Exception $er){
269
+        } catch (Exception $er) {
270 270
             $this->transaction('rollback');
271 271
             throw $er;
272 272
         }
@@ -276,13 +276,13 @@  discard block
 block discarded – undo
276 276
 
277 277
     public function toEntity()
278 278
     {
279
-        if($this->getCount() === 0){
279
+        if ($this->getCount() === 0) {
280 280
             return null;
281 281
         }
282 282
 
283 283
         $entity = $this->setByDatabase($this->result[0]);
284 284
 
285
-        if(count($this->result) > 1){
285
+        if (count($this->result) > 1) {
286 286
             $entity = [];
287 287
             foreach ($this->result as $key => $value) {
288 288
                 $entity[] = $this->setByDatabase($value);
@@ -294,13 +294,13 @@  discard block
 block discarded – undo
294 294
 
295 295
     public function findById($id): Datamanager
296 296
     {
297
-        $this->where([$this->primary,'=',$id]);
297
+        $this->where([$this->primary, '=', $id]);
298 298
         return $this;
299 299
     }
300 300
 
301 301
     public function execute(): Datamanager
302 302
     {
303
-        if(!is_null($this->clause) && $this->clause == 'remove'){
303
+        if (!is_null($this->clause) && $this->clause == 'remove') {
304 304
             return $this->remove(true);
305 305
         }
306 306
 
@@ -308,7 +308,7 @@  discard block
 block discarded – undo
308 308
         
309 309
         $this->mountSelect();
310 310
         
311
-        $where = substr($this->mountWhereExec()['where'],0,-1);
311
+        $where = substr($this->mountWhereExec()['where'], 0, -1);
312 312
         $this->query .= " WHERE {$where} ";
313 313
 
314 314
         $this->query .= $this->order;
@@ -330,7 +330,7 @@  discard block
 block discarded – undo
330 330
     {
331 331
         $this->query = " SELECT * FROM {$this->table} ";
332 332
 
333
-        if(is_int($key)){
333
+        if (is_int($key)) {
334 334
             return $this->findById($key);
335 335
         }
336 336
 
Please login to merge, or discard this patch.
Braces   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -266,7 +266,7 @@
 block discarded – undo
266 266
             
267 267
             $this->transaction('commit');
268 268
 
269
-        }catch(Exception $er){
269
+        } catch(Exception $er){
270 270
             $this->transaction('rollback');
271 271
             throw $er;
272 272
         }
Please login to merge, or discard this patch.