DBExprOrderBySpec   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 12
eloc 22
c 2
b 0
f 0
dl 0
loc 47
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getFields() 0 2 1
B __construct() 0 25 11
1
<?php
2
3
namespace Kir\MySQL\Builder\Expr;
4
5
/**
6
 * @deprecated
7
 */
8
class DBExprOrderBySpec implements OrderBySpecification {
9
	/** @var array<int, array{string, 'ASC'|'DESC'}> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<int, array{string, 'ASC'|'DESC'}> at position 6 could not be parsed: Expected ':' at position 6, but found 'string'.
Loading history...
10
	private $fields = [];
11
12
	/**
13
	 * @param array<string|int, string> $spec Can be used to build a order-by-spec based on a key-value array of fields where keys represent an field alias and the value part represents an SQL-expression.
14
	 * @param array<int, array{string, 'ASC'|'DESC'}> $sortFieldsSpec Key value array where the key represents the field alias and the value is either 'ASC' or 'DESC'
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<int, array{string, 'ASC'|'DESC'}> at position 6 could not be parsed: Expected ':' at position 6, but found 'string'.
Loading history...
15
	 */
16
	public function __construct(array $spec, array $sortFieldsSpec) {
17
		$expressions = [];
18
		foreach($spec as $specReference => $dbExpr) {
19
			if(is_int($specReference)) {
20
				$specReference = $dbExpr;
21
			}
22
			$expressions[$specReference] = $dbExpr;
23
		}
24
		foreach($sortFieldsSpec as $sortFieldSpec) {
25
			if(array_key_exists(0, $sortFieldSpec)) {
26
				if(array_key_exists($sortFieldSpec[0], $expressions)) {
27
					$direction = 'ASC';
28
					if(array_key_exists(1, $sortFieldSpec) && strtoupper($sortFieldSpec[1]) !== 'ASC') {
29
						$direction = 'DESC';
30
					}
31
					$this->fields[] = [
32
						$expressions[$sortFieldSpec[0]],
33
						$direction,
34
					];
35
				}
36
			} else { // @phpstan-ignore-line
37
				foreach($sortFieldSpec as $alias => $direction) {
38
					$direction = strtoupper($direction) === 'DESC' ? 'DESC' : 'ASC';
39
					if(array_key_exists($alias, $expressions)) {
40
						$this->fields[] = [$expressions[$alias], $direction];
41
					}
42
				}
43
			}
44
		}
45
	}
46
47
	/**
48
	 * Returns an array[], where each value is a [db-expression, sort-direction]
49
	 * The sort-direction can be either ASC or DESC
50
	 *
51
	 * @return array<int, array{string, string&('ASC'|'DESC')}>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<int, array{string, string&('ASC'|'DESC')}> at position 6 could not be parsed: Expected ':' at position 6, but found 'string'.
Loading history...
52
	 */
53
	public function getFields(): array {
54
		return $this->fields;
55
	}
56
}
57