|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace HnrAzevedo\Datamanager; |
|
4
|
|
|
|
|
5
|
|
|
use HnrAzevedo\Datamanager\DatamanagerException; |
|
6
|
|
|
|
|
7
|
|
|
abstract class Datamanager |
|
8
|
|
|
{ |
|
9
|
|
|
use DataTrait, SynchronizeTrait, EntityTrait, MagicsTrait; |
|
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) |
|
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() |
|
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') |
|
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) |
|
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
|
|
|
$this->select[$this->primary] = true; |
|
69
|
|
|
|
|
70
|
|
|
return $this; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function where(array $where) |
|
74
|
|
|
{ |
|
75
|
|
|
$this->where['AND'] = (array_key_exists('AND',$this->where)) ?? ''; |
|
76
|
|
|
$w = []; |
|
77
|
|
|
foreach ($where as $condition => $values) { |
|
78
|
|
|
|
|
79
|
|
|
if(!is_array($values)){ |
|
80
|
|
|
$w['AND'][] = $values; |
|
81
|
|
|
continue; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$this->check_where_array($values); |
|
85
|
|
|
|
|
86
|
|
|
$w[(is_int($condition) ? 'AND' : $condition)][] = $values; |
|
87
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$this->where = array_merge($this->where,$w); |
|
91
|
|
|
|
|
92
|
|
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function between(array $bet) |
|
96
|
|
|
{ |
|
97
|
|
|
$this->between = array_merge($this->between, $bet); |
|
98
|
|
|
return $this; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function limit(string $limit) |
|
102
|
|
|
{ |
|
103
|
|
|
$this->limit = $limit; |
|
104
|
|
|
return $this; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function offset(int $offset) |
|
108
|
|
|
{ |
|
109
|
|
|
$this->checkLimit(); |
|
110
|
|
|
|
|
111
|
|
|
$this->offset = $offset; |
|
112
|
|
|
return $this; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function result(): array |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->result; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function first() |
|
121
|
|
|
{ |
|
122
|
|
|
return (count($this->result) > 0) ? $this->setByDatabase($this->result[0]) : $this; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function setByDatabase(array $arrayValues) |
|
126
|
|
|
{ |
|
127
|
|
|
$clone = clone $this; |
|
128
|
|
|
|
|
129
|
|
|
$clone->result = [ |
|
130
|
|
|
0 => $this->result[0] |
|
131
|
|
|
]; |
|
132
|
|
|
|
|
133
|
|
|
$clone->count = 1; |
|
134
|
|
|
|
|
135
|
|
|
foreach ($arrayValues as $key => $value) { |
|
136
|
|
|
|
|
137
|
|
|
$this->isSettable($key); |
|
138
|
|
|
|
|
139
|
|
|
$clone->data[$key]['value'] = $value; |
|
140
|
|
|
|
|
141
|
|
|
} |
|
142
|
|
|
return $clone; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public function toJson(): string |
|
146
|
|
|
{ |
|
147
|
|
|
$string = ''; |
|
148
|
|
|
foreach ($this->data as $key => $value) { |
|
149
|
|
|
|
|
150
|
|
|
if(gettype($value)==='object'){ |
|
151
|
|
|
$value = $value->getData()[$this->primary]['value']; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
$string .= '"'.$key.'"'.':"'.$value.'",'; |
|
155
|
|
|
} |
|
156
|
|
|
return str_replace(',}', '}', '{'.$string.'}'); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
private function removeById(): bool |
|
|
|
|
|
|
160
|
|
|
{ |
|
161
|
|
|
$delete = $this->delete("{$this->primary}=:{$this->primary}","{$this->primary}={$this->getData()[$this->primary]['value']}"); |
|
162
|
|
|
|
|
163
|
|
|
$this->check_fail(); |
|
164
|
|
|
|
|
165
|
|
|
return $delete; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
public function findById($id) |
|
169
|
|
|
{ |
|
170
|
|
|
return $this->where([$this->primary,'=',$id]); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
public function execute() |
|
174
|
|
|
{ |
|
175
|
|
|
if(!is_null($this->clause) && $this->clause == 'remove'){ |
|
176
|
|
|
return $this->remove(true); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
$this->deny(); |
|
180
|
|
|
|
|
181
|
|
|
$this->mountSelect(); |
|
182
|
|
|
|
|
183
|
|
|
$where = substr($this->mountWhereExec()['where'],0,-1); |
|
184
|
|
|
$where .= substr($this->mountBetweenExec()['where'],0,-1); |
|
185
|
|
|
|
|
186
|
|
|
$this->query .= " WHERE {$where} "; |
|
187
|
|
|
|
|
188
|
|
|
$this->query .= $this->order; |
|
189
|
|
|
|
|
190
|
|
|
$this->mountLimit(); |
|
191
|
|
|
$this->mountOffset(); |
|
192
|
|
|
|
|
193
|
|
|
$this->result = $this->select( |
|
194
|
|
|
$this->query, |
|
195
|
|
|
array_merge($this->mountWhereExec()['data'], $this->mountBetweenExec()['data']) |
|
196
|
|
|
); |
|
197
|
|
|
|
|
198
|
|
|
$this->check_fail(); |
|
199
|
|
|
|
|
200
|
|
|
$this->count = count($this->result); |
|
201
|
|
|
$this->query = null; |
|
202
|
|
|
|
|
203
|
|
|
return $this; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
public function find(?int $key = null) |
|
207
|
|
|
{ |
|
208
|
|
|
$this->query = " SELECT * FROM {$this->table} "; |
|
209
|
|
|
return (is_int($key)) ? $this->findById($key) : $this; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
|
|
213
|
|
|
|
|
214
|
|
|
} |
|
215
|
|
|
|
This check looks for private methods that have been defined, but are not used inside the class.