@@ -11,21 +11,21 @@ discard block |
||
11 | 11 | protected ?string $table = null; |
12 | 12 | protected ?string $primary = null; |
13 | 13 | protected array $data = []; |
14 | - protected array $where = [''=> ["1",'=',"1"] ]; |
|
14 | + protected array $where = [''=> ["1", '=', "1"]]; |
|
15 | 15 | protected array $between = []; |
16 | 16 | |
17 | - public function __set(string $prop,$value) |
|
17 | + public function __set(string $prop, $value) |
|
18 | 18 | { |
19 | - if(is_array($value)){ |
|
19 | + if (is_array($value)) { |
|
20 | 20 | $attr = array_keys($value)[0]; |
21 | 21 | $this->data[$prop][$attr] = $value[$attr]; |
22 | 22 | return $this; |
23 | 23 | } |
24 | 24 | |
25 | - if($this->full){ |
|
26 | - switch($this->data[$prop]['type']){ |
|
25 | + if ($this->full) { |
|
26 | + switch ($this->data[$prop]['type']) { |
|
27 | 27 | case 'date': |
28 | - $value = (date_format( date_create_from_format(DATAMANAGER_CONFIG['dateformat'],$value) , 'Y-m-d')); |
|
28 | + $value = (date_format(date_create_from_format(DATAMANAGER_CONFIG['dateformat'], $value), 'Y-m-d')); |
|
29 | 29 | break; |
30 | 30 | } |
31 | 31 | } |
@@ -47,9 +47,9 @@ discard block |
||
47 | 47 | { |
48 | 48 | $this->isSettable($field); |
49 | 49 | |
50 | - if($this->full){ |
|
51 | - switch($this->data[$field]['type']){ |
|
52 | - case 'date': $this->data[$field]['value'] = (date_format( date_create_from_format('Y-m-d' , $this->data[$field]['value'] ) , DATAMANAGER_CONFIG['dateformat'])); |
|
50 | + if ($this->full) { |
|
51 | + switch ($this->data[$field]['type']) { |
|
52 | + case 'date': $this->data[$field]['value'] = (date_format(date_create_from_format('Y-m-d', $this->data[$field]['value']), DATAMANAGER_CONFIG['dateformat'])); |
|
53 | 53 | } |
54 | 54 | } |
55 | 55 | |
@@ -66,7 +66,7 @@ discard block |
||
66 | 66 | $deniable = (is_array($deniable)) ? $deniable : [$deniable]; |
67 | 67 | |
68 | 68 | foreach ($deniable as $field) { |
69 | - if(!array_key_exists($field,$this->data)){ |
|
69 | + if (!array_key_exists($field, $this->data)) { |
|
70 | 70 | throw new DatamanagerException("{$field} field does not exist in the table {$this->table}."); |
71 | 71 | } |
72 | 72 | |
@@ -86,9 +86,9 @@ discard block |
||
86 | 86 | |
87 | 87 | public function orderBy(string $field, string $ord = 'ASC') |
88 | 88 | { |
89 | - $this->isSettable( str_replace(['asc','ASC','desc','DESC',' '],'',$field) ); |
|
89 | + $this->isSettable(str_replace(['asc', 'ASC', 'desc', 'DESC', ' '], '', $field)); |
|
90 | 90 | |
91 | - $ord = (strpos(strtolower($field),'asc') || strpos(strtolower($field),'desc')) ? '' : $ord; |
|
91 | + $ord = (strpos(strtolower($field), 'asc') || strpos(strtolower($field), 'desc')) ? '' : $ord; |
|
92 | 92 | |
93 | 93 | $this->order = " ORDER BY {$field} {$ord} "; |
94 | 94 | return $this; |
@@ -112,11 +112,11 @@ discard block |
||
112 | 112 | |
113 | 113 | public function where(array $where) |
114 | 114 | { |
115 | - $this->where['AND'] = (array_key_exists('AND',$this->where)) ?? ''; |
|
115 | + $this->where['AND'] = (array_key_exists('AND', $this->where)) ?? ''; |
|
116 | 116 | $w = []; |
117 | 117 | foreach ($where as $condition => $values) { |
118 | 118 | |
119 | - if(!is_array($values)){ |
|
119 | + if (!is_array($values)) { |
|
120 | 120 | $w['AND'][] = $values; |
121 | 121 | continue; |
122 | 122 | } |
@@ -127,7 +127,7 @@ discard block |
||
127 | 127 | |
128 | 128 | } |
129 | 129 | |
130 | - $this->where = array_merge($this->where,$w); |
|
130 | + $this->where = array_merge($this->where, $w); |
|
131 | 131 | |
132 | 132 | return $this; |
133 | 133 | } |
@@ -187,7 +187,7 @@ discard block |
||
187 | 187 | $string = ''; |
188 | 188 | foreach ($this->data as $key => $value) { |
189 | 189 | |
190 | - if(gettype($value)==='object'){ |
|
190 | + if (gettype($value) === 'object') { |
|
191 | 191 | $value = $value->getData()[$this->primary]['value']; |
192 | 192 | } |
193 | 193 | |
@@ -198,7 +198,7 @@ discard block |
||
198 | 198 | |
199 | 199 | private function removeById(): bool |
200 | 200 | { |
201 | - $delete = $this->delete("{$this->primary}=:{$this->primary}","{$this->primary}={$this->getData()[$this->primary]['value']}"); |
|
201 | + $delete = $this->delete("{$this->primary}=:{$this->primary}", "{$this->primary}={$this->getData()[$this->primary]['value']}"); |
|
202 | 202 | |
203 | 203 | $this->check_fail(); |
204 | 204 | |
@@ -207,12 +207,12 @@ discard block |
||
207 | 207 | |
208 | 208 | public function findById($id) |
209 | 209 | { |
210 | - return $this->where([$this->primary,'=',$id]); |
|
210 | + return $this->where([$this->primary, '=', $id]); |
|
211 | 211 | } |
212 | 212 | |
213 | 213 | public function execute() |
214 | 214 | { |
215 | - if(!is_null($this->clause) && $this->clause == 'remove'){ |
|
215 | + if (!is_null($this->clause) && $this->clause == 'remove') { |
|
216 | 216 | return $this->remove(true); |
217 | 217 | } |
218 | 218 | |
@@ -220,8 +220,8 @@ discard block |
||
220 | 220 | |
221 | 221 | $this->mountSelect(); |
222 | 222 | |
223 | - $where = substr($this->mountWhereExec()['where'],0,-1); |
|
224 | - $where .= substr($this->mountBetweenExec()['where'],0,-1); |
|
223 | + $where = substr($this->mountWhereExec()['where'], 0, -1); |
|
224 | + $where .= substr($this->mountBetweenExec()['where'], 0, -1); |
|
225 | 225 | |
226 | 226 | $this->query .= " WHERE {$where} "; |
227 | 227 |
@@ -2,7 +2,7 @@ discard block |
||
2 | 2 | |
3 | 3 | namespace HnrAzevedo\Datamanager; |
4 | 4 | |
5 | -trait DataTrait{ |
|
5 | +trait DataTrait { |
|
6 | 6 | use CrudTrait, CheckTrait; |
7 | 7 | |
8 | 8 | protected ?string $table = null; |
@@ -24,14 +24,14 @@ discard block |
||
24 | 24 | protected function mountRemove(): array |
25 | 25 | { |
26 | 26 | $return = ['data' => '', 'where' => '']; |
27 | - foreach($this->where as $clause => $condition){ |
|
28 | - if(strlen($clause) === 0){ |
|
27 | + foreach ($this->where as $clause => $condition) { |
|
28 | + if (strlen($clause) === 0) { |
|
29 | 29 | $return['where'] .= " {$clause} {$condition[0]} {$condition[1]} :q_{$condition[0]} "; |
30 | 30 | $return['data'] .= "q_{$condition[0]}={$condition[2]}&"; |
31 | 31 | continue; |
32 | 32 | } |
33 | 33 | |
34 | - foreach($condition as $value){ |
|
34 | + foreach ($condition as $value) { |
|
35 | 35 | $return['where'] .= " {$clause} {$value[0]} {$value[1]} :q_{$value[0]} "; |
36 | 36 | $return['data'] .= "q_{$value[0]}={$value[2]}&"; |
37 | 37 | } |
@@ -44,7 +44,7 @@ discard block |
||
44 | 44 | $return = ['data' => []]; |
45 | 45 | |
46 | 46 | foreach ($this->data as $key => $value) { |
47 | - if($this->upgradeable($key) && !$this->isIncremented($key)){ |
|
47 | + if ($this->upgradeable($key) && !$this->isIncremented($key)) { |
|
48 | 48 | $return['data'][$key] = $this->data[$key]['value']; |
49 | 49 | } |
50 | 50 | } |
@@ -60,7 +60,7 @@ discard block |
||
60 | 60 | |
61 | 61 | $key = (!$key) ? '' : " {$key} "; |
62 | 62 | |
63 | - if(is_array($value[0])){ |
|
63 | + if (is_array($value[0])) { |
|
64 | 64 | |
65 | 65 | foreach ($value as $k => $v) { |
66 | 66 | $return['where'] .= " {$key} {$v[0]} {$v[1]} :q_{$v[0]} "; |
@@ -81,13 +81,13 @@ discard block |
||
81 | 81 | { |
82 | 82 | $return = ['where' => '', 'data' => []]; |
83 | 83 | |
84 | - foreach($this->between as $field => $value){ |
|
85 | - $condition = (count(explode(' ',$field)) > 2) ? ' '.explode(' ',$field)[0].' ' : ' AND '; |
|
86 | - $field = str_replace(['AND','OR',' '],'',$field); |
|
84 | + foreach ($this->between as $field => $value) { |
|
85 | + $condition = (count(explode(' ', $field)) > 2) ? ' '.explode(' ', $field)[0].' ' : ' AND '; |
|
86 | + $field = str_replace(['AND', 'OR', ' '], '', $field); |
|
87 | 87 | $return['where'] .= " {$condition} {$field} BETWEEN :q_1{$field} AND :q_2{$field} "; |
88 | 88 | $return['data'] = [ |
89 | - "q_1{$field}" => (date_format( date_create_from_format(DATAMANAGER_CONFIG['dateformat'],$value[0]) , 'Y-m-d')), |
|
90 | - "q_2{$field}" => (date_format( date_create_from_format(DATAMANAGER_CONFIG['dateformat'],$value[1]) , 'Y-m-d')) |
|
89 | + "q_1{$field}" => (date_format(date_create_from_format(DATAMANAGER_CONFIG['dateformat'], $value[0]), 'Y-m-d')), |
|
90 | + "q_2{$field}" => (date_format(date_create_from_format(DATAMANAGER_CONFIG['dateformat'], $value[1]), 'Y-m-d')) |
|
91 | 91 | ]; |
92 | 92 | } |
93 | 93 | return $return; |
@@ -95,21 +95,21 @@ discard block |
||
95 | 95 | |
96 | 96 | protected function mountSelect() |
97 | 97 | { |
98 | - $select = implode(',',array_keys($this->select)); |
|
98 | + $select = implode(',', array_keys($this->select)); |
|
99 | 99 | |
100 | - $this->query = str_replace('*', $select,$this->query); |
|
100 | + $this->query = str_replace('*', $select, $this->query); |
|
101 | 101 | } |
102 | 102 | |
103 | 103 | protected function mountLimit() |
104 | 104 | { |
105 | - if(!is_null($this->limit)){ |
|
105 | + if (!is_null($this->limit)) { |
|
106 | 106 | $this->query .= " LIMIT {$this->limit}"; |
107 | 107 | } |
108 | 108 | } |
109 | 109 | |
110 | 110 | protected function mountOffset() |
111 | 111 | { |
112 | - if(!is_null($this->offset)){ |
|
112 | + if (!is_null($this->offset)) { |
|
113 | 113 | $this->query .= " OFFSET {$this->offset}"; |
114 | 114 | } |
115 | 115 | } |
@@ -11,7 +11,7 @@ discard block |
||
11 | 11 | |
12 | 12 | $entity = new User(); |
13 | 13 | |
14 | -try{ |
|
14 | +try { |
|
15 | 15 | /* Set new info for insert in database */ |
16 | 16 | $entity->name = 'Henri Azevedo'; |
17 | 17 | $entity->email = '[email protected]'; |
@@ -26,14 +26,14 @@ discard block |
||
26 | 26 | $user = $entity->find()->execute()->first()->toEntity(); |
27 | 27 | |
28 | 28 | /* Search only for columns defined in advance */ |
29 | - $user = $entity->find()->only(['name','email'])->execute()->first(); |
|
29 | + $user = $entity->find()->only(['name', 'email'])->execute()->first(); |
|
30 | 30 | $name = $user->name; |
31 | 31 | $email = $user->email; |
32 | 32 | |
33 | 33 | /* OR */ |
34 | 34 | $name = $entity->find()->only('name')->execute()->first()->name; |
35 | 35 | |
36 | - $user = $entity->find()->between(['AND birth'=> ['01/01/1996','31/12/1996']])->execute()->first(); |
|
36 | + $user = $entity->find()->between(['AND birth'=> ['01/01/1996', '31/12/1996']])->execute()->first(); |
|
37 | 37 | |
38 | 38 | /* Change info to update */ |
39 | 39 | $user->name = 'Other Name'; |
@@ -48,10 +48,10 @@ discard block |
||
48 | 48 | |
49 | 49 | /* Remove by cause *Where* */ |
50 | 50 | $user->remove()->where([ |
51 | - ['name','=','Other Name'], |
|
52 | - 'OR' => ['email','LIKE','[email protected]'] |
|
51 | + ['name', '=', 'Other Name'], |
|
52 | + 'OR' => ['email', 'LIKE', '[email protected]'] |
|
53 | 53 | ])->execute(); |
54 | -}catch(DatamanagerException $er){ |
|
54 | +} catch (DatamanagerException $er) { |
|
55 | 55 | |
56 | 56 | //var_dump($er); |
57 | 57 | die("Code Error: {$er->getCode()}, Line: {$er->getLine()}, File: {$er->getFile()}, Message: {$er->getMessage()}."); |