1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Component\Model\Orm; |
4
|
|
|
|
5
|
|
|
use App\Config; |
6
|
|
|
use App\Container; |
7
|
|
|
use NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder; |
8
|
|
|
use NilPortugues\Sql\QueryBuilder\Manipulation\Select; |
9
|
|
|
use NilPortugues\Sql\QueryBuilder\Manipulation\Update; |
10
|
|
|
use NilPortugues\Sql\QueryBuilder\Manipulation\Insert; |
11
|
|
|
use NilPortugues\Sql\QueryBuilder\Manipulation\Delete; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Poor man orm |
15
|
|
|
*/ |
16
|
|
|
class Orm implements IOrm |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* config |
21
|
|
|
* |
22
|
|
|
* @var Config |
23
|
|
|
*/ |
24
|
|
|
protected $config; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* columns fields |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $columns; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* where criterias |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
protected $where; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* table name |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $tablename; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* table primary key |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
protected $primary; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* database name |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
protected $dbname; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* pool name |
58
|
|
|
* @var string |
59
|
|
|
*/ |
60
|
|
|
protected $poolname; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* query builder instance |
64
|
|
|
* @var GenericBuilder |
65
|
|
|
* @see https://github.com/nilportugues/php-sql-query-builder |
66
|
|
|
*/ |
67
|
|
|
protected $queryBuilder; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* query from builder |
71
|
|
|
* @var object |
72
|
|
|
*/ |
73
|
|
|
protected $query; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* instanciate |
77
|
|
|
* @param Container $container |
78
|
|
|
*/ |
79
|
14 |
|
public function __construct(Container $container) |
80
|
|
|
{ |
81
|
14 |
|
$this->config = $container->getService(Config::class); |
82
|
14 |
|
$this->queryBuilder = new GenericBuilder(); |
83
|
14 |
|
$this->query = null; |
84
|
14 |
|
$this->columns = []; |
85
|
14 |
|
$this->where = []; |
86
|
14 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* set required columns |
91
|
|
|
* |
92
|
|
|
* @param array $columns |
93
|
|
|
* @return Orm |
94
|
|
|
*/ |
95
|
6 |
|
public function setColumns(array $columns): Orm |
96
|
|
|
{ |
97
|
6 |
|
$this->columns = $columns; |
98
|
6 |
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* find a record with columns field matching where criterias |
103
|
|
|
* @param array $columns |
104
|
|
|
* @param array $where |
105
|
|
|
*/ |
106
|
4 |
|
public function find(array $columns = [], array $where = []): Orm |
107
|
|
|
{ |
108
|
4 |
|
$this->where = $where; |
109
|
|
|
$this |
110
|
4 |
|
->setColumns($columns) |
111
|
4 |
|
->setQuery(new Select()) |
112
|
4 |
|
->build($this->tablename, $this->columns, $this->where); |
113
|
4 |
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* count records matching where criterias. |
118
|
|
|
* aliases is formely [columnn => column_alias] |
119
|
|
|
* |
120
|
|
|
* @param array $where |
121
|
|
|
* @param array $aliases |
122
|
|
|
* @return Orm |
123
|
|
|
*/ |
124
|
2 |
|
public function count(array $where = [], array $aliases = []): Orm |
125
|
|
|
{ |
126
|
2 |
|
$this->where = $where; |
127
|
2 |
|
$this->setQuery(new Select()); |
128
|
2 |
|
if (empty($aliases)) { |
129
|
1 |
|
$this->query->count(); |
130
|
|
|
} else { |
131
|
1 |
|
reset($aliases); |
132
|
1 |
|
$fistKey = key($aliases); |
133
|
1 |
|
$aliasValue = $aliases[$fistKey]; |
134
|
1 |
|
$this->query->count($fistKey, $aliasValue); |
135
|
|
|
} |
136
|
2 |
|
$this->buildWhere($this->where); |
137
|
2 |
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* update a record with columns fields matching where criterias |
142
|
|
|
* @param array $columns |
143
|
|
|
* @param array $where |
144
|
|
|
*/ |
145
|
1 |
|
public function update(array $columns = [], array $where = []): Orm |
146
|
|
|
{ |
147
|
1 |
|
$this->where = $where; |
148
|
1 |
|
return $this->setColumns($columns) |
149
|
1 |
|
->setQuery(new Update()) |
150
|
1 |
|
->build($this->tablename, $this->columns, $this->where); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* insert record with columns fields |
155
|
|
|
* @param array $columns |
156
|
|
|
*/ |
157
|
1 |
|
public function insert(array $columns = []): Orm |
158
|
|
|
{ |
159
|
|
|
$this |
160
|
1 |
|
->setColumns($columns) |
161
|
1 |
|
->setQuery(new Insert()) |
162
|
1 |
|
->build($this->tablename, $this->columns, []); |
163
|
1 |
|
return $this; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* delete matching where criterias |
168
|
|
|
* @param array $where |
169
|
|
|
*/ |
170
|
1 |
|
public function delete(array $where = []): Orm |
171
|
|
|
{ |
172
|
1 |
|
$this->where = $where; |
173
|
|
|
return $this |
174
|
1 |
|
->setQuery(new Delete()) |
175
|
1 |
|
->build($this->tablename, [], $this->where); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* builder instance |
180
|
|
|
* @return GenericBuilder |
181
|
|
|
*/ |
182
|
1 |
|
public function getQueryBuilder(): GenericBuilder |
183
|
|
|
{ |
184
|
1 |
|
return $this->queryBuilder; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* query instance |
189
|
|
|
* @return object |
190
|
|
|
*/ |
191
|
9 |
|
public function getQuery() |
192
|
|
|
{ |
193
|
9 |
|
return $this->query; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* set query instance |
198
|
|
|
* |
199
|
|
|
* @param Select|Update|Insert|Delete $query |
200
|
|
|
* @return Orm |
201
|
|
|
*/ |
202
|
10 |
|
public function setQuery($query): Orm |
203
|
|
|
{ |
204
|
10 |
|
$this->query = $query; |
205
|
10 |
|
return $this; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* query sql string |
210
|
|
|
* @return string |
211
|
|
|
*/ |
212
|
1 |
|
public function getSql() |
213
|
|
|
{ |
214
|
1 |
|
return $this->queryBuilder->write($this->query); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* build query |
219
|
|
|
* |
220
|
|
|
* @param string $tablename |
221
|
|
|
* @param array $columns |
222
|
|
|
* @param array $where |
223
|
|
|
* @return Orm |
224
|
|
|
*/ |
225
|
4 |
|
protected function build(string $tablename, array $columns, array $where): Orm |
226
|
|
|
{ |
227
|
4 |
|
if (false === is_object($this->getQuery())) { |
228
|
1 |
|
throw new \Exception('Build : Invalid query instance'); |
229
|
|
|
} |
230
|
3 |
|
$queryClassname = get_class($this->getQuery()); |
231
|
|
|
$allowedClasses = [ |
232
|
3 |
|
Update::class, Select::class, Delete::class, Insert::class |
233
|
|
|
]; |
234
|
3 |
|
if (false === in_array($queryClassname, $allowedClasses)) { |
235
|
1 |
|
throw new \Exception('Build : Invalid query type'); |
236
|
|
|
} |
237
|
2 |
|
$this->query->setTable($tablename); |
238
|
2 |
|
switch ($queryClassname) { |
239
|
|
|
case Select::class: |
240
|
1 |
|
$this->query->setColumns($columns); |
241
|
1 |
|
break; |
242
|
|
|
case Update::class: |
243
|
1 |
|
if (empty($columns)) { |
244
|
1 |
|
throw new \Exception( |
245
|
1 |
|
'Build : Update requires not empty payload' |
246
|
|
|
); |
247
|
|
|
} |
248
|
|
|
if (empty($where)) { |
249
|
|
|
throw new \Exception( |
250
|
|
|
'Build : Update requires at least one condition' |
251
|
|
|
); |
252
|
|
|
} |
253
|
|
|
$this->query->setValues($columns); |
254
|
|
|
break; |
255
|
|
|
case Insert::class: |
256
|
|
|
if (empty($columns)) { |
257
|
|
|
throw new \Exception( |
258
|
|
|
'Build : Insert requires not empty payload' |
259
|
|
|
); |
260
|
|
|
} |
261
|
|
|
$this->query->setValues($columns); |
262
|
|
|
break; |
263
|
|
|
case Delete::class: |
264
|
|
|
if (empty($where)) { |
265
|
|
|
throw new \Exception( |
266
|
|
|
'Build : Delete requires at least one condition' |
267
|
|
|
); |
268
|
|
|
} |
269
|
|
|
break; |
270
|
|
|
default: |
271
|
|
|
break; |
272
|
|
|
} |
273
|
1 |
|
$this->buildWhere($where); |
274
|
1 |
|
return $this; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* build where condition on query |
279
|
|
|
* |
280
|
|
|
* @param array $where |
281
|
|
|
* @return Orm |
282
|
|
|
*/ |
283
|
|
|
protected function buildWhere(array $where): Orm |
284
|
|
|
{ |
285
|
|
|
if (false === empty($where)) { |
286
|
|
|
foreach ($where as $k => $v) { |
287
|
|
|
$whereOperator = $this->getWhereOperator($k, $v); |
288
|
|
|
$this->query->where()->{$whereOperator}($k, $v); |
289
|
|
|
} |
290
|
|
|
} |
291
|
|
|
return $this; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* check where condition ket values to find operators |
296
|
|
|
* |
297
|
|
|
* @param string $whereColumn |
298
|
|
|
* @return string |
299
|
|
|
*/ |
300
|
|
|
protected function getWhereOperator(string &$whereColumn, $value): string |
301
|
|
|
{ |
302
|
|
|
$hasArray = is_array($value); |
303
|
|
|
$operator = $whereColumn[strlen($whereColumn) - 1]; |
304
|
|
|
$hasOperator = in_array($operator, self::OPERATORS); |
305
|
|
|
if (false === $hasOperator) { |
306
|
|
|
return ($hasArray) ? 'in' : 'equals'; |
307
|
|
|
} |
308
|
|
|
foreach (self::OPERATORS as $op) { |
309
|
|
|
$whereColumn = str_replace($op, '', $whereColumn); |
310
|
|
|
} |
311
|
|
|
switch ($operator) { |
312
|
|
|
case '!': |
313
|
|
|
return ($hasArray) ? 'notIn' : 'notEquals'; |
314
|
|
|
break; |
|
|
|
|
315
|
|
|
case '<': |
316
|
|
|
return 'lessThan'; |
317
|
|
|
break; |
318
|
|
|
case '>': |
319
|
|
|
return 'greaterThan'; |
320
|
|
|
break; |
321
|
|
|
case '#': |
322
|
|
|
return 'like'; |
323
|
|
|
break; |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
} |
327
|
|
|
|
The
break
statement is not necessary if it is preceded for example by areturn
statement:If you would like to keep this construct to be consistent with other
case
statements, you can safely mark this issue as a false-positive.