1
|
|
|
<?php |
2
|
|
|
namespace Kir\MySQL\Builder\Expr; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* @deprecated |
6
|
|
|
*/ |
7
|
|
|
class DBExprOrderBySpec implements OrderBySpecification { |
8
|
|
|
/** @var array<int, array{string, 'ASC'|'DESC'}> */ |
|
|
|
|
9
|
|
|
private $fields = []; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @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. |
13
|
|
|
* @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' |
|
|
|
|
14
|
|
|
*/ |
15
|
|
|
public function __construct(array $spec, array $sortFieldsSpec) { |
16
|
|
|
$expressions = []; |
17
|
|
|
foreach($spec as $specReference => $dbExpr) { |
18
|
|
|
if(is_int($specReference)) { |
19
|
|
|
$specReference = $dbExpr; |
20
|
|
|
} |
21
|
|
|
$expressions[$specReference] = $dbExpr; |
22
|
|
|
} |
23
|
|
|
foreach($sortFieldsSpec as $sortFieldSpec) { |
24
|
|
|
if(array_key_exists(0, $sortFieldSpec)) { |
25
|
|
|
if(array_key_exists($sortFieldSpec[0], $expressions)) { |
26
|
|
|
$direction = 'ASC'; |
27
|
|
|
if(array_key_exists(1, $sortFieldSpec) && strtoupper($sortFieldSpec[1]) !== 'ASC') { |
28
|
|
|
$direction = 'DESC'; |
29
|
|
|
} |
30
|
|
|
$this->fields[] = [ |
31
|
|
|
$expressions[$sortFieldSpec[0]], |
32
|
|
|
$direction |
33
|
|
|
]; |
34
|
|
|
} |
35
|
|
|
} else { // @phpstan-ignore-line |
36
|
|
|
foreach($sortFieldSpec as $alias => $direction) { |
37
|
|
|
$direction = strtoupper($direction) === 'DESC' ? 'DESC' : 'ASC'; |
38
|
|
|
if(array_key_exists($alias, $expressions)) { |
39
|
|
|
$this->fields[] = [$expressions[$alias], $direction]; |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Returns an array[], where each value is a [db-expression, sort-direction] |
48
|
|
|
* The sort-direction can be either ASC or DESC |
49
|
|
|
* |
50
|
|
|
* @return array<int, array{string, string&('ASC'|'DESC')}> |
|
|
|
|
51
|
|
|
*/ |
52
|
|
|
public function getFields(): array { |
53
|
|
|
return $this->fields; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|