@@ -10,7 +10,7 @@ discard block |
||
10 | 10 | |
11 | 11 | $entity = new User(); |
12 | 12 | |
13 | -try{ |
|
13 | +try { |
|
14 | 14 | /* Set new info for insert in database */ |
15 | 15 | $entity->name = 'Henri Azevedo'; |
16 | 16 | $entity->email = '[email protected]'; |
@@ -23,7 +23,7 @@ discard block |
||
23 | 23 | $user = $entity->find()->execute()->first()->toEntity(); |
24 | 24 | |
25 | 25 | /* Search only for columns defined in advance */ |
26 | - $user = $entity->find()->only(['name','email'])->execute()->first(); |
|
26 | + $user = $entity->find()->only(['name', 'email'])->execute()->first(); |
|
27 | 27 | $name = $user->name; |
28 | 28 | $email = $user->email; |
29 | 29 | |
@@ -45,10 +45,10 @@ discard block |
||
45 | 45 | |
46 | 46 | /* Remove by cause *Where* */ |
47 | 47 | $user->remove()->where([ |
48 | - ['name','=','Other Name'], |
|
49 | - 'OR' => ['email','LIKE','[email protected]'] |
|
48 | + ['name', '=', 'Other Name'], |
|
49 | + 'OR' => ['email', 'LIKE', '[email protected]'] |
|
50 | 50 | ])->execute(); |
51 | -}catch(Exception $er){ |
|
51 | +} catch (Exception $er) { |
|
52 | 52 | |
53 | 53 | die("Code Error: {$er->getCode()}, Line: {$er->getLine()}, File: {$er->getFile()}, Message: {$er->getMessage()}."); |
54 | 54 |
@@ -4,36 +4,36 @@ |
||
4 | 4 | |
5 | 5 | use Exception; |
6 | 6 | |
7 | -trait CheckTrait{ |
|
7 | +trait CheckTrait { |
|
8 | 8 | |
9 | 9 | protected function check_where_array(array $where) |
10 | 10 | { |
11 | - if(count($where) != 3){ |
|
12 | - throw new Exception("Condition where set incorrectly: ".implode(' ',$where)); |
|
11 | + if (count($where) != 3) { |
|
12 | + throw new Exception("Condition where set incorrectly: ".implode(' ', $where)); |
|
13 | 13 | } |
14 | 14 | |
15 | - if(!array_key_exists($where[0],$this->data) && $this->full){ |
|
15 | + if (!array_key_exists($where[0], $this->data) && $this->full) { |
|
16 | 16 | throw new Exception("{$where[0]} field does not exist in the table {$this->table}."); |
17 | 17 | } |
18 | 18 | } |
19 | 19 | |
20 | 20 | protected function isSettable(string $prop) |
21 | 21 | { |
22 | - if($this->full && !array_key_exists($prop,$this->data)){ |
|
22 | + if ($this->full && !array_key_exists($prop, $this->data)) { |
|
23 | 23 | throw new Exception("{$prop} field does not exist in the table {$this->table}."); |
24 | 24 | } |
25 | 25 | } |
26 | 26 | |
27 | 27 | protected function checkLimit() |
28 | 28 | { |
29 | - if(is_null($this->limit)){ |
|
29 | + if (is_null($this->limit)) { |
|
30 | 30 | throw new Exception("The limit must be set before the offset."); |
31 | 31 | } |
32 | 32 | } |
33 | 33 | |
34 | - protected function checkMaxlength(string $field, $val , $max) |
|
34 | + protected function checkMaxlength(string $field, $val, $max) |
|
35 | 35 | { |
36 | - if(strlen($val) > $max){ |
|
36 | + if (strlen($val) > $max) { |
|
37 | 37 | throw new Exception("The information provided for column {$field} of table {$this->table} exceeded that allowed."); |
38 | 38 | } |
39 | 39 | } |
@@ -11,20 +11,20 @@ discard block |
||
11 | 11 | protected array $data = []; |
12 | 12 | |
13 | 13 | |
14 | - private array $where = [''=> ["1",'=',"1"] ]; |
|
14 | + private array $where = [''=> ["1", '=', "1"]]; |
|
15 | 15 | |
16 | 16 | |
17 | 17 | private function mountRemove(): array |
18 | 18 | { |
19 | 19 | $return = ['data' => '', 'where' => '']; |
20 | - foreach($this->where as $clause => $condition){ |
|
21 | - if(strlen($clause) === 0){ |
|
20 | + foreach ($this->where as $clause => $condition) { |
|
21 | + if (strlen($clause) === 0) { |
|
22 | 22 | $return['where'] .= " {$clause} {$condition[0]} {$condition[1]} :q_{$condition[0]} "; |
23 | 23 | $return['data'] .= "q_{$condition[0]}={$condition[2]}&"; |
24 | 24 | continue; |
25 | 25 | } |
26 | 26 | |
27 | - foreach($condition as $value){ |
|
27 | + foreach ($condition as $value) { |
|
28 | 28 | $return['where'] .= " {$clause} {$value[0]} {$value[1]} :q_{$value[0]} "; |
29 | 29 | $return['data'] .= "q_{$value[0]}={$value[2]}&"; |
30 | 30 | } |
@@ -37,11 +37,11 @@ discard block |
||
37 | 37 | $return = ['data' => []]; |
38 | 38 | |
39 | 39 | foreach ($this->data as $key => $value) { |
40 | - if(strstr($this->data[$key]['extra'],'auto_increment') && $key !== $this->primary){ |
|
40 | + if (strstr($this->data[$key]['extra'], 'auto_increment') && $key !== $this->primary) { |
|
41 | 41 | continue; |
42 | 42 | } |
43 | 43 | |
44 | - if(($this->data[$key]['changed'] && $this->data[$key]['upgradeable']) || $this->primary === $key){ |
|
44 | + if (($this->data[$key]['changed'] && $this->data[$key]['upgradeable']) || $this->primary === $key) { |
|
45 | 45 | $return['data'][$key] = $this->data[$key]['value']; |
46 | 46 | } |
47 | 47 | } |
@@ -59,7 +59,7 @@ discard block |
||
59 | 59 | |
60 | 60 | $key = (!$key) ? '' : " {$key} "; |
61 | 61 | |
62 | - if(is_array($value[0])){ |
|
62 | + if (is_array($value[0])) { |
|
63 | 63 | |
64 | 64 | foreach ($value as $k => $v) { |
65 | 65 | $return['where'] .= " {$key} {$v[0]} {$v[1]} :q_{$v[0]} "; |
@@ -78,21 +78,21 @@ discard block |
||
78 | 78 | |
79 | 79 | private function mountSelect() |
80 | 80 | { |
81 | - $select = implode(',',array_keys($this->select)); |
|
81 | + $select = implode(',', array_keys($this->select)); |
|
82 | 82 | |
83 | - $this->query = str_replace('*', $select,$this->query); |
|
83 | + $this->query = str_replace('*', $select, $this->query); |
|
84 | 84 | } |
85 | 85 | |
86 | 86 | private function mountLimit() |
87 | 87 | { |
88 | - if(!is_null($this->limit)){ |
|
88 | + if (!is_null($this->limit)) { |
|
89 | 89 | $this->query .= " LIMIT {$this->limit}"; |
90 | 90 | } |
91 | 91 | } |
92 | 92 | |
93 | 93 | private function mountOffset() |
94 | 94 | { |
95 | - if(!is_null($this->offset)){ |
|
95 | + if (!is_null($this->offset)) { |
|
96 | 96 | $this->query .= " OFFSET {$this->offset}"; |
97 | 97 | } |
98 | 98 | } |
@@ -4,7 +4,7 @@ discard block |
||
4 | 4 | |
5 | 5 | use Exception; |
6 | 6 | |
7 | -trait DataTrait{ |
|
7 | +trait DataTrait { |
|
8 | 8 | use CrudTrait, CheckTrait; |
9 | 9 | |
10 | 10 | protected ?string $table = null; |
@@ -27,10 +27,10 @@ discard block |
||
27 | 27 | |
28 | 28 | |
29 | 29 | |
30 | - public function __set(string $prop,$value) |
|
30 | + public function __set(string $prop, $value) |
|
31 | 31 | { |
32 | 32 | |
33 | - if(is_array($value)){ |
|
33 | + if (is_array($value)) { |
|
34 | 34 | $attr = array_keys($value)[0]; |
35 | 35 | $this->data[$prop][$attr] = $value[$attr]; |
36 | 36 | return $this; |
@@ -65,7 +65,7 @@ discard block |
||
65 | 65 | $deniable = (is_array($deniable)) ? $deniable : [$deniable]; |
66 | 66 | |
67 | 67 | foreach ($deniable as $field) { |
68 | - if(!array_key_exists($field,$this->data)){ |
|
68 | + if (!array_key_exists($field, $this->data)) { |
|
69 | 69 | throw new Exception("{$field} field does not exist in the table {$this->table}."); |
70 | 70 | } |
71 | 71 | |
@@ -85,9 +85,9 @@ discard block |
||
85 | 85 | |
86 | 86 | public function orderBy(string $field, string $ord = 'ASC') |
87 | 87 | { |
88 | - $this->isSettable( str_replace(['asc','ASC','desc','DESC',' '],'',$field) ); |
|
88 | + $this->isSettable(str_replace(['asc', 'ASC', 'desc', 'DESC', ' '], '', $field)); |
|
89 | 89 | |
90 | - $ord = (strpos(strtolower($field),'asc') || strpos(strtolower($field),'desc')) ? '' : $ord; |
|
90 | + $ord = (strpos(strtolower($field), 'asc') || strpos(strtolower($field), 'desc')) ? '' : $ord; |
|
91 | 91 | |
92 | 92 | $this->order = " ORDER BY {$field} {$ord} "; |
93 | 93 | return $this; |
@@ -111,11 +111,11 @@ discard block |
||
111 | 111 | |
112 | 112 | public function where(array $where) |
113 | 113 | { |
114 | - $this->where['AND'] = (array_key_exists('AND',$this->where)) ?? ''; |
|
114 | + $this->where['AND'] = (array_key_exists('AND', $this->where)) ?? ''; |
|
115 | 115 | $w = []; |
116 | 116 | foreach ($where as $condition => $values) { |
117 | 117 | |
118 | - if(!is_array($values)){ |
|
118 | + if (!is_array($values)) { |
|
119 | 119 | $w['AND'][] = $values; |
120 | 120 | continue; |
121 | 121 | } |
@@ -126,7 +126,7 @@ discard block |
||
126 | 126 | |
127 | 127 | } |
128 | 128 | |
129 | - $this->where = array_merge($this->where,$w); |
|
129 | + $this->where = array_merge($this->where, $w); |
|
130 | 130 | |
131 | 131 | return $this; |
132 | 132 | } |
@@ -180,7 +180,7 @@ discard block |
||
180 | 180 | $string = ''; |
181 | 181 | foreach ($this->data as $key => $value) { |
182 | 182 | |
183 | - if(gettype($value)==='object'){ |
|
183 | + if (gettype($value) === 'object') { |
|
184 | 184 | $value = $value->getData()[$this->primary]['value']; |
185 | 185 | } |
186 | 186 | |
@@ -191,19 +191,19 @@ discard block |
||
191 | 191 | |
192 | 192 | public function remove(?bool $exec = false) |
193 | 193 | { |
194 | - if(!$exec){ |
|
194 | + if (!$exec) { |
|
195 | 195 | $this->clause = 'remove'; |
196 | 196 | return $this; |
197 | 197 | } |
198 | 198 | |
199 | 199 | $this->clause = null; |
200 | 200 | |
201 | - if(count($this->where) == 1){ |
|
201 | + if (count($this->where) == 1) { |
|
202 | 202 | $this->removeById(); |
203 | 203 | return $this; |
204 | 204 | } |
205 | 205 | |
206 | - $this->delete($this->mountRemove()['where'], substr( $this->mountRemove()['data'] ,0,-1) ); |
|
206 | + $this->delete($this->mountRemove()['where'], substr($this->mountRemove()['data'], 0, -1)); |
|
207 | 207 | |
208 | 208 | $this->check_fail(); |
209 | 209 | |
@@ -212,7 +212,7 @@ discard block |
||
212 | 212 | |
213 | 213 | private function removeById(): bool |
214 | 214 | { |
215 | - $delete = $this->delete("{$this->primary}=:{$this->primary}","{$this->primary}={$this->getData()[$this->primary]['value']}"); |
|
215 | + $delete = $this->delete("{$this->primary}=:{$this->primary}", "{$this->primary}={$this->getData()[$this->primary]['value']}"); |
|
216 | 216 | |
217 | 217 | $this->check_fail(); |
218 | 218 | |
@@ -226,7 +226,7 @@ discard block |
||
226 | 226 | $data = []; |
227 | 227 | |
228 | 228 | foreach ($this->data as $key => $value) { |
229 | - if(strstr($this->data[$key]['extra'],'auto_increment')){ |
|
229 | + if (strstr($this->data[$key]['extra'], 'auto_increment')) { |
|
230 | 230 | continue; |
231 | 231 | } |
232 | 232 | |
@@ -238,7 +238,7 @@ discard block |
||
238 | 238 | } |
239 | 239 | |
240 | 240 | $this->transaction('begin'); |
241 | - try{ |
|
241 | + try { |
|
242 | 242 | |
243 | 243 | $id = $this->insert($data); |
244 | 244 | |
@@ -248,7 +248,7 @@ discard block |
||
248 | 248 | |
249 | 249 | $this->transaction('commit'); |
250 | 250 | |
251 | - }catch(Exception $er){ |
|
251 | + } catch (Exception $er) { |
|
252 | 252 | $this->transaction('rollback'); |
253 | 253 | throw $er; |
254 | 254 | } |
@@ -258,13 +258,13 @@ discard block |
||
258 | 258 | |
259 | 259 | public function toEntity() |
260 | 260 | { |
261 | - if($this->getCount() === 0){ |
|
261 | + if ($this->getCount() === 0) { |
|
262 | 262 | return null; |
263 | 263 | } |
264 | 264 | |
265 | 265 | $entity = $this->setByDatabase($this->result[0]); |
266 | 266 | |
267 | - if(count($this->result) > 1){ |
|
267 | + if (count($this->result) > 1) { |
|
268 | 268 | $entity = []; |
269 | 269 | foreach ($this->result as $key => $value) { |
270 | 270 | $entity[] = $this->setByDatabase($value); |
@@ -276,12 +276,12 @@ discard block |
||
276 | 276 | |
277 | 277 | public function findById($id) |
278 | 278 | { |
279 | - return $this->where([$this->primary,'=',$id]); |
|
279 | + return $this->where([$this->primary, '=', $id]); |
|
280 | 280 | } |
281 | 281 | |
282 | 282 | public function execute() |
283 | 283 | { |
284 | - if(!is_null($this->clause) && $this->clause == 'remove'){ |
|
284 | + if (!is_null($this->clause) && $this->clause == 'remove') { |
|
285 | 285 | return $this->remove(true); |
286 | 286 | } |
287 | 287 | |
@@ -289,7 +289,7 @@ discard block |
||
289 | 289 | |
290 | 290 | $this->mountSelect(); |
291 | 291 | |
292 | - $where = substr($this->mountWhereExec()['where'],0,-1); |
|
292 | + $where = substr($this->mountWhereExec()['where'], 0, -1); |
|
293 | 293 | $this->query .= " WHERE {$where} "; |
294 | 294 | |
295 | 295 | $this->query .= $this->order; |
@@ -317,7 +317,7 @@ discard block |
||
317 | 317 | { |
318 | 318 | $this->transaction('begin'); |
319 | 319 | |
320 | - try{ |
|
320 | + try { |
|
321 | 321 | $this->update( |
322 | 322 | $this->mountSave()['data'], |
323 | 323 | "{$this->primary}=:{$this->primary}", |
@@ -327,7 +327,7 @@ discard block |
||
327 | 327 | $this->check_fail(); |
328 | 328 | |
329 | 329 | $this->transaction('commit'); |
330 | - }catch(Exception $er){ |
|
330 | + } catch (Exception $er) { |
|
331 | 331 | $this->transaction('rollback'); |
332 | 332 | throw $er; |
333 | 333 | } |
@@ -248,7 +248,7 @@ discard block |
||
248 | 248 | |
249 | 249 | $this->transaction('commit'); |
250 | 250 | |
251 | - }catch(Exception $er){ |
|
251 | + } catch(Exception $er){ |
|
252 | 252 | $this->transaction('rollback'); |
253 | 253 | throw $er; |
254 | 254 | } |
@@ -327,7 +327,7 @@ discard block |
||
327 | 327 | $this->check_fail(); |
328 | 328 | |
329 | 329 | $this->transaction('commit'); |
330 | - }catch(Exception $er){ |
|
330 | + } catch(Exception $er){ |
|
331 | 331 | $this->transaction('rollback'); |
332 | 332 | throw $er; |
333 | 333 | } |
@@ -2,7 +2,7 @@ discard block |
||
2 | 2 | |
3 | 3 | namespace HnrAzevedo\Datamanager; |
4 | 4 | |
5 | -trait SynchronizeTrait{ |
|
5 | +trait SynchronizeTrait { |
|
6 | 6 | use CrudTrait; |
7 | 7 | |
8 | 8 | protected ?string $table = null; |
@@ -43,16 +43,16 @@ discard block |
||
43 | 43 | $type = $value; |
44 | 44 | $maxlength = null; |
45 | 45 | |
46 | - if(strpos($value,'(')){ |
|
47 | - $type = (in_array( substr($value, 0, strpos($value,'(')) , ['varchar','char','text'])) ? 'string' : $type; |
|
48 | - $type = (in_array( substr($value, 0, strpos($value,'(')) , ['tinyint','mediumint','smallint','bigtint','int'])) ? 'int' : $type; |
|
49 | - $type = (in_array( substr($value, 0, strpos($value,'(')) , ['decimal','float','double','real'])) ? 'float' : $type; |
|
46 | + if (strpos($value, '(')) { |
|
47 | + $type = (in_array(substr($value, 0, strpos($value, '(')), ['varchar', 'char', 'text'])) ? 'string' : $type; |
|
48 | + $type = (in_array(substr($value, 0, strpos($value, '(')), ['tinyint', 'mediumint', 'smallint', 'bigtint', 'int'])) ? 'int' : $type; |
|
49 | + $type = (in_array(substr($value, 0, strpos($value, '(')), ['decimal', 'float', 'double', 'real'])) ? 'float' : $type; |
|
50 | 50 | } |
51 | 51 | |
52 | - $maxlength = (in_array( $type , ['string','float','int'])) ? substr($value,(strpos($value,'(')+1),-1) : $maxlength; |
|
53 | - $maxlength = (in_array( $type , ['date'])) ? 10 : $maxlength; |
|
54 | - $maxlength = (in_array( $type , ['datetime'])) ? 19 : $maxlength; |
|
55 | - $maxlength = (in_array( $type , ['boolean'])) ? 1 : $maxlength; |
|
52 | + $maxlength = (in_array($type, ['string', 'float', 'int'])) ? substr($value, (strpos($value, '(') + 1), -1) : $maxlength; |
|
53 | + $maxlength = (in_array($type, ['date'])) ? 10 : $maxlength; |
|
54 | + $maxlength = (in_array($type, ['datetime'])) ? 19 : $maxlength; |
|
55 | + $maxlength = (in_array($type, ['boolean'])) ? 1 : $maxlength; |
|
56 | 56 | |
57 | 57 | $this->$field = ['maxlength' => $maxlength]; |
58 | 58 | $this->$field = ['type' => $type]; |