Completed
Push — develop ( 748eae...d41f6b )
by Michael
02:52
created

QueryBuilder::getWhereClause()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.4285
cc 3
eloc 7
nc 3
nop 0
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 join conditions. */
33
	private $join;
34
35
	/* @var array The where conditions. */
36
	private $where;
37
38
	/* @var array The group by conditions. */
39
	private $groupBy;
40
41
	/* @var array The order by conditions. */
42
	private $orderBy;
43
44
	/* @var string The limit. */
45
	private $limit;
46
47
	/* @var string The offset. */
48
	private $offset;
49
50
	/**
51
	 * Construct a query builder object with the given table.
52
	 *
53
	 * @param string $table
54
	 */
55 45
	public function __construct($table)
56
	{
57 45
		$this->table = $table;
58 45
		$this->join = [];
59 45
		$this->where = [];
60 45
		$this->groupBy = [];
61 45
		$this->orderBy = [];
62 45
	}
63
64
	/**
65
	 * Returns a string representation of the query object.
66
	 *
67
	 * @return string a string representation of the query object.
68
	 */
69 45
	public function __toString()
70
	{
71 45
		switch ($this->modifier) {
72 45
			case self::SELECT:
73 33
				$result = $this->getSelectClause();
74
75 33
				if ($join = $this->getJoinClause()) {
76 8
					$result .= ' ' . $join;
77 8
				}
78
79 33
				if ($where = $this->getWhereClause()) {
80 9
					$result .= ' ' . $where;
81 9
				}
82
83 33
				if ($groupBy = $this->getGroupByClause()) {
84 2
					$result .= ' ' . $groupBy;
85 2
				}
86
87 33
				if ($orderBy = $this->getOrderByClause()) {
88 4
					$result .= ' ' . $orderBy;
89 4
				}
90
91 33
				if ($limit = $this->getLimitClause()) {
92 4
					$result .= ' ' . $limit;
93 4
				}
94
95 33
				if ($offset = $this->getOffsetClause()) {
96 2
					$result .= ' ' . $offset;
97 2
				}
98
99 33
				return $result;
100
101 12
			case self::INSERT:
102 3
				$result = $this->getInsertClause();
103
104 3
				return $result;
105
106 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...
107 4
				$result = $this->getUpdateClause();
108
109 4
				if ($where = $this->getWhereClause()) {
110 4
					$result .= ' ' . $where;
111 4
				}
112
113 4
				if ($limit = $this->getLimitClause()) {
114 1
					$result .= ' ' . $limit;
115 1
				}
116
117 4
				return $result;
118
119 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...
120 3
				$result = $this->getDeleteClause();
121
122 3
				if ($where = $this->getWhereClause()) {
123 2
					$result .= ' ' . $where;
124 2
				}
125
126 3
				if ($limit = $this->getLimitClause()) {
127 1
					$result .= ' ' . $limit;
128 1
				}
129
130 3
				return $result;
131
132 2
			default:
133 2
				return '';
134 2
		}
135
	}
136
137
	/**
138
	 * {@inheritdoc}
139
	 */
140 33
	public function select($columns = ['*'])
141
	{
142 33
		$this->columns = is_array($columns) ? $columns : func_get_args();
143 33
		$this->modifier = self::SELECT;
144
145 33
		return $this;
146
	}
147
148
	/**
149
	 * Returns the select clause.
150
	 *
151
	 * @return string the select clause.
152
	 */
153 33
	public function getSelectClause()
154
	{
155 33
		return sprintf('SELECT %s FROM %s', implode(', ', $this->columns), $this->table);
156
	}
157
158
	/**
159
	 * {@inheritdoc}
160
	 */
161 3
	public function insert(array $values)
162
	{
163 3
		$this->values = $values;
164 3
		$this->modifier = self::INSERT;
165
166 3
		return $this;
167
	}
168
169
	/**
170
	 * Returns the insert clause.
171
	 *
172
	 * @return string the insert clause.
173
	 */
174 3
	public function getInsertClause()
175
	{
176 3
		$columns = [];
177 3
		$values = [];
178
179 3
		foreach ($this->values as $key => $value) {
180 3
			$columns[] = $key;
181 3
			$values[] = sprintf('%s', $value);
182 3
		}
183
184 3
		return sprintf('INSERT INTO %s (%s) VALUES (%s)', $this->table, implode(', ', $columns), implode(', ', $values));
185
	}
186
187
	/**
188
	 * {@inheritdoc}
189
	 */
190 4
	public function update(array $values)
191
	{
192 4
		$this->values = $values;
193 4
		$this->modifier = self::UPDATE;
194
195 4
		return $this;
196
	}
197
198
	/**
199
	 * Returns the update clause.
200
	 *
201
	 * @return string the update clause.
202
	 */
203 4
	public function getUpdateClause()
204
	{
205 4
		$placeholders = [];
206
207 4
		foreach ($this->values as $key => $value) {
208 4
			$placeholders[] = sprintf('%s = %s', $key, $value);
209 4
		}
210
211 4
		return sprintf('UPDATE %s SET %s', $this->table, implode(', ', $placeholders));
212
	}
213
214
	/**
215
	 * {@inheritdoc}
216
	 */
217 3
	public function delete()
218
	{
219 3
		$this->modifier = self::DELETE;
220
221 3
		return $this;
222
	}
223
224
	/**
225
	 * Returns the delete clause.
226
	 *
227
	 * @return string the delete clause.
228
	 */
229 3
	public function getDeleteClause()
230
	{
231 3
		return sprintf('DELETE FROM %s', $this->table);
232
	}
233
234
	/**
235
	 * {@inheritdoc}
236
	 */
237 2
	public function join($table, $primary, $operator, $secondary)
238
	{
239 2
		$this->join[] = ['INNER JOIN', $table, $primary, $operator, $secondary];
240
241 2
		return $this;
242
	}
243
244
	/**
245
	 * {@inheritdoc}
246
	 */
247 2
	public function leftJoin($table, $primary, $operator, $secondary)
248
	{
249 2
		$this->join[] = ['LEFT JOIN', $table, $primary, $operator, $secondary];
250
251 2
		return $this;
252
	}
253
254
	/**
255
	 * {@inheritdoc}
256
	 */
257 2
	public function rightJoin($table, $primary, $operator, $secondary)
258
	{
259 2
		$this->join[] = ['RIGHT JOIN', $table, $primary, $operator, $secondary];
260
261 2
		return $this;
262
	}
263
264
	/**
265
	 * {@inheritdoc}
266
	 */
267 2
	public function crossJoin($table, $primary, $operator, $secondary)
268
	{
269 2
		$this->join[] = ['CROSS JOIN', $table, $primary, $operator, $secondary];
270
271 2
		return $this;
272
	}
273
274 33
	public function getJoinClause()
275
	{
276 33
		$result = [];
277
278 33
		foreach ($this->join as $key => $value) {
279 8
			$result[] = sprintf('%s %s ON %s %s %s', $value[0], $value[1], $value[2], $value[3], $value[4]);
280 33
		}
281
282 33
		return implode(' ', $result);
283
	}
284
285
	/**
286
	 * {@inheritdoc}
287
	 */
288 15
	public function where($column, $operator, $value)
289
	{
290 15
		$this->where[] = [$column, $operator, $value];
291
292 15
		return $this;
293
	}
294
295
	/**
296
	 * Returns the where clause.
297
	 *
298
	 * @return string the where clause.
299
	 */
300 40
	private function getWhereClause()
301
	{
302 40
		if (empty($this->where)) {
303 25
			return '';
304
		}
305
306 15
		$result = [];
307
308 15
		foreach ($this->where as $key => $value) {
309 15
			$result[] = $this->getWhereCondition($value[0], $value[1], $value[2]);
310 15
		}
311
312 15
		return sprintf('WHERE %s', implode(' AND ', $result));
313
	}
314
315
	/**
316
	 * Returns the where condition.
317
	 *
318
	 * @param string $column
319
	 * @param string $operator
320
	 * @param mixed $value
321
	 * @return string the where condition.
322
	 */
323 15
	private function getWhereCondition($column, $operator, $value)
324
	{
325 15
		if ($operator == 'IN') {
326 3
			return sprintf('%s IN (%s)', $column, is_array($value) ? implode(', ', $value) : $value);
327
		} else {
328 12
			return sprintf('%s %s %s', $column, $operator, $value);
329
		}
330
	}
331
332
	/**
333
	 * {@inheritdoc}
334
	 */
335 2
	public function groupBy($column)
336
	{
337 2
		$this->groupBy[] = $column;
338
339 2
		return $this;
340
	}
341
342
	/**
343
	 * Returns the group by clause.
344
	 *
345
	 * @return string the group by clause.
346
	 */
347 33
	public function getGroupByClause()
348
	{
349 33
		if (empty($this->groupBy)) {
350 31
			return '';
351
		}
352
353 2
		return sprintf('GROUP BY %s', implode(', ', $this->groupBy));
354
	}
355
356
	/**
357
	 * {@inheritdoc}
358
	 */
359 4
	public function orderBy($column, $order = null)
360
	{
361 4
		if (strcasecmp($order, 'asc') == 0) {
362 1
			$column .= ' ASC';
363 4
		} elseif(strcasecmp($order, 'desc') == 0) {
364 2
			$column .= ' DESC';
365 2
		}
366
367 4
		$this->orderBy[] = $column;
368
369 4
		return $this;
370
	}
371
372
	/**
373
	 * Returns the order by clause.
374
	 *
375
	 * @return string the order by clause.
376
	 */
377 33
	public function getOrderByClause()
378
	{
379 33
		if (empty($this->orderBy)) {
380 29
			return '';
381
		}
382
383 4
		return sprintf('ORDER BY %s', implode(', ', $this->orderBy));
384
	}
385
386
	/**
387
	 * {@inheritdoc}
388
	 */
389 6
	public function limit($limit)
390
	{
391 6
		$this->limit = $limit;
392
393 6
		return $this;
394
	}
395
396
	/**
397
	 * Returns the limit clause.
398
	 *
399
	 * @return string the limit clause.
400
	 */
401 40
	private function getLimitClause()
402
	{
403 40
		if (!$this->limit) {
404 34
			return '';
405
		}
406
407 6
		return sprintf('LIMIT %s', $this->limit);
408
	}
409
410
	/**
411
	 * {@inheritdoc}
412
	 */
413 2
	public function offset($offset)
414
	{
415 2
		$this->offset = $offset;
416
417 2
		return $this;
418
	}
419
420
	/**
421
	 * Returns the offset clause.
422
	 *
423
	 * @return string the offset clause.
424
	 */
425 33
	private function getOffsetClause()
426
	{
427 33
		if (!$this->limit || !$this->offset) {
428 31
			return '';
429
		}
430
431 2
		return sprintf('OFFSET %s', $this->offset);
432
	}
433
}
434