Passed
Push — master ( 511eb5...e7d893 )
by Henri
03:04 queued 01:47
created

Datamanager::toEntity()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 2
b 0
f 0
nc 3
nop 0
dl 0
loc 16
rs 10
1
<?php
2
3
namespace HnrAzevedo\Datamanager;
4
5
use Exception;
6
7
abstract class Datamanager
8
{
9
    use DataTrait, SynchronizeTrait, EntityTrait;
10
11
    protected ?string $table = null;
12
    protected ?string $primary = null;
13
    protected array $data = [];
14
    protected array $where = [''=> ["1",'=',"1"] ];
15
16
    public function __set(string $prop,$value)
17
    {
18
        if(is_array($value)){
19
            $attr = array_keys($value)[0];
20
            $this->data[$prop][$attr] = $value[$attr];
21
            return $this;
22
        }
23
24
        $this->isSettable($prop);
25
26
        $this->data[$prop]['changed'] = true;
27
        $this->data[$prop]['value'] = $value;
28
        
29
        return $this;
30
    }
31
32
    public function getData(): ?array
33
    {
34
        return $this->data;
35
    }
36
37
    public function __get(string $field)
38
    {
39
        $this->isSettable($field);
40
        return $this->data[$field]['value'];
41
    }
42
43
    public function getCount(): int
44
    {
45
        return $this->count;
46
    }
47
48
    public function except($deniable)
49
    {
50
        $deniable = (is_array($deniable)) ? $deniable : [$deniable];
51
52
        foreach ($deniable as $field) {
53
            if(!array_key_exists($field,$this->data)){
54
                throw new Exception("{$field} field does not exist in the table {$this->table}.");
55
            }
56
57
            $this->excepts[$field] = true;
58
        }
59
60
        return $this;
61
    }
62
63
    public function deny()
64
    {
65
        foreach ($this->excepts as $field => $value) {
66
            unset($this->select[$field]);
67
        }
68
        return $this;
69
    }
70
71
    public function orderBy(string $field, string $ord = 'ASC')
72
    {
73
        $this->isSettable( str_replace(['asc','ASC','desc','DESC',' '],'',$field) );
74
75
        $ord = (strpos(strtolower($field),'asc') || strpos(strtolower($field),'desc')) ? '' : $ord;
76
77
        $this->order = " ORDER BY {$field} {$ord} ";
78
        return $this;
79
    }
80
81
    public function only($params)
82
    {
83
        $params = (is_array($params)) ? $params : [$params];
84
        $this->select = [];
85
86
        foreach ($params as $field) {
87
88
            $this->isSettable($field);
89
90
            $this->select[$field] = true;
91
        }
92
        $this->select[$this->primary] = true;
93
94
        return $this;
95
    }
96
97
    public function where(array $where)
98
    {
99
        $this->where['AND'] = (array_key_exists('AND',$this->where)) ?? '';
100
        $w = [];
101
        foreach ($where as $condition => $values) {
102
103
            if(!is_array($values)){
104
                $w['AND'][] = $values;
105
                continue;
106
            }
107
108
            $this->check_where_array($values);
109
110
            $w[(is_int($condition) ? 'AND' : $condition)][] = $values;
111
                       
112
        }
113
114
        $this->where = array_merge($this->where,$w);
115
116
        return $this;
117
    }
118
119
    public function limit(string $limit)
120
    {
121
        $this->limit = $limit;
122
        return $this;
123
    }
124
125
    public function offset(int $offset)
126
    {
127
        $this->checkLimit();
128
129
        $this->offset = $offset;
130
        return $this;
131
    }
132
133
    public function result(): array
134
    {
135
        return $this->result;
136
    }
137
138
    public function first()
139
    {
140
        return  (count($this->result) > 0) ? $this->setByDatabase($this->result[0]) : $this;
141
    }
142
143
    public function setByDatabase(array $arrayValues)
144
    {
145
        $clone = clone $this;
146
        
147
        $clone->result = [
148
            0 => $this->result[0]
149
        ];
150
151
        $clone->count = 1;
152
153
        foreach ($arrayValues as $key => $value) {
154
155
            $this->isSettable($key);
156
157
            $clone->data[$key]['value'] = $value;
158
159
        }
160
        return $clone;
161
    }
162
163
    public function toJson(): string
164
    {
165
        $string = '';
166
        foreach ($this->data as $key => $value) {
167
168
            if(gettype($value)==='object'){
169
                $value = $value->getData()[$this->primary]['value'];
170
            }
171
172
            $string .= '"'.$key.'"'.':"'.$value.'",';
173
        }
174
        return str_replace(',}', '}', '{'.$string.'}');
175
    }
176
177
    private function removeById(): bool
0 ignored issues
show
Unused Code introduced by
The method removeById() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
178
    {
179
        $delete = $this->delete("{$this->primary}=:{$this->primary}","{$this->primary}={$this->getData()[$this->primary]['value']}");
180
181
        $this->check_fail();
182
183
        return $delete;
184
    }
185
186
    public function findById($id)
187
    {
188
        return $this->where([$this->primary,'=',$id]);
189
    }
190
191
    public function execute()
192
    {
193
        if(!is_null($this->clause) && $this->clause == 'remove'){
194
            return $this->remove(true);
195
        }
196
197
        $this->deny();
198
        
199
        $this->mountSelect();
200
        
201
        $where = substr($this->mountWhereExec()['where'],0,-1);
202
        $this->query .= " WHERE {$where} ";
203
204
        $this->query .= $this->order;
205
       
206
        $this->mountLimit();
207
        $this->mountOffset();
208
209
        $this->result = $this->select($this->query, $this->mountWhereExec()['data']);
210
211
        $this->check_fail();
212
213
        $this->count = count($this->result);
214
        $this->query = null;
215
216
        return $this;
217
    }
218
219
    public function find(?int $key = null)
220
    {
221
        $this->query = " SELECT * FROM {$this->table} ";
222
        return (is_int($key)) ? $this->findById($key) : $this;
223
    }
224
225
    
226
    
227
}
228