Passed
Push — master ( af5b28...95be4b )
by Ron
02:37
created

OrderByBuilder::resetOrderBy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
namespace Kir\MySQL\Builder\Traits;
3
4
use Kir\MySQL\Builder\Expr\OrderBySpecification;
5
6
trait OrderByBuilder {
7
	use AbstractDB;
8
9
	/** @var array<int, array{string, string}> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<int, array{string, string}> at position 6 could not be parsed: Expected ':' at position 6, but found 'string'.
Loading history...
10
	private $orderBy = [];
11
12
	/**
13
	 * @param string|OrderBySpecification $expression
14
	 * @param string&('ASC'|'DESC') $direction
0 ignored issues
show
Documentation Bug introduced by
The doc comment string&('ASC'|'DESC') at position 3 could not be parsed: Unknown type name ''ASC'' at position 3 in string&('ASC'|'DESC').
Loading history...
15
	 * @return $this
16
	 */
17
	public function orderBy($expression, string $direction = 'ASC') {
18
		if($expression instanceof OrderBySpecification) {
19
			foreach($expression->getFields() as $field) {
20
				$this->addOrder($field[0], $field[1]);
21
			}
22
			return $this;
23
		}
24
		$this->addOrder($expression, $direction);
25
		return $this;
26
	}
27
28
	/**
29
	 * @param string $fieldName
30
	 * @param array<int, int|float|string> $values
31
	 * @return $this
32
	 */
33
	public function orderByValues(string $fieldName, array $values) {
34
		$expr = [];
35
		foreach(array_values($values) as $idx => $value) {
36
			$expr[] = $this->db()->quoteExpression("WHEN ? THEN ?", [$value, $idx]);
37
		}
38
		$this->orderBy[] = [sprintf("CASE %s\n\t\t%s\n\tEND", $this->db()->quoteField($fieldName), implode("\n\t\t", $expr)), 'ASC'];
39
		return $this;
40
	}
41
42
	/**
43
	 * @param string $fieldName
44
	 * @param array<int, int|float|string> $values
45
	 * @return
46
	 */
47
	public function getOrderBy() {
48
		return $this->orderBy;
49
	}
50
51
	/**
52
	 * Removed all order information from the current layer. This dies not affect sub-selects.
53
	 *
54
	 * @return $this
55
	 */
56
	public function resetOrderBy() {
57
		$this->orderBy = [];
58
		return $this;
59
	}
60
61
	/**
62
	 * @param string $query
63
	 * @return string
64
	 */
65
	protected function buildOrder(string $query): string {
66
		if(!count($this->orderBy)) {
67
			return $query;
68
		}
69
		$query .= "ORDER BY\n";
70
		$arr = [];
71
		foreach($this->orderBy as [$expression, $direction]) {
72
			$arr[] = sprintf("\t%s %s", $expression, strtoupper($direction));
73
		}
74
		return $query.implode(",\n", $arr)."\n";
75
	}
76
77
	/**
78
	 * @param string|array<int, mixed> $expression
79
	 * @param string&('ASC'|'DESC') $direction
0 ignored issues
show
Documentation Bug introduced by
The doc comment string&('ASC'|'DESC') at position 3 could not be parsed: Unknown type name ''ASC'' at position 3 in string&('ASC'|'DESC').
Loading history...
80
	 */
81
	private function addOrder($expression, string $direction): void {
82
		$direction = $this->fixDirection($direction);
83
		if(is_array($expression)) {
84
			if(count($expression) < 1) {
85
				return;
86
			}
87
			$expr = (string) $expression[0];
88
			$expression = $this->db()->quoteExpression($expr, array_slice($expression, 1));
89
		}
90
		$this->orderBy[] = [$expression, $direction];
91
	}
92
93
	/**
94
	 * @param string $direction
95
	 * @return string&('ASC'|'DESC')
0 ignored issues
show
Documentation Bug introduced by
The doc comment string&('ASC'|'DESC') at position 3 could not be parsed: Unknown type name ''ASC'' at position 3 in string&('ASC'|'DESC').
Loading history...
96
	 */
97
	private function fixDirection(string $direction): string {
98
		return strtoupper($direction) !== 'ASC' ? 'DESC' : 'ASC';
99
	}
100
}
101