GroupByBuilder   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 18
dl 0
loc 48
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A quoteExpr() 0 2 1
A groupBy() 0 12 4
A buildGroups() 0 11 3
1
<?php
2
3
namespace Kir\MySQL\Builder\Traits;
4
5
use Kir\MySQL\Builder;
6
7
trait GroupByBuilder {
8
	use AbstractDB;
9
10
	/** @var string[] */
11
	private array $groupBy = [];
12
13
	/**
14
	 * @param mixed ...$args
15
	 * @return $this
16
	 */
17
	public function groupBy(...$args) {
18
		foreach($args as $expression) {
19
			if(is_array($expression)) {
20
				if(!count($expression)) {
21
					continue;
22
				}
23
				$expression = $this->quoteExpr($expression[0], array_slice($expression, 1));
24
			}
25
			$this->groupBy[] = $expression;
26
		}
27
28
		return $this;
29
	}
30
31
	/**
32
	 * @param string $query
33
	 * @return string
34
	 */
35
	protected function buildGroups(string $query): string {
36
		if(!count($this->groupBy)) {
37
			return $query;
38
		}
39
		$query .= "GROUP BY\n";
40
		$arr = [];
41
		foreach($this->groupBy as $expression) {
42
			$arr[] = "\t{$expression}";
43
		}
44
45
		return $query . implode(",\n", $arr) . "\n";
46
	}
47
48
	/**
49
	 * @param string $expression
50
	 * @param array<int, null|int|float|string|array<int, string>|Builder\DBExpr|Builder\Select> $arguments
51
	 * @return string
52
	 */
53
	protected function quoteExpr(string $expression, array $arguments): string {
54
		return $this->db()->quoteExpression($expression, $arguments);
55
	}
56
}
57