Passed
Push — master ( 2c321e...f12c68 )
by Henri
01:20
created

Datamanager::execute()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 35
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 1
Metric Value
cc 3
eloc 19
c 6
b 0
f 1
nc 2
nop 0
dl 0
loc 35
rs 9.6333
1
<?php
2
3
namespace HnrAzevedo\Datamanager;
4
5
use HnrAzevedo\Datamanager\DatamanagerException;
6
7
class Datamanager
8
{
9
    use DataTrait, SynchronizeTrait, EntityTrait, MagicsTrait, DebugTrait;
10
11
    protected ?string $table = null;
12
    protected ?string $primary = null;
13
    protected array $data = [];
14
    protected array $where = [''=> ["1",'=',"1"] ];
15
    protected array $between = [];    
16
17
    public function getData(): ?array
18
    {
19
        return $this->data;
20
    }
21
22
    public function getCount(): int
23
    {
24
        return $this->count;
25
    }
26
27
    public function except($deniable): Datamanager
28
    {
29
        $deniable = (is_array($deniable)) ? $deniable : [$deniable];
30
31
        foreach ($deniable as $field) {
32
            if(!array_key_exists($field,$this->data)){
33
                throw new DatamanagerException("{$field} field does not exist in the table {$this->table}.");
34
            }
35
            $this->excepts[$field] = true;
36
        }
37
38
        return $this;
39
    }
40
41
    public function deny(): Datamanager
42
    {
43
        foreach ($this->excepts as $field => $value) {
44
            unset($this->select[$field]);
45
        }
46
        return $this;
47
    }
48
49
    public function orderBy(string $field, string $ord = 'ASC'): Datamanager
50
    {
51
        $this->isSettable( str_replace(['asc','ASC','desc','DESC',' '],'',$field) );
52
53
        $ord = (strpos(strtolower($field),'asc') || strpos(strtolower($field),'desc')) ? '' : $ord;
54
55
        $this->order = " ORDER BY {$field} {$ord} ";
56
        return $this;
57
    }
58
59
    public function only($params): Datamanager
60
    {
61
        $params = (is_array($params)) ? $params : [$params];
62
        $this->select = [];
63
64
        foreach ($params as $field) {
65
            $this->isSettable($field);
66
            $this->select[$field] = true;
67
        }
68
69
        if(!is_null($this->primary)){
70
            $this->select[$this->primary] = true;
71
        }
72
        return $this;
73
    }
74
75
    public function where(array $where): Datamanager
76
    {
77
        $this->where['AND'] = (array_key_exists('AND',$this->where)) ?? '';
78
        $w = [];
79
        foreach ($where as $condition => $values) {
80
81
            if(!is_array($values)){
82
                $w['AND'][] = $values;
83
                continue;
84
            }
85
86
            $this->check_where_array($values);
87
88
            $w[(is_int($condition) ? 'AND' : $condition)][] = $values;
89
                       
90
        }
91
92
        $this->where = array_merge($this->where,$w);
93
94
        return $this;
95
    }
96
97
    public function between(array $bet): Datamanager
98
    {
99
        $this->between = array_merge($this->between, $bet);
100
        return $this;
101
    }
102
103
    public function limit(string $limit): Datamanager
104
    {
105
        $this->limit = $limit;
106
        return $this;
107
    }
108
109
    public function offset(int $offset): Datamanager
110
    {
111
        $this->checkLimit();
112
113
        $this->offset = $offset;
114
        return $this;
115
    }
116
117
    public function result(): array
118
    {
119
        return $this->result;
120
    }
121
122
    public function first(): Datamanager
123
    {
124
        return  (count($this->result) > 0) ? $this->setByDatabase($this->result[0]) : $this;
125
    }
126
127
    public function setByDatabase(array $arrayValues): Datamanager
128
    {
129
        $clone = clone $this;
130
        
131
        $clone->result = [
132
            0 => $this->result[0]
133
        ];
134
135
        $clone->count = 1;
136
137
        foreach ($arrayValues as $key => $value) {
138
139
            $this->isSettable($key);
140
141
            $clone->data[$key]['value'] = $value;
142
143
        }
144
        return $clone;
145
    }
146
147
    public function toJson(): string
148
    {
149
        $string = '';
150
        foreach ($this->data as $key => $value) {
151
152
            if(gettype($value)==='object'){
153
                $value = $value->getData()[$this->primary]['value'];
154
            }
155
156
            $string .= '"'.$key.'"'.':"'.$this->$key.'",';
157
        }
158
        return str_replace(',}', '}', '{'.$string.'}');
159
    }
160
161
    private function removeById(): bool
162
    {
163
        $delete = $this->delete("{$this->primary}=:{$this->primary}","{$this->primary}={$this->getData()[$this->primary]['value']}");
164
165
        $this->check_fail();
166
167
        return $delete;
168
    }
169
170
    public function findById($id): Datamanager
171
    {
172
        return $this->where([$this->primary,'=',$id]);
173
    }
174
175
    public function execute(): Datamanager
176
    {
177
        if(!is_null($this->clause) && $this->clause == 'remove'){
178
            return $this->remove(true);
179
        }
180
181
        $this->deny();
182
        
183
        $this->mountSelect();
184
        
185
        $where = substr($this->mountWhereExec()['where'],0,-1);
186
        $where .= substr($this->mountBetweenExec()['where'],0,-1);
187
188
        $this->query .= " WHERE {$where} ";
189
190
        $this->query .= $this->order;
191
       
192
        $this->mountLimit();
193
        $this->mountOffset();
194
195
        
196
        $dataSelect = array_merge($this->mountWhereExec()['data'], $this->mountBetweenExec()['data']);
197
198
        $this->result = $this->select(
199
            $this->query, 
200
            $dataSelect
201
        );
202
203
        $this->check_fail();
204
205
        $this->count = count($this->result);
206
        $this->query = null;
207
        $this->where = [''=> ["1",'=',"1"] ];
208
209
        return $this;
210
    }
211
212
    public function find(?int $key = null): Datamanager
213
    {
214
        $this->query = " SELECT * FROM {$this->table} ";
215
        return (is_int($key)) ? $this->findById($key) : $this;
216
    }
217
218
    
219
    
220
}
221