-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask.php
40 lines (35 loc) · 1.09 KB
/
task.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
function createSelectQuery(string $table, array $fields = [], array $filter = []): string
{
$columns = (empty($fields) ? '*' : implode(', ', $fields));
$result = sprintf('select %s from %s', $columns, $table);
if (!empty($filter)) {
$conditions = [];
$operators = [
'>=',
'<=',
'!=',
'>',
'<',
'%',
];
foreach ($filter as $column => $value) {
$op = 'like';
if (is_array($value)) {
$op = 'in';
$value = '(' . implode(', ', $value) . ')';
} else {
foreach ($operators as $operator) {
if (strpos($column, $operator) === 0) {
$column = substr($column, strlen($operator));
$op = $operator;
break;
}
}
}
$conditions[] = sprintf('%s %s %s', $column, $op, $value);
}
$result .= ' where ' . implode(' AND ', $conditions);
}
return $result;
}