Passed
Push — master ( 620cae...c5a08c )
by Henri
01:20
created
src/Datamanager.php 1 patch
Spacing   +24 added lines, -24 removed lines patch added patch discarded remove patch
@@ -10,7 +10,7 @@  discard block
 block discarded – undo
10 10
     
11 11
     
12 12
 
13
-    private array $where = [''=> ["1",'=',"1"] ];
13
+    private array $where = [''=> ["1", '=', "1"]];
14 14
 
15 15
     private function mountTable_Field(string $field, $value = null)
16 16
     {
@@ -22,16 +22,16 @@  discard block
 block discarded – undo
22 22
         $type = $value;
23 23
         $maxlength = null;
24 24
 
25
-        if(strpos($value,'(')){
26
-            $type = (in_array( substr($value, 0, strpos($value,'(')) , ['varchar','char','text'])) ? 'string' : $type;
27
-            $type = (in_array( substr($value, 0, strpos($value,'(')) , ['tinyint','mediumint','smallint','bigtint','int'])) ? 'int' : $type;
28
-            $type = (in_array( substr($value, 0, strpos($value,'(')) , ['decimal','float','double','real'])) ? 'float' : $type;
25
+        if (strpos($value, '(')) {
26
+            $type = (in_array(substr($value, 0, strpos($value, '(')), ['varchar', 'char', 'text'])) ? 'string' : $type;
27
+            $type = (in_array(substr($value, 0, strpos($value, '(')), ['tinyint', 'mediumint', 'smallint', 'bigtint', 'int'])) ? 'int' : $type;
28
+            $type = (in_array(substr($value, 0, strpos($value, '(')), ['decimal', 'float', 'double', 'real'])) ? 'float' : $type;
29 29
         }
30 30
 
31
-        $maxlength = (in_array( $type , ['string','float','int'])) ? substr($value,(strpos($value,'(')+1),-1) : $maxlength;
32
-        $maxlength = (in_array( $type , ['date'])) ? 10 : $maxlength;
33
-        $maxlength = (in_array( $type , ['datetime'])) ? 19 : $maxlength;
34
-        $maxlength = (in_array( $type , ['boolean'])) ? 1 : $maxlength;
31
+        $maxlength = (in_array($type, ['string', 'float', 'int'])) ? substr($value, (strpos($value, '(') + 1), -1) : $maxlength;
32
+        $maxlength = (in_array($type, ['date'])) ? 10 : $maxlength;
33
+        $maxlength = (in_array($type, ['datetime'])) ? 19 : $maxlength;
34
+        $maxlength = (in_array($type, ['boolean'])) ? 1 : $maxlength;
35 35
 
36 36
         $this->$field = ['maxlength' => $maxlength];
37 37
         $this->$field = ['type' => $type];
@@ -74,11 +74,11 @@  discard block
 block discarded – undo
74 74
 
75 75
     public function check_where_array(array $where)
76 76
     {
77
-        if(count($where) != 3){
78
-            throw new Exception("Condition where set incorrectly: ".implode(' ',$where));
77
+        if (count($where) != 3) {
78
+            throw new Exception("Condition where set incorrectly: ".implode(' ', $where));
79 79
         }
80 80
 
81
-        if(!array_key_exists($where[0],$this->data) && $this->full){
81
+        if (!array_key_exists($where[0], $this->data) && $this->full) {
82 82
             throw new Exception("{$where[0]} field does not exist in the table {$this->table}.");
83 83
         }
84 84
     }
@@ -86,14 +86,14 @@  discard block
 block discarded – undo
86 86
     private function mountRemove(): array
87 87
     {
88 88
         $return = ['data' => [], 'where' => ''];
89
-        foreach($this->where as $clause => $condition){
90
-            if(strlen($clause) === 0){
89
+        foreach ($this->where as $clause => $condition) {
90
+            if (strlen($clause) === 0) {
91 91
                 $return['where'] .= " {$clause} {$condition[0]} {$condition[1]} :q_{$condition[0]} ";
92 92
                 $return['data'] .= "q_{$condition[0]}={$condition[2]}&";
93 93
                 continue;
94 94
             }
95 95
                 
96
-            foreach($condition as $value){
96
+            foreach ($condition as $value) {
97 97
                 $return['where'] .= " {$clause} {$value[0]} {$value[1]} :q_{$value[0]} ";
98 98
                 $return['data'] .= "q_{$value[0]}={$value[2]}&";
99 99
             }
@@ -106,11 +106,11 @@  discard block
 block discarded – undo
106 106
         $return = ['data' => []];
107 107
 
108 108
         foreach ($this->data as $key => $value) {
109
-            if(strstr($this->data[$key]['extra'],'auto_increment') && $key !== $this->primary){
109
+            if (strstr($this->data[$key]['extra'], 'auto_increment') && $key !== $this->primary) {
110 110
                 continue;
111 111
             }
112 112
 
113
-            if(($this->data[$key]['changed'] && $this->data[$key]['upgradeable']) || $this->primary === $key){
113
+            if (($this->data[$key]['changed'] && $this->data[$key]['upgradeable']) || $this->primary === $key) {
114 114
                 $return['data'][$key] = $this->data[$key]['value'];
115 115
             }
116 116
         }
@@ -122,7 +122,7 @@  discard block
 block discarded – undo
122 122
     {
123 123
         $this->transaction('begin');
124 124
 
125
-        try{
125
+        try {
126 126
             $this->update(
127 127
                 $this->mountSave()['data'],
128 128
                 "{$this->primary}=:{$this->primary}", 
@@ -132,7 +132,7 @@  discard block
 block discarded – undo
132 132
             $this->check_fail();
133 133
 
134 134
             $this->transaction('commit');
135
-        }catch(Exception $er){
135
+        } catch (Exception $er) {
136 136
             $this->transaction('rollback');
137 137
             throw $er;
138 138
         }
@@ -148,7 +148,7 @@  discard block
 block discarded – undo
148 148
 
149 149
             $key = (!$key) ? '' : " {$key} ";
150 150
 
151
-            if(is_array($value[0])){
151
+            if (is_array($value[0])) {
152 152
 
153 153
                 foreach ($value as $k => $v) {
154 154
                     $return['where'] .= " {$key} {$v[0]} {$v[1]} :q_{$v[0]} ";
@@ -167,21 +167,21 @@  discard block
 block discarded – undo
167 167
 
168 168
     private function mountSelect()
169 169
     {
170
-        $select = implode(',',array_keys($this->select));
170
+        $select = implode(',', array_keys($this->select));
171 171
 
172
-        $this->query = str_replace('*', $select,$this->query);
172
+        $this->query = str_replace('*', $select, $this->query);
173 173
     }
174 174
 
175 175
     private function mountLimit()
176 176
     {
177
-        if(!is_null($this->limit)){
177
+        if (!is_null($this->limit)) {
178 178
             $this->query .= " LIMIT {$this->limit}";
179 179
         }
180 180
     }
181 181
 
182 182
     private function mountOffset()
183 183
     {
184
-        if(!is_null($this->offset)){
184
+        if (!is_null($this->offset)) {
185 185
             $this->query .= " OFFSET {$this->offset}";
186 186
         }
187 187
     }
Please login to merge, or discard this patch.
src/DataTrait.php 1 patch
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
 
@@ -34,16 +34,16 @@  discard block
 block discarded – undo
34 34
         return $this;
35 35
     }
36 36
 
37
-    public function __set(string $prop,$value)
37
+    public function __set(string $prop, $value)
38 38
     {
39 39
 
40
-        if(is_array($value)){
40
+        if (is_array($value)) {
41 41
             $attr = array_keys($value)[0];
42 42
             $this->data[$prop][$attr] = $value[$attr];
43 43
             return $this;
44 44
         }
45 45
 
46
-        if($this->full && !array_key_exists($prop,$this->data)){
46
+        if ($this->full && !array_key_exists($prop, $this->data)) {
47 47
             throw new Exception("{$prop} field does not exist in the table {$this->table}.");
48 48
         }
49 49
 
@@ -60,7 +60,7 @@  discard block
 block discarded – undo
60 60
 
61 61
     public function __get(string $field)
62 62
     {
63
-        if($this->full && !array_key_exists($field,$this->data)){
63
+        if ($this->full && !array_key_exists($field, $this->data)) {
64 64
             throw new Exception("{$field} field does not exist in the table {$this->table}.");
65 65
         }
66 66
 
@@ -77,7 +77,7 @@  discard block
 block discarded – undo
77 77
         $deniable = (is_array($deniable)) ? $deniable : [$deniable];
78 78
 
79 79
         foreach ($deniable as $field) {
80
-            if(!array_key_exists($field,$this->data)){
80
+            if (!array_key_exists($field, $this->data)) {
81 81
                 throw new Exception("{$field} field does not exist in the table {$this->table}.");
82 82
             }
83 83
 
@@ -97,11 +97,11 @@  discard block
 block discarded – undo
97 97
 
98 98
     public function orderBy(string $field, string $ord = 'ASC')
99 99
     {
100
-        if(!array_key_exists(str_replace(['asc','ASC','desc','DESC',' '],'',$field),$this->data) && $this->full){
100
+        if (!array_key_exists(str_replace(['asc', 'ASC', 'desc', 'DESC', ' '], '', $field), $this->data) && $this->full) {
101 101
             throw new Exception("{$field} field does not exist in the table {$this->table}.");
102 102
         }
103 103
 
104
-        if(strpos(strtolower($field),'asc') || strpos(strtolower($field),'desc')){
104
+        if (strpos(strtolower($field), 'asc') || strpos(strtolower($field), 'desc')) {
105 105
             $ord = '';
106 106
         }
107 107
 
@@ -116,7 +116,7 @@  discard block
 block discarded – undo
116 116
 
117 117
         foreach ($params as $field) {
118 118
 
119
-            if(!array_key_exists($field,$this->data) && $this->full){
119
+            if (!array_key_exists($field, $this->data) && $this->full) {
120 120
                 throw new Exception("{$field} field does not exist in the table {$this->table}.");
121 121
             }
122 122
 
@@ -129,11 +129,11 @@  discard block
 block discarded – undo
129 129
 
130 130
     public function where(array $where)
131 131
     {
132
-        $this->where['AND'] = (array_key_exists('AND',$this->where)) ?? '';
132
+        $this->where['AND'] = (array_key_exists('AND', $this->where)) ?? '';
133 133
         $w = [];
134 134
         foreach ($where as $condition => $values) {
135 135
 
136
-            if(!is_array($values)){
136
+            if (!is_array($values)) {
137 137
                 $w['AND'][] = $values;
138 138
                 continue;
139 139
             }
@@ -144,7 +144,7 @@  discard block
 block discarded – undo
144 144
                        
145 145
         }
146 146
 
147
-        $this->where = array_merge($this->where,$w);
147
+        $this->where = array_merge($this->where, $w);
148 148
 
149 149
         return $this;
150 150
     }
@@ -157,7 +157,7 @@  discard block
 block discarded – undo
157 157
 
158 158
     public function offset(int $offset)
159 159
     {
160
-        if(is_null($this->limit)){
160
+        if (is_null($this->limit)) {
161 161
             throw new Exception("The limit must be set before the offset.");
162 162
         }
163 163
 
@@ -187,7 +187,7 @@  discard block
 block discarded – undo
187 187
 
188 188
         foreach ($arrayValues as $key => $value) {
189 189
 
190
-            if(!array_key_exists($key,$this->data)){
190
+            if (!array_key_exists($key, $this->data)) {
191 191
                 throw new Exception("{$key} field does not exist in the table {$this->table}.");
192 192
             }
193 193
 
@@ -201,7 +201,7 @@  discard block
 block discarded – undo
201 201
         $string = '';
202 202
         foreach ($this->data as $key => $value) {
203 203
 
204
-            if(gettype($value)==='object'){
204
+            if (gettype($value) === 'object') {
205 205
                 $value = $value->getData()[$this->primary]['value'];
206 206
             }
207 207
 
@@ -212,19 +212,19 @@  discard block
 block discarded – undo
212 212
 
213 213
     public function remove(?bool $exec = false)
214 214
     {
215
-        if(!$exec){
215
+        if (!$exec) {
216 216
             $this->clause = 'remove';    
217 217
             return $this;
218 218
         }
219 219
 
220 220
         $this->clause = null;
221 221
 
222
-        if(count($this->where) == 1){
222
+        if (count($this->where) == 1) {
223 223
             $this->removeById();
224 224
             return $this;
225 225
         }
226 226
 
227
-        $this->delete($this->mountRemove()['where'], substr( $this->mountRemove()['data'] ,0,-1) );
227
+        $this->delete($this->mountRemove()['where'], substr($this->mountRemove()['data'], 0, -1));
228 228
 
229 229
         $this->check_fail();
230 230
             
@@ -233,7 +233,7 @@  discard block
 block discarded – undo
233 233
 
234 234
     private function removeById(): bool
235 235
     {
236
-        $delete = $this->delete("{$this->primary}=:{$this->primary}","{$this->primary}={$this->getData()[$this->primary]['value']}");
236
+        $delete = $this->delete("{$this->primary}=:{$this->primary}", "{$this->primary}={$this->getData()[$this->primary]['value']}");
237 237
 
238 238
         $this->check_fail();
239 239
 
@@ -247,11 +247,11 @@  discard block
 block discarded – undo
247 247
         $data = [];
248 248
 
249 249
         foreach ($this->data as $key => $value) {
250
-            if(strstr($this->data[$key]['extra'],'auto_increment')){
250
+            if (strstr($this->data[$key]['extra'], 'auto_increment')) {
251 251
                 continue;
252 252
             }
253 253
 
254
-            if(strlen($value['value']) > $value['maxlength']){
254
+            if (strlen($value['value']) > $value['maxlength']) {
255 255
                 throw new Exception("The information provided for column {$key} of table {$this->table} exceeded that allowed.");
256 256
             }
257 257
 
@@ -261,7 +261,7 @@  discard block
 block discarded – undo
261 261
         }
262 262
 
263 263
         $this->transaction('begin');
264
-        try{
264
+        try {
265 265
            
266 266
             $id = $this->insert($data);
267 267
 
@@ -271,7 +271,7 @@  discard block
 block discarded – undo
271 271
             
272 272
             $this->transaction('commit');
273 273
 
274
-        }catch(Exception $er){
274
+        } catch (Exception $er) {
275 275
             $this->transaction('rollback');
276 276
             throw $er;
277 277
         }
@@ -281,13 +281,13 @@  discard block
 block discarded – undo
281 281
 
282 282
     public function toEntity()
283 283
     {
284
-        if($this->getCount() === 0){
284
+        if ($this->getCount() === 0) {
285 285
             return null;
286 286
         }
287 287
 
288 288
         $entity = $this->setByDatabase($this->result[0]);
289 289
 
290
-        if(count($this->result) > 1){
290
+        if (count($this->result) > 1) {
291 291
             $entity = [];
292 292
             foreach ($this->result as $key => $value) {
293 293
                 $entity[] = $this->setByDatabase($value);
@@ -299,13 +299,13 @@  discard block
 block discarded – undo
299 299
 
300 300
     public function findById($id)
301 301
     {
302
-        $this->where([$this->primary,'=',$id]);
302
+        $this->where([$this->primary, '=', $id]);
303 303
         return $this;
304 304
     }
305 305
 
306 306
     public function execute()
307 307
     {
308
-        if(!is_null($this->clause) && $this->clause == 'remove'){
308
+        if (!is_null($this->clause) && $this->clause == 'remove') {
309 309
             return $this->remove(true);
310 310
         }
311 311
 
@@ -313,7 +313,7 @@  discard block
 block discarded – undo
313 313
         
314 314
         $this->mountSelect();
315 315
         
316
-        $where = substr($this->mountWhereExec()['where'],0,-1);
316
+        $where = substr($this->mountWhereExec()['where'], 0, -1);
317 317
         $this->query .= " WHERE {$where} ";
318 318
 
319 319
         $this->query .= $this->order;
@@ -335,7 +335,7 @@  discard block
 block discarded – undo
335 335
     {
336 336
         $this->query = " SELECT * FROM {$this->table} ";
337 337
 
338
-        if(is_int($key)){
338
+        if (is_int($key)) {
339 339
             return $this->findById($key);
340 340
         }
341 341
 
Please login to merge, or discard this patch.