@@ -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,20 +11,20 @@ |
||
| 11 | 11 | use HnrAzevedo\Datamanager\DatamanagerException; |
| 12 | 12 | use Model\User; |
| 13 | 13 | |
| 14 | -try{ |
|
| 14 | +try { |
|
| 15 | 15 | $entity = new User(); |
| 16 | 16 | |
| 17 | 17 | /* Set new info for insert in database */ |
| 18 | 18 | $entity->name = 'Henri Azevedo'; |
| 19 | 19 | $entity->email = '[email protected]'; |
| 20 | - $entity->password = password_hash('123456' ,PASSWORD_DEFAULT); |
|
| 20 | + $entity->password = password_hash('123456', PASSWORD_DEFAULT); |
|
| 21 | 21 | $entity->birth = '28/09/1996'; |
| 22 | 22 | $entity->register = date('Y-m-d H:i:s'); |
| 23 | 23 | |
| 24 | 24 | /* Insert entity in database */ |
| 25 | 25 | $entity->persist(); |
| 26 | 26 | |
| 27 | -}catch(DatamanagerException $er){ |
|
| 27 | +} catch (DatamanagerException $er) { |
|
| 28 | 28 | |
| 29 | 29 | die("Code Error: {$er->getCode()}, Line: {$er->getLine()}, File: {$er->getFile()}, Message: {$er->getMessage()}."); |
| 30 | 30 | |
@@ -4,16 +4,16 @@ discard block |
||
| 4 | 4 | |
| 5 | 5 | use HnrAzevedo\Datamanager\DatamanagerException; |
| 6 | 6 | |
| 7 | -trait EntityTrait{ |
|
| 7 | +trait EntityTrait { |
|
| 8 | 8 | public function toEntity() |
| 9 | 9 | { |
| 10 | - if($this->getCount() === 0){ |
|
| 10 | + if ($this->getCount() === 0) { |
|
| 11 | 11 | return null; |
| 12 | 12 | } |
| 13 | 13 | |
| 14 | 14 | $entity = $this->setByDatabase($this->result[0]); |
| 15 | 15 | |
| 16 | - if(count($this->result) > 1){ |
|
| 16 | + if (count($this->result) > 1) { |
|
| 17 | 17 | $entity = []; |
| 18 | 18 | foreach ($this->result as $key => $value) { |
| 19 | 19 | $entity[] = $this->setByDatabase($value); |
@@ -30,7 +30,7 @@ discard block |
||
| 30 | 30 | $data = []; |
| 31 | 31 | |
| 32 | 32 | foreach ($this->data as $key => $value) { |
| 33 | - if(strstr($this->data[$key]['extra'],'auto_increment')){ |
|
| 33 | + if (strstr($this->data[$key]['extra'], 'auto_increment')) { |
|
| 34 | 34 | continue; |
| 35 | 35 | } |
| 36 | 36 | |
@@ -42,7 +42,7 @@ discard block |
||
| 42 | 42 | } |
| 43 | 43 | |
| 44 | 44 | $this->transaction('begin'); |
| 45 | - try{ |
|
| 45 | + try { |
|
| 46 | 46 | |
| 47 | 47 | $id = $this->insert($data); |
| 48 | 48 | |
@@ -54,7 +54,7 @@ discard block |
||
| 54 | 54 | |
| 55 | 55 | $this->transaction('commit'); |
| 56 | 56 | |
| 57 | - }catch(DatamanagerException $er){ |
|
| 57 | + } catch (DatamanagerException $er) { |
|
| 58 | 58 | $this->transaction('rollback'); |
| 59 | 59 | throw $er; |
| 60 | 60 | } |
@@ -64,19 +64,19 @@ discard block |
||
| 64 | 64 | |
| 65 | 65 | public function remove(bool $exec = false) |
| 66 | 66 | { |
| 67 | - if(!$exec){ |
|
| 67 | + if (!$exec) { |
|
| 68 | 68 | $this->clause = 'remove'; |
| 69 | 69 | return $this; |
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | $this->clause = null; |
| 73 | 73 | |
| 74 | - if(count($this->where) == 1){ |
|
| 74 | + if (count($this->where) == 1) { |
|
| 75 | 75 | $this->removeById(); |
| 76 | 76 | return $this; |
| 77 | 77 | } |
| 78 | 78 | |
| 79 | - $this->delete($this->mountRemove()['where'], substr( $this->mountRemove()['data'] ,0,-1) ); |
|
| 79 | + $this->delete($this->mountRemove()['where'], substr($this->mountRemove()['data'], 0, -1)); |
|
| 80 | 80 | |
| 81 | 81 | $this->check_fail(); |
| 82 | 82 | |
@@ -87,7 +87,7 @@ discard block |
||
| 87 | 87 | { |
| 88 | 88 | $this->transaction('begin'); |
| 89 | 89 | |
| 90 | - try{ |
|
| 90 | + try { |
|
| 91 | 91 | $this->update( |
| 92 | 92 | $this->mountSave()['data'], |
| 93 | 93 | "{$this->primary}=:{$this->primary}", |
@@ -97,7 +97,7 @@ discard block |
||
| 97 | 97 | $this->check_fail(); |
| 98 | 98 | |
| 99 | 99 | $this->transaction('commit'); |
| 100 | - }catch(DatamanagerException $er){ |
|
| 100 | + } catch (DatamanagerException $er) { |
|
| 101 | 101 | $this->transaction('rollback'); |
| 102 | 102 | throw $er; |
| 103 | 103 | } |