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

WhereBuilder   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
eloc 15
c 2
b 0
f 0
dl 0
loc 35
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B where() 0 16 7
A buildWhereConditions() 0 2 1
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