Passed
Push — master ( 1a5a52...83a14b )
by Henri
01:16
created

DataTrait::mountBetweenExec()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 0
dl 0
loc 14
rs 9.9666
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($this->upgradeable($key) && !$this->isIncremented($key)){
48
                $return['data'][$key] = $this->data[$key]['value'];
49
            }
50
        }
51
52
        return $return;
53
    }
54
55
    protected function mountWhereExec(): array
56
    {
57
        $return = ['where' => '', 'data' => []];
58
59
        foreach ($this->where as $key => $value) {
60
61
            $key = (!$key) ? '' : " {$key} ";
62
63
            if(is_array($value[0])){
64
65
                foreach ($value as $k => $v) {
66
                    $return['where'] .= " {$key} {$v[0]} {$v[1]} :q_{$v[0]} ";
67
                    $return['data']["q_{$v[0]}"] = $v[2];
68
                }
69
70
                continue;
71
            }
72
             
73
            $return['where'] .= " {$key} {$value[0]} {$value[1]} :q_{$value[0]} ";
74
            $return['data']["q_{$value[0]}"] = $value[2];
75
76
        }
77
        return $return;
78
    }
79
80
    protected function mountBetweenExec(): array
81
    {
82
        $return = ['where' => '', 'data' => []];
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);
87
            $return['where'] .= " {$condition} {$field} BETWEEN :q_1{$field} AND :q_2{$field} ";
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'))
91
            ];
92
        }
93
        return $return;
94
    }
95
96
    protected function mountSelect()
97
    {
98
        $select = implode(',',array_keys($this->select));
99
100
        $this->query = str_replace('*', $select,$this->query);
101
    }
102
103
    protected function mountLimit()
104
    {
105
        if(!is_null($this->limit)){
106
            $this->query .= " LIMIT {$this->limit}";
107
        }
108
    }
109
110
    protected function mountOffset()
111
    {
112
        if(!is_null($this->offset)){
113
            $this->query .= " OFFSET {$this->offset}";
114
        }
115
    }
116
117
}