Passed
Branch master (8f2ee9)
by Henri
02:28 queued 01:06
created

DataTrait::mountSave()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 7
nc 4
nop 0
dl 0
loc 15
rs 8.8333
c 0
b 0
f 0
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
        foreach($this->where as $clause => $condition){
28
            if(strlen($clause) === 0){
29
                $return['where'] .= " {$clause} {$condition[0]} {$condition[1]} :q_{$condition[0]} ";
30
                $return['data'] .= "q_{$condition[0]}={$condition[2]}&";
31
                continue;
32
            }
33
                
34
            foreach($condition as $value){
35
                $return['where'] .= " {$clause} {$value[0]} {$value[1]} :q_{$value[0]} ";
36
                $return['data'] .= "q_{$value[0]}={$value[2]}&";
37
            }
38
        }
39
        return $return;
40
    }   
41
42
    protected function mountSave(): array
43
    {
44
        $return = ['data' => []];
45
46
        foreach ($this->data as $key => $value) {
47
            if(strstr($this->data[$key]['extra'],'auto_increment') && $key !== $this->primary){
48
                continue;
49
            }
50
51
            if(($this->data[$key]['changed'] && $this->data[$key]['upgradeable']) || $this->primary === $key){
52
                $return['data'][$key] = $this->data[$key]['value'];
53
            }
54
        }
55
56
        return $return;
57
    }
58
59
    protected function mountWhereExec(): array
60
    {
61
        $return = ['where' => '', 'data' => []];
62
63
        foreach ($this->where as $key => $value) {
64
65
            $key = (!$key) ? '' : " {$key} ";
66
67
            if(is_array($value[0])){
68
69
                foreach ($value as $k => $v) {
70
                    $return['where'] .= " {$key} {$v[0]} {$v[1]} :q_{$v[0]} ";
71
                    $return['data']["q_{$v[0]}"] = $v[2];
72
                }
73
74
                continue;
75
            }
76
             
77
            $return['where'] .= " {$key} {$value[0]} {$value[1]} :q_{$value[0]} ";
78
            $return['data']["q_{$value[0]}"] = $value[2];
79
80
        }
81
        return $return;
82
    }
83
84
    protected function mountSelect()
85
    {
86
        $select = implode(',',array_keys($this->select));
87
88
        $this->query = str_replace('*', $select,$this->query);
89
    }
90
91
    protected function mountLimit()
92
    {
93
        if(!is_null($this->limit)){
94
            $this->query .= " LIMIT {$this->limit}";
95
        }
96
    }
97
98
    protected function mountOffset()
99
    {
100
        if(!is_null($this->offset)){
101
            $this->query .= " OFFSET {$this->offset}";
102
        }
103
    }
104
105
}