Completed
Push — master ( 80645f...ecbf0c )
by Ron
03:07
created

WhereBuilder::where()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 16
rs 8.8333
cc 7
nc 7
nop 2
1
<?php
2
namespace Kir\MySQL\Builder\Traits;
3
4
use Kir\MySQL\Builder\DBExpr;
5
use Kir\MySQL\Builder\Expr\OptionalExpression;
6
use Kir\MySQL\Builder\Internal\ConditionBuilder;
7
8
trait WhereBuilder {
9
	use AbstractDB;
10
11
	/** @var array<int, mixed> */
12
	private $where = [];
13
14
	/**
15
	 * @param string|array|OptionalExpression $expression
16
	 * @param array<int, mixed> $args
17
	 * @return $this
18
	 */
19
	public function where($expression, ...$args) {
20
		if($expression instanceof OptionalExpression) {
21
			if($expression->isValid()) {
22
				$this->where[] = [$expression->getExpression(), $expression->getValue()];
23
			}
24
		} elseif(is_array($expression) || is_object($expression)) {
25
			if(is_object($expression)) {
0 ignored issues
show
introduced by
The condition is_object($expression) is always false.
Loading history...
26
				$expression = (array) $expression;
27
			}
28
			if(count($expression) > 0) {
29
				$this->where[] = [$expression, $args];
30
			}
31
		} else {
32
			$this->where[] = [$expression, $args];
33
		}
34
		return $this;
35
	}
36
37
	/**
38
	 * @param string $query
39
	 * @return string
40
	 */
41
	protected function buildWhereConditions($query) {
42
		return ConditionBuilder::build($this->db(), $query, $this->where, 'WHERE');
43
	}
44
}
45