Passed
Push — master ( 7544db...24169a )
by Henri
01:19
created

DataTrait   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Importance

Changes 7
Bugs 0 Features 2
Metric Value
eloc 72
c 7
b 0
f 2
dl 0
loc 131
rs 10
wmc 24

8 Methods

Rating   Name   Duplication   Size   Complexity  
A mountRemove() 0 18 4
A mountSave() 0 11 4
A mountOffset() 0 4 2
A mountLimit() 0 4 2
A mountWhereExec() 0 18 4
A mountSelect() 0 5 1
A mountWheres() 0 21 4
A mountBetweenExec() 0 16 3
1
<?php
2
3
namespace HnrAzevedo\Datamanager;
4
5
trait DataTrait{
6
    use CrudTrait, CheckTrait;
7
8
    protected ?string $table = null;
9
    protected ?string $primary = null;
10
    protected array $data = [];
11
    protected bool $full = false;
12
13
    protected array $result = [];
14
    protected ?string $clause = null;
15
16
    protected ?string $order = null;
17
    protected ?string $limit = null;
18
    protected ?int $offset = null;
19
    protected array $excepts = [];
20
    protected int $count = 0;
21
    protected array $select = [];
22
    protected ?string $query = null;
23
24
    protected function mountRemove(): array
25
    {
26
        $return = ['data' => '', 'where' => ''];
27
        $c = -1;
28
        foreach($this->where as $clause => $condition){
29
            $c++;
30
            if(strlen($clause) === 0){
31
                $return['where'] .= " {$clause} {$condition[0]} {$condition[1]} :q_{$condition[0]}{$c} ";
32
                $return['data'] .= "q_{$condition[0]}={$condition[2]}{$c}&";
33
                continue;
34
            }
35
                
36
            foreach($condition as $value){
37
                $return['where'] .= " {$clause} {$value[0]} {$value[1]} :q_{$value[0]}{$c} ";
38
                $return['data'] .= "q_{$value[0]}={$value[2]}{$c}&";
39
            }
40
        }
41
        return $return;
42
    }   
43
44
    protected function mountSave(): array
45
    {
46
        $return = ['data' => []];
47
48
        foreach ($this->data as $key => $value) {
49
            if($this->upgradeable($key) && !$this->isIncremented($key)){
50
                $return['data'][$key] = $this->data[$key]['value'];
51
            }
52
        }
53
54
        return $return;
55
    }
56
57
    private function mountWheres(array $value, string $key): array
58
    {
59
        $return = [];
60
        for($i = 0; $i < count($value); $i++){
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
61
                
62
            if(!is_array($value[$i][2])){
63
                $return['where'] .= " {$key} {$value[$i][0]} {$value[$i][1]} :q_{$value[$i][0]}{$c}{$i} ";
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $c seems to be never defined.
Loading history...
64
                $return['data']["q_{$value[$i][0]}{$c}{$i}"] = $value[$i][2];
65
                continue;
66
            }
67
68
            $return['where'] .= " {$key} {$value[$i][0]} {$value[$i][1]} (";
69
70
            foreach($value[$i][2] as $v => $valu){
71
                $return['where'] .= " :q_{$value[$i][0]}{$c}{$i}_{$v},";
72
                $return['data']["q_{$value[$i][0]}{$c}{$i}_{$v}"] = $valu;
73
            }
74
75
            $return['where'] = substr($return['where'],0,-1) .') ';
76
        }
77
        return $return;
78
    }
79
80
    protected function mountWhereExec(): array
81
    {
82
        $return = ['where' => '', 'data' => []];
83
        $c = -1;
84
        foreach ($this->where as $key => $value) {
85
            $c++;
86
            $key = (!$key) ? '' : " {$key} ";
87
88
            if(!is_array($value[0])){
89
                $return['where'] .= " {$key} {$value[0]} {$value[1]} :q_{$value[0]}{$c} ";
90
                $return['data']["q_{$value[0]}{$c}"] = $value[2];
91
                continue;
92
            }
93
94
            $return = array_merge($return,$this->mountWheres($value, $key));
95
            
96
        }
97
        return $return;
98
    }
99
100
    protected function mountBetweenExec(): array
101
    {
102
        $return = ['where' => '', 'data' => []];
103
104
        $c = -1;
105
        foreach($this->between as $field => $value){
106
            $c++;
107
            $condition = (count(explode(' ',$field)) > 2) ? ' '.explode(' ',$field)[0].' ' : ' AND ';
108
            $field = str_replace(['AND','OR',' '],'',$field);
109
            $return['where'] .= " {$condition} {$field} BETWEEN :q_1{$field}{$c} AND :q_2{$field}{$c} ";
110
            $return['data'] = [
111
                "q_1{$field}{$c}" => (date_format( date_create_from_format(DATAMANAGER_CONFIG['dateformat'],$value[0]) , 'Y-m-d')),
112
                "q_2{$field}{$c}" => (date_format( date_create_from_format(DATAMANAGER_CONFIG['dateformat'],$value[1]) , 'Y-m-d'))
113
            ];
114
        }
115
        return $return;
116
    }
117
118
    protected function mountSelect()
119
    {
120
        $select = implode(',',array_keys($this->select));
121
122
        $this->query = str_replace('*', $select,$this->query);
123
    }
124
125
    protected function mountLimit()
126
    {
127
        if(!is_null($this->limit)){
128
            $this->query .= " LIMIT {$this->limit}";
129
        }
130
    }
131
132
    protected function mountOffset()
133
    {
134
        if(!is_null($this->offset)){
135
            $this->query .= " OFFSET {$this->offset}";
136
        }
137
    }
138
139
}
140