Completed
Push — master ( 04d014...522bfc )
by Midori
12:46
created

QueryMaker::reset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace midorikocak\querymaker;
6
7
use InvalidArgumentException;
8
9
use function array_keys;
10
use function array_map;
11
use function array_values;
12
use function implode;
13
use function in_array;
14
use function uniqid;
15
16
class QueryMaker implements QueryInterface
17
{
18
    private string $query;
19
    private string $statement;
20 21
    private array $params;
21
    private string $offset;
0 ignored issues
show
introduced by
The private property $offset is not used, and could be removed.
Loading history...
22 21
    private string $limit;
23 21
    private string $orderBy;
24 21
25 21
    public function __construct()
26
    {
27 18
        $this->reset();
28
    }
29 18
30 18
    private function reset(): void
31 18
    {
32 18
        $this->query = '';
33
        $this->statement = '';
34
        $this->params = [];
35 3
36
        $this->limit = '';
37 3
        $this->orderBy = '';
38 3
    }
39 3
40 3
    public function select($table, array $columns = ['*']): QueryInterface
41
    {
42
        $this->reset();
43 15
        $columnsText = implode(', ', $columns);
44
        $this->statement = 'SELECT ' . $columnsText . ' FROM ' . $table;
45 15
        $this->query = 'SELECT ' . $columnsText . ' FROM ' . $table;
46 15
        return $this;
47
    }
48
49 15
    public function update($table, array $values): QueryInterface
50
    {
51
        $this->reset();
52 15
        $this->statement = 'UPDATE ' . $table . ' SET ';
53 15
        $this->query = 'UPDATE ' . $table . ' SET ';
54 15
        $this->prepareParams($values, ', ');
55 15
        return $this;
56
    }
57
58 6
    public function insert($table, array $values): QueryInterface
59
    {
60 6
        $this->reset();
61 6
        $fields = implode(', ', array_keys($values));
62 6
        $params = implode(', ', array_map(fn($key) => ':' . $key, array_keys($values)));
63 6
        $queryValues = implode(', ', array_map(fn($value) => "'$value'", array_values($values)));
64
65
        $this->statement = "INSERT INTO $table ($fields) VALUES ($params)";
66 6
        $this->query = "INSERT INTO $table ($fields) VALUES ($queryValues)";
67
        $this->params = $values;
68 6
69 6
        return $this;
70 6
    }
71 6
72
    public function delete($table): QueryInterface
73
    {
74
        $this->reset();
75
        $this->statement = 'DELETE FROM ' . $table;
76
        $this->query = 'DELETE FROM ' . $table;
77
        return $this;
78
    }
79
80
    public function where($key, $value, string $operator = '='): QueryInterface
81
    {
82
        $this->checkOperator($operator);
83
84 21
        $this->statement .= ' WHERE ' . $key . $operator . ':' . $key;
85
        $this->query .= ' WHERE ' . $key . $operator . '\'' . $value . '\'';
86 21
        $this->params[$key] = $value;
87
        return $this;
88
    }
89 21
90
    public function and($key, $value, string $operator = '='): QueryInterface
91 21
    {
92
        $this->checkOperator($operator);
93
94
        $this->query .= ' AND ';
95
        $this->statement .= ' AND ';
96
        $this->prepareParam($key, $value, 'AND', $operator);
97
        return $this;
98
    }
99 12
100
    public function orderBy($key, string $order = 'ASC'): QueryInterface
101 12
    {
102 12
        if ($order !== 'DESC' && $order !== 'ASC') {
103
            throw new InvalidArgumentException('Invalid order value');
104 12
        }
105 12
106 12
        $this->orderBy .= ' ORDER BY ' . $key . ' ' . $order;
107
        return $this;
108
    }
109
110 12
    public function limit(int $limit): QueryInterface
111
    {
112
        $this->limit .= ' LIMIT ' . $limit;
113 12
        return $this;
114 12
    }
115 12
116
    public function offset(int $offset): QueryInterface
117 12
    {
118
        $this->limit .= ' OFFSET ' . $offset;
119
        return $this;
120
    }
121
122
    public function or($key, $value, $operator = '='): QueryInterface
123
    {
124
        $this->checkOperator($operator);
125
126
        $this->query .= " OR ";
127 12
        $this->statement .= " OR ";
128 12
        $this->prepareParam($key, $value, 'OR');
129 12
        return $this;
130
    }
131 9
132
    public function between($key, $before, $after): QueryInterface
133 9
    {
134 9
        $this->query .= $key . " BETWEEN $before AND $after";
135
        $this->statement .= $key . ' BETWEEN :before AND :after';
136
137
        $this->params['before'] = $before;
138
        $this->params['after'] = $after;
139
        return $this;
140
    }
141
142
    public function getQuery(): string
143
    {
144
        return $this->query . $this->orderBy . $this->limit;
145
    }
146
147
    public function getStatement(): string
148
    {
149
        return $this->statement . $this->orderBy . $this->limit;
150
    }
151
152
    public function getParams(): array
153
    {
154
        return $this->params;
155
    }
156
157
    private function prepareParams(array $values, string $glue, string $operator = '=')
158
    {
159
        $this->checkOperator($operator);
160
        $params = [];
161
        $queryValues = [];
162
163
        foreach ($values as $key => $value) {
164
            if (!isset($this->params[$key])) {
165
                $queryValues[] = $key . $operator . '\'' . $value . '\'';
166
                $params [] = $key . $operator . ':' . $key;
167
168
                $this->params[$key] = $value;
169
            } else {
170
                $uniqid = uniqid('', true);
171
                $queryValues[] = $key . $operator . '\'' . $value . '\'';
172
                $params [] = $key . $operator . ':' . $key . $uniqid;
173
174
                $this->params[$key . $uniqid] = $value;
175
            }
176
        }
177
178
        $this->query .= implode($glue, $queryValues);
179
        $this->statement .= implode($glue, $params);
180
    }
181
182
    private function prepareParam(string $key, $value, string $glue, $operator = '='): void
183
    {
184
        $this->prepareParams([$key => $value], $glue, $operator);
185
    }
186
187
    private function checkOperator(string $operator): void
188
    {
189
        $operators = ['=', '>', '>=', '<', '<=', 'LIKE'];
190
        if (!in_array($operator, $operators, true)) {
191
            throw new InvalidArgumentException('Invalid Operator');
192
        }
193
    }
194
}
195