Completed
Push — master ( 01769e...5b8011 )
by Ron
03:23
created

DBExprOrderBySpec::__construct()   B

Complexity

Conditions 8
Paths 12

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 7.1428
c 0
b 0
f 0
cc 8
eloc 14
nc 12
nop 2
1
<?php
2
namespace Kir\MySQL\Builder\Expr;
3
4
class DBExprOrderBySpec implements OrderBySpecification {
5
	/** @var array[] */
6
	private $fields = [];
7
	
8
	/**
9
	 * @param $spec
10
	 * @param $sortFieldsSpec
11
	 */
12
	public function __construct($spec, $sortFieldsSpec) {
13
		$expressions = [];
14
		foreach($spec as $specReference => $dbExpr) {
15
			if(is_int($specReference)) {
16
				$specReference = $dbExpr;
17
			}
18
			$expressions[$specReference] = $dbExpr;
19
		}
20
		foreach($sortFieldsSpec as $sortFieldSpec) {
21
			if(array_key_exists(0, $sortFieldSpec) && array_key_exists($sortFieldSpec[0], $expressions)) {
22
				$direction = 'ASC';
23
				if(array_key_exists(1, $sortFieldSpec) && strtoupper($sortFieldSpec[1]) !== 'ASC') {
24
					$direction = 'DESC';
25
				}
26
				$this->fields[] = [
27
					$expressions[$sortFieldSpec[0]],
28
					$direction
29
				];
30
			}
31
		}
32
	}
33
	
34
	/**
35
	 * Returns an array[], where each value is a [db-expression, sort-direction]
36
	 * The sort-direction can be either ASC or DESC
37
	 *
38
	 * @return array[]
39
	 */
40
	public function getFields() {
41
		return $this->fields;
42
	}
43
}
44