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

HavingBuilder::having()   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\Expr\OptionalExpression;
5
use Kir\MySQL\Builder\Internal\ConditionBuilder;
6
7
trait HavingBuilder {
8
	use AbstractDB;
9
10
	/** @var array */
11
	private $having = [];
12
13
	/**
14
	 * @param string|array|OptionalExpression $expression
15
	 * @param array<int, mixed> $args
16
	 * @return $this
17
	 */
18
	public function having($expression, ...$args) {
19
		if($expression instanceof OptionalExpression) {
20
			if($expression->isValid()) {
21
				$this->having[] = [$expression->getExpression(), $expression->getValue()];
22
			}
23
		} elseif(is_array($expression) || is_object($expression)) {
24
			if(is_object($expression)) {
25
				$expression = (array) $expression;
26
			}
27
			if(count($expression) > 0) {
28
				$this->having[] = [$expression, array_slice(func_get_args(), 1)];
29
			}
30
		} else {
31
			$this->having[] = [$expression, array_slice(func_get_args(), 1)];
32
		}
33
		return $this;
34
	}
35
36
	/**
37
	 * @param string $query
38
	 * @return string
39
	 */
40
	protected function buildHavingConditions($query) {
41
		return ConditionBuilder::build($this->db(), $query, $this->having, 'HAVING');
42
	}
43
}
44