@@ -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; |
@@ -25,15 +25,15 @@ discard block |
||
| 25 | 25 | { |
| 26 | 26 | $return = ['data' => '', 'where' => '']; |
| 27 | 27 | $c = -1; |
| 28 | - foreach($this->where as $clause => $condition){ |
|
| 28 | + foreach ($this->where as $clause => $condition) { |
|
| 29 | 29 | $c++; |
| 30 | - if(strlen($clause) === 0){ |
|
| 30 | + if (strlen($clause) === 0) { |
|
| 31 | 31 | $return['where'] .= " {$clause} {$condition[0]} {$condition[1]} :q_{$condition[0]}{$c} "; |
| 32 | 32 | $return['data'] .= "q_{$condition[0]}={$condition[2]}{$c}&"; |
| 33 | 33 | continue; |
| 34 | 34 | } |
| 35 | 35 | |
| 36 | - foreach($condition as $value){ |
|
| 36 | + foreach ($condition as $value) { |
|
| 37 | 37 | $return['where'] .= " {$clause} {$value[0]} {$value[1]} :q_{$value[0]}{$c} "; |
| 38 | 38 | $return['data'] .= "q_{$value[0]}={$value[2]}{$c}&"; |
| 39 | 39 | } |
@@ -46,7 +46,7 @@ discard block |
||
| 46 | 46 | $return = ['data' => []]; |
| 47 | 47 | |
| 48 | 48 | foreach ($this->data as $key => $value) { |
| 49 | - if($this->upgradeable($key) && !$this->isIncremented($key)){ |
|
| 49 | + if ($this->upgradeable($key) && !$this->isIncremented($key)) { |
|
| 50 | 50 | $return['data'][$key] = $this->data[$key]['value']; |
| 51 | 51 | } |
| 52 | 52 | } |
@@ -57,9 +57,9 @@ discard block |
||
| 57 | 57 | private function mountWheres(array $value, string $key, int $c): array |
| 58 | 58 | { |
| 59 | 59 | $return = ['where' => '', 'data' => []]; |
| 60 | - for($i = 0; $i < count($value); $i++){ |
|
| 60 | + for ($i = 0; $i < count($value); $i++) { |
|
| 61 | 61 | |
| 62 | - if(!is_array($value[$i][2])){ |
|
| 62 | + if (!is_array($value[$i][2])) { |
|
| 63 | 63 | $return['where'] .= " {$key} {$value[$i][0]} {$value[$i][1]} :q_{$value[$i][0]}{$c}{$i} "; |
| 64 | 64 | $return['data']["q_{$value[$i][0]}{$c}{$i}"] = $value[$i][2]; |
| 65 | 65 | continue; |
@@ -67,12 +67,12 @@ discard block |
||
| 67 | 67 | |
| 68 | 68 | $return['where'] .= " {$key} {$value[$i][0]} {$value[$i][1]} ("; |
| 69 | 69 | |
| 70 | - foreach($value[$i][2] as $v => $valu){ |
|
| 70 | + foreach ($value[$i][2] as $v => $valu) { |
|
| 71 | 71 | $return['where'] .= " :q_{$value[$i][0]}{$c}{$i}_{$v},"; |
| 72 | 72 | $return['data']["q_{$value[$i][0]}{$c}{$i}_{$v}"] = $valu; |
| 73 | 73 | } |
| 74 | 74 | |
| 75 | - $return['where'] = substr($return['where'],0,-1) .') '; |
|
| 75 | + $return['where'] = substr($return['where'], 0, -1).') '; |
|
| 76 | 76 | } |
| 77 | 77 | return $return; |
| 78 | 78 | } |
@@ -85,7 +85,7 @@ discard block |
||
| 85 | 85 | $c++; |
| 86 | 86 | $key = (!$key) ? '' : " {$key} "; |
| 87 | 87 | |
| 88 | - if(!is_array($value[0])){ |
|
| 88 | + if (!is_array($value[0])) { |
|
| 89 | 89 | $return['where'] .= " {$key} {$value[0]} {$value[1]} :q_{$value[0]}{$c} "; |
| 90 | 90 | $return['data']["q_{$value[0]}{$c}"] = $value[2]; |
| 91 | 91 | continue; |
@@ -93,7 +93,7 @@ discard block |
||
| 93 | 93 | |
| 94 | 94 | $altReturn = $this->mountWheres($value, $key, $c); |
| 95 | 95 | $return['where'] .= $altReturn['where']; |
| 96 | - $return['data'] = array_merge($return['data'],$altReturn['data']); |
|
| 96 | + $return['data'] = array_merge($return['data'], $altReturn['data']); |
|
| 97 | 97 | |
| 98 | 98 | } |
| 99 | 99 | return $return; |
@@ -104,14 +104,14 @@ discard block |
||
| 104 | 104 | $return = ['where' => '', 'data' => []]; |
| 105 | 105 | |
| 106 | 106 | $c = -1; |
| 107 | - foreach($this->between as $field => $value){ |
|
| 107 | + foreach ($this->between as $field => $value) { |
|
| 108 | 108 | $c++; |
| 109 | - $condition = (count(explode(' ',$field)) > 2) ? ' '.explode(' ',$field)[0].' ' : ' AND '; |
|
| 110 | - $field = str_replace(['AND','OR',' '],'',$field); |
|
| 109 | + $condition = (count(explode(' ', $field)) > 2) ? ' '.explode(' ', $field)[0].' ' : ' AND '; |
|
| 110 | + $field = str_replace(['AND', 'OR', ' '], '', $field); |
|
| 111 | 111 | $return['where'] .= " {$condition} {$field} BETWEEN :q_1{$field}{$c} AND :q_2{$field}{$c} "; |
| 112 | 112 | $return['data'] = [ |
| 113 | - "q_1{$field}{$c}" => (date_format( date_create_from_format(DATAMANAGER_CONFIG['dateformat'],$value[0]) , 'Y-m-d')), |
|
| 114 | - "q_2{$field}{$c}" => (date_format( date_create_from_format(DATAMANAGER_CONFIG['dateformat'],$value[1]) , 'Y-m-d')) |
|
| 113 | + "q_1{$field}{$c}" => (date_format(date_create_from_format(DATAMANAGER_CONFIG['dateformat'], $value[0]), 'Y-m-d')), |
|
| 114 | + "q_2{$field}{$c}" => (date_format(date_create_from_format(DATAMANAGER_CONFIG['dateformat'], $value[1]), 'Y-m-d')) |
|
| 115 | 115 | ]; |
| 116 | 116 | } |
| 117 | 117 | return $return; |
@@ -119,21 +119,21 @@ discard block |
||
| 119 | 119 | |
| 120 | 120 | protected function mountSelect() |
| 121 | 121 | { |
| 122 | - $select = implode(',',array_keys($this->select)); |
|
| 122 | + $select = implode(',', array_keys($this->select)); |
|
| 123 | 123 | |
| 124 | - $this->query = str_replace('*', $select,$this->query); |
|
| 124 | + $this->query = str_replace('*', $select, $this->query); |
|
| 125 | 125 | } |
| 126 | 126 | |
| 127 | 127 | protected function mountLimit() |
| 128 | 128 | { |
| 129 | - if(!is_null($this->limit)){ |
|
| 129 | + if (!is_null($this->limit)) { |
|
| 130 | 130 | $this->query .= " LIMIT {$this->limit}"; |
| 131 | 131 | } |
| 132 | 132 | } |
| 133 | 133 | |
| 134 | 134 | protected function mountOffset() |
| 135 | 135 | { |
| 136 | - if(!is_null($this->offset)){ |
|
| 136 | + if (!is_null($this->offset)) { |
|
| 137 | 137 | $this->query .= " OFFSET {$this->offset}"; |
| 138 | 138 | } |
| 139 | 139 | } |
@@ -2,20 +2,20 @@ discard block |
||
| 2 | 2 | |
| 3 | 3 | namespace HnrAzevedo\Datamanager; |
| 4 | 4 | |
| 5 | -trait MagicsTrait{ |
|
| 5 | +trait MagicsTrait { |
|
| 6 | 6 | |
| 7 | - public function __set(string $prop,$value) |
|
| 7 | + public function __set(string $prop, $value) |
|
| 8 | 8 | { |
| 9 | - if(is_array($value)){ |
|
| 9 | + if (is_array($value)) { |
|
| 10 | 10 | $attr = array_keys($value)[0]; |
| 11 | 11 | $this->data[$prop][$attr] = $value[$attr]; |
| 12 | 12 | return $this; |
| 13 | 13 | } |
| 14 | 14 | |
| 15 | - if($this->full){ |
|
| 16 | - switch($this->data[$prop]['type']){ |
|
| 15 | + if ($this->full) { |
|
| 16 | + switch ($this->data[$prop]['type']) { |
|
| 17 | 17 | case 'date': |
| 18 | - $value = (date_format( date_create_from_format(DATAMANAGER_CONFIG['dateformat'],$value) , 'Y-m-d')); |
|
| 18 | + $value = (date_format(date_create_from_format(DATAMANAGER_CONFIG['dateformat'], $value), 'Y-m-d')); |
|
| 19 | 19 | break; |
| 20 | 20 | } |
| 21 | 21 | } |
@@ -32,13 +32,13 @@ discard block |
||
| 32 | 32 | { |
| 33 | 33 | $this->isSettable($field); |
| 34 | 34 | |
| 35 | - if($this->full){ |
|
| 36 | - switch($this->data[$field]['type']){ |
|
| 35 | + if ($this->full) { |
|
| 36 | + switch ($this->data[$field]['type']) { |
|
| 37 | 37 | case 'date': |
| 38 | - return (!empty($this->data[$field]['value'])) ? (@date_format( @date_create_from_format('Y-m-d' , $this->data[$field]['value'] ) , DATAMANAGER_CONFIG['dateformat'])) : null ; |
|
| 38 | + return (!empty($this->data[$field]['value'])) ? (@date_format(@date_create_from_format('Y-m-d', $this->data[$field]['value']), DATAMANAGER_CONFIG['dateformat'])) : null; |
|
| 39 | 39 | break; |
| 40 | 40 | case 'datetime': |
| 41 | - return (!empty($this->data[$field]['value'])) ? (@date_format( @date_create_from_format('Y-m-d H:i:s' , $this->data[$field]['value'] ) , DATAMANAGER_CONFIG['datetimeformat'])) : null ; |
|
| 41 | + return (!empty($this->data[$field]['value'])) ? (@date_format(@date_create_from_format('Y-m-d H:i:s', $this->data[$field]['value']), DATAMANAGER_CONFIG['datetimeformat'])) : null; |
|
| 42 | 42 | break; |
| 43 | 43 | } |
| 44 | 44 | } |
@@ -11,7 +11,7 @@ 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 | 17 | public function getData(): ?array |
@@ -29,7 +29,7 @@ discard block |
||
| 29 | 29 | $deniable = (is_array($deniable)) ? $deniable : [$deniable]; |
| 30 | 30 | |
| 31 | 31 | foreach ($deniable as $field) { |
| 32 | - if(!array_key_exists($field,$this->data)){ |
|
| 32 | + if (!array_key_exists($field, $this->data)) { |
|
| 33 | 33 | throw new DatamanagerException("{$field} field does not exist in the table {$this->table}."); |
| 34 | 34 | } |
| 35 | 35 | $this->excepts[$field] = true; |
@@ -48,9 +48,9 @@ discard block |
||
| 48 | 48 | |
| 49 | 49 | public function orderBy(string $field, string $ord = 'ASC') |
| 50 | 50 | { |
| 51 | - $this->isSettable( str_replace(['asc','ASC','desc','DESC',' '],'',$field) ); |
|
| 51 | + $this->isSettable(str_replace(['asc', 'ASC', 'desc', 'DESC', ' '], '', $field)); |
|
| 52 | 52 | |
| 53 | - $ord = (strpos(strtolower($field),'asc') || strpos(strtolower($field),'desc')) ? '' : $ord; |
|
| 53 | + $ord = (strpos(strtolower($field), 'asc') || strpos(strtolower($field), 'desc')) ? '' : $ord; |
|
| 54 | 54 | |
| 55 | 55 | $this->order = " ORDER BY {$field} {$ord} "; |
| 56 | 56 | return $this; |
@@ -66,7 +66,7 @@ discard block |
||
| 66 | 66 | $this->select[$field] = true; |
| 67 | 67 | } |
| 68 | 68 | |
| 69 | - if(!is_null($this->primary)){ |
|
| 69 | + if (!is_null($this->primary)) { |
|
| 70 | 70 | $this->select[$this->primary] = true; |
| 71 | 71 | } |
| 72 | 72 | return $this; |
@@ -74,11 +74,11 @@ discard block |
||
| 74 | 74 | |
| 75 | 75 | public function where(array $where) |
| 76 | 76 | { |
| 77 | - $this->where['AND'] = (array_key_exists('AND',$this->where)) ?? ''; |
|
| 77 | + $this->where['AND'] = (array_key_exists('AND', $this->where)) ?? ''; |
|
| 78 | 78 | $w = []; |
| 79 | 79 | foreach ($where as $condition => $values) { |
| 80 | 80 | |
| 81 | - if(!is_array($values)){ |
|
| 81 | + if (!is_array($values)) { |
|
| 82 | 82 | $w['AND'][] = $values; |
| 83 | 83 | continue; |
| 84 | 84 | } |
@@ -89,7 +89,7 @@ discard block |
||
| 89 | 89 | |
| 90 | 90 | } |
| 91 | 91 | |
| 92 | - $this->where = array_merge($this->where,$w); |
|
| 92 | + $this->where = array_merge($this->where, $w); |
|
| 93 | 93 | |
| 94 | 94 | return $this; |
| 95 | 95 | } |
@@ -149,7 +149,7 @@ discard block |
||
| 149 | 149 | $string = ''; |
| 150 | 150 | foreach ($this->data as $key => $value) { |
| 151 | 151 | |
| 152 | - if(gettype($value)==='object'){ |
|
| 152 | + if (gettype($value) === 'object') { |
|
| 153 | 153 | $value = $value->getData()[$this->primary]['value']; |
| 154 | 154 | } |
| 155 | 155 | |
@@ -160,7 +160,7 @@ discard block |
||
| 160 | 160 | |
| 161 | 161 | private function removeById(): bool |
| 162 | 162 | { |
| 163 | - $delete = $this->delete("{$this->primary}=:{$this->primary}","{$this->primary}={$this->getData()[$this->primary]['value']}"); |
|
| 163 | + $delete = $this->delete("{$this->primary}=:{$this->primary}", "{$this->primary}={$this->getData()[$this->primary]['value']}"); |
|
| 164 | 164 | |
| 165 | 165 | $this->check_fail(); |
| 166 | 166 | |
@@ -169,12 +169,12 @@ discard block |
||
| 169 | 169 | |
| 170 | 170 | public function findById($id) |
| 171 | 171 | { |
| 172 | - return $this->where([$this->primary,'=',$id]); |
|
| 172 | + return $this->where([$this->primary, '=', $id]); |
|
| 173 | 173 | } |
| 174 | 174 | |
| 175 | 175 | public function execute() |
| 176 | 176 | { |
| 177 | - if(!is_null($this->clause) && $this->clause == 'remove'){ |
|
| 177 | + if (!is_null($this->clause) && $this->clause == 'remove') { |
|
| 178 | 178 | return $this->remove(true); |
| 179 | 179 | } |
| 180 | 180 | |
@@ -182,8 +182,8 @@ discard block |
||
| 182 | 182 | |
| 183 | 183 | $this->mountSelect(); |
| 184 | 184 | |
| 185 | - $where = substr($this->mountWhereExec()['where'],0,-1); |
|
| 186 | - $where .= substr($this->mountBetweenExec()['where'],0,-1); |
|
| 185 | + $where = substr($this->mountWhereExec()['where'], 0, -1); |
|
| 186 | + $where .= substr($this->mountBetweenExec()['where'], 0, -1); |
|
| 187 | 187 | |
| 188 | 188 | $this->query .= " WHERE {$where} "; |
| 189 | 189 | |
@@ -201,7 +201,7 @@ discard block |
||
| 201 | 201 | |
| 202 | 202 | $this->count = count($this->result); |
| 203 | 203 | $this->query = null; |
| 204 | - $this->where = [''=> ["1",'=',"1"] ]; |
|
| 204 | + $this->where = [''=> ["1", '=', "1"]]; |
|
| 205 | 205 | |
| 206 | 206 | return $this; |
| 207 | 207 | } |
@@ -4,36 +4,36 @@ discard block |
||
| 4 | 4 | |
| 5 | 5 | use HnrAzevedo\Datamanager\DatamanagerException; |
| 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 DatamanagerException("Condition where set incorrectly: ".implode(' ',$where)); |
|
| 11 | + if (count($where) != 3) { |
|
| 12 | + throw new DatamanagerException("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 DatamanagerException("{$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 DatamanagerException("{$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 DatamanagerException("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 DatamanagerException("The information provided for column {$field} of table {$this->table} exceeded that allowed."); |
| 38 | 38 | } |
| 39 | 39 | } |
@@ -45,18 +45,18 @@ discard block |
||
| 45 | 45 | |
| 46 | 46 | protected function isIncremented(string $field): bool |
| 47 | 47 | { |
| 48 | - return ( strstr($this->data[$field]['extra'],'auto_increment') && $field === $this->primary ); |
|
| 48 | + return (strstr($this->data[$field]['extra'], 'auto_increment') && $field === $this->primary); |
|
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | protected function checkForChanges(): bool |
| 52 | 52 | { |
| 53 | 53 | $hasChanges = false; |
| 54 | - foreach($this->data as $data){ |
|
| 55 | - if($data['changed']){ |
|
| 54 | + foreach ($this->data as $data) { |
|
| 55 | + if ($data['changed']) { |
|
| 56 | 56 | $hasChanges = true; |
| 57 | 57 | } |
| 58 | 58 | } |
| 59 | - if(!$hasChanges){ |
|
| 59 | + if (!$hasChanges) { |
|
| 60 | 60 | throw new DatamanagerException('There were no changes to be saved in the database.'); |
| 61 | 61 | } |
| 62 | 62 | return true; |
@@ -64,10 +64,10 @@ discard block |
||
| 64 | 64 | |
| 65 | 65 | protected function checkUniques($data) |
| 66 | 66 | { |
| 67 | - foreach($this->data as $d => $dd){ |
|
| 68 | - if($dd['key'] === 'UNI'){ |
|
| 69 | - $exist = $this->find()->where([$d,'=',$data[$d]])->only('id')->execute()->getCount(); |
|
| 70 | - if($exist > 0){ |
|
| 67 | + foreach ($this->data as $d => $dd) { |
|
| 68 | + if ($dd['key'] === 'UNI') { |
|
| 69 | + $exist = $this->find()->where([$d, '=', $data[$d]])->only('id')->execute()->getCount(); |
|
| 70 | + if ($exist > 0) { |
|
| 71 | 71 | throw new DatamanagerException("A record with the same {$this->getField($d)} already exists."); |
| 72 | 72 | } |
| 73 | 73 | } |