Completed
Push — develop ( b248ea...be3180 )
by Michael
03:02
created

QueryBuilder::getWhereCondition()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
cc 3
eloc 5
nc 2
nop 3
crap 3
1
<?php
2
3
/**
4
 * This file is part of the miBadger package.
5
 *
6
 * @author Michael Webbers <[email protected]>
7
 * @license http://opensource.org/licenses/Apache-2.0 Apache v2 License
8
 * @version 1.0.0
9
 */
10
11
namespace miBadger\Query;
12
13
/**
14
 * The query builder class.
15
 *
16
 * @since 1.0.0
17
 */
18
class QueryBuilder implements QueryInterface
19
{
20
	/* @var string The modifier. SELECT, INSERT INTO, UPDATE or DELETE */
21
	private $modifier;
22
23
	/* @var string The table name. */
24
	private $table;
25
26
	/* @var array The columns. */
27
	private $columns;
28
29
	/* @var array The values. */
30
	private $values;
31
32
	/* @var array The where conditions. */
33
	private $where;
34
35
	/* @var array The group by conditions. */
36
	private $groupBy;
37
38
	/* @var array The order by conditions. */
39
	private $orderBy;
40
41
	/* @var string The limit. */
42
	private $limit;
43
44
	/* @var string The offset. */
45
	private $offset;
46
47
	/**
48
	 * Construct a query object with the given table.
49
	 *
50
	 * @param string $table
51
	 */
52 35
	public function __construct($table)
53
	{
54 35
		$this->table = $table;
55 35
		$this->where = [];
56 35
		$this->groupBy = [];
57 35
		$this->orderBy = [];
58 35
	}
59
60
	/**
61
	 * Returns a string representation of the query object.
62
	 *
63
	 * @return string a string representation of the query object.
64
	 */
65 35
	public function __toString()
66
	{
67 35
		switch ($this->modifier) {
68 35
			case self::SELECT:
69 23
				$result = $this->getSelectClause();
70
71 23
				if ($where = $this->getWhereClause()) {
72 7
					$result .= ' ' . $where;
73 7
				}
74
75 23
				if ($groupBy = $this->getGroupByClause()) {
76 2
					$result .= ' ' . $groupBy;
77 2
				}
78
79 23
				if ($orderBy = $this->getOrderByClause()) {
80 4
					$result .= ' ' . $orderBy;
81 4
				}
82
83 23
				if ($limit = $this->getLimitClause()) {
84 4
					$result .= ' ' . $limit;
85 4
				}
86
87 23
				if ($offset = $this->getOffsetClause()) {
88 2
					$result .= ' ' . $offset;
89 2
				}
90
91 23
				return $result;
92
93 12
			case self::INSERT:
94 3
				$result = $this->getInsertClause();
95
96 3
				return $result;
97
98 9 View Code Duplication
			case self::UPDATE:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99 4
				$result = $this->getUpdateClause();
100
101 4
				if ($where = $this->getWhereClause()) {
102 4
					$result .= ' ' . $where;
103 4
				}
104
105 4
				if ($limit = $this->getLimitClause()) {
106 1
					$result .= ' ' . $limit;
107 1
				}
108
109 4
				return $result;
110
111 5 View Code Duplication
			case self::DELETE:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
112 3
				$result = $this->getDeleteClause();
113
114 3
				if ($where = $this->getWhereClause()) {
115 2
					$result .= ' ' . $where;
116 2
				}
117
118 3
				if ($limit = $this->getLimitClause()) {
119 1
					$result .= ' ' . $limit;
120 1
				}
121
122 3
				return $result;
123
124 2
			default:
125 2
				return '';
126 2
		}
127
	}
128
129
	/**
130
	 * {@inheritdoc}
131
	 */
132 23
	public function select($columns = ['*'])
133
	{
134 23
		$this->columns = is_array($columns) ? $columns : func_get_args();
135 23
		$this->modifier = self::SELECT;
136
137 23
		return $this;
138
	}
139
140
	/**
141
	 * Returns the select clause.
142
	 *
143
	 * @return string the select clause.
144
	 */
145 23
	public function getSelectClause()
146
	{
147 23
		return sprintf('SELECT %s FROM %s', implode(', ', $this->columns), $this->table);
148
	}
149
150
	/**
151
	 * {@inheritdoc}
152
	 */
153 3
	public function insert(array $values)
154
	{
155 3
		$this->values = $values;
156 3
		$this->modifier = self::INSERT;
157
158 3
		return $this;
159
	}
160
161
	/**
162
	 * Returns the insert clause.
163
	 *
164
	 * @return string the insert clause.
165
	 */
166 3
	public function getInsertClause()
167
	{
168 3
		$columns = [];
169 3
		$values = [];
170
171 3
		foreach ($this->values as $key => $value) {
172 3
			$columns[] = $key;
173 3
			$values[] = sprintf('%s', $value);
174 3
		}
175
176 3
		return sprintf('INSERT INTO %s (%s) VALUES (%s)', $this->table, implode(', ', $columns), implode(', ', $values));
177
	}
178
179
	/**
180
	 * {@inheritdoc}
181
	 */
182 4
	public function update(array $values)
183
	{
184 4
		$this->values = $values;
185 4
		$this->modifier = self::UPDATE;
186
187 4
		return $this;
188
	}
189
190
	/**
191
	 * Returns the update clause.
192
	 *
193
	 * @return string the update clause.
194
	 */
195 4
	public function getUpdateClause()
196
	{
197 4
		$placeholders = [];
198
199 4
		foreach ($this->values as $key => $value) {
200 4
			$placeholders[] = sprintf('%s = %s', $key, $value);
201 4
		}
202
203 4
		return sprintf('UPDATE %s SET %s', $this->table, implode(', ', $placeholders));
204
	}
205
206
	/**
207
	 * {@inheritdoc}
208
	 */
209 3
	public function delete()
210
	{
211 3
		$this->modifier = self::DELETE;
212
213 3
		return $this;
214
	}
215
216
	/**
217
	 * Returns the delete clause.
218
	 *
219
	 * @return string the delete clause.
220
	 */
221 3
	public function getDeleteClause()
222
	{
223 3
		return sprintf('DELETE FROM %s', $this->table);
224
	}
225
226
	/**
227
	 * {@inheritdoc}
228
	 */
229 13
	public function where($column, $operator, $value)
230
	{
231 13
		$this->where[] = [$column, $operator, $value];
232
233 13
		return $this;
234
	}
235
236
	/**
237
	 * Returns the where clause.
238
	 *
239
	 * @return string the where clause.
240
	 */
241 30
	private function getWhereClause()
242
	{
243 30
		if (empty($this->where)) {
244 17
			return '';
245
		}
246
247 13
		$result = [];
248
249 13
		foreach ($this->where as $key => $value) {
250 13
			$result[] = $this->getWhereCondition($value[0], $value[1], $value[2]);
251 13
		}
252
253 13
		return sprintf('WHERE %s', implode(' AND ', $result));
254
	}
255
256
	/**
257
	 * Returns the where condition.
258
	 *
259
	 * @param string $column
260
	 * @param string $operator
261
	 * @param mixed $value
262
	 * @return string the where condition.
263
	 */
264 13
	private function getWhereCondition($column, $operator, $value)
265
	{
266 13
		if ($operator == 'IN') {
267 3
			return sprintf('%s IN (%s)', $column, is_array($value) ? implode(', ', $value) : $value);
268
		} else {
269 10
			return sprintf('%s %s %s', $column, $operator, $value);
270
		}
271
	}
272
273
	/**
274
	 * {@inheritdoc}
275
	 */
276 2
	public function groupBy($column)
277
	{
278 2
		$this->groupBy[] = $column;
279
280 2
		return $this;
281
	}
282
283
	/**
284
	 * Returns the group by clause.
285
	 *
286
	 * @return string the group by clause.
287
	 */
288 23
	public function getGroupByClause()
289
	{
290 23
		if (empty($this->groupBy)) {
291 21
			return '';
292
		}
293
294 2
		return sprintf('GROUP BY %s', implode(', ', $this->groupBy));
295
	}
296
297
	/**
298
	 * {@inheritdoc}
299
	 */
300 4
	public function orderBy($column, $order = null)
301
	{
302 4
		if (strcasecmp($order, 'asc') == 0) {
303 1
			$column .= ' ASC';
304 4
		} elseif(strcasecmp($order, 'desc') == 0) {
305 2
			$column .= ' DESC';
306 2
		}
307
308 4
		$this->orderBy[] = $column;
309
310 4
		return $this;
311
	}
312
313
	/**
314
	 * Returns the order by clause.
315
	 *
316
	 * @return string the order by clause.
317
	 */
318 23
	public function getOrderByClause()
319
	{
320 23
		if (empty($this->orderBy)) {
321 19
			return '';
322
		}
323
324 4
		return sprintf('ORDER BY %s', implode(', ', $this->orderBy));
325
	}
326
327
	/**
328
	 * {@inheritdoc}
329
	 */
330 6
	public function limit($limit)
331
	{
332 6
		$this->limit = $limit;
333
334 6
		return $this;
335
	}
336
337
	/**
338
	 * Returns the limit clause.
339
	 *
340
	 * @return string the limit clause.
341
	 */
342 30
	private function getLimitClause()
343
	{
344 30
		if (!$this->limit) {
345 24
			return '';
346
		}
347
348 6
		return sprintf('LIMIT %s', $this->limit);
349
	}
350
351
	/**
352
	 * {@inheritdoc}
353
	 */
354 2
	public function offset($offset)
355
	{
356 2
		$this->offset = $offset;
357
358 2
		return $this;
359
	}
360
361
	/**
362
	 * Returns the offset clause.
363
	 *
364
	 * @return string the offset clause.
365
	 */
366 23
	private function getOffsetClause()
367
	{
368 23
		if (!$this->limit || !$this->offset) {
369 21
			return '';
370
		}
371
372 2
		return sprintf('OFFSET %s', $this->offset);
373
	}
374
}
375