Completed
Push — master ( ff2cd3...da92b9 )
by mw
236:43 queued 201:44
created

ParametersProfileAnnotator::doSerializeSortKeys()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
nc 3
nop 1
dl 0
loc 16
rs 9.4285
c 1
b 0
f 0
1
<?php
2
3
namespace SMW\Query\ProfileAnnotators;
4
5
use SMW\DIProperty;
6
use SMW\Query\Language\Description;
7
use SMW\Query\ProfileAnnotator;
8
use SMWDIBlob as DIBlob;
9
use SMWQuery as Query;
10
11
/**
12
 * @license GNU GPL v2+
13
 * @since 2.5
14
 *
15
 * @author mwjames
16
 */
17
class ParametersProfileAnnotator extends ProfileAnnotatorDecorator {
18
19
	/**
20
	 * @var Query
21
	 */
22
	private $query;
23
24
	/**
25
	 * @since 2.5
26
	 *
27
	 * @param ProfileAnnotator $profileAnnotator
28
	 * @param Query $query
29
	 */
30
	public function __construct( ProfileAnnotator $profileAnnotator, Query $query ) {
31
		parent::__construct( $profileAnnotator );
32
		$this->query = $query;
33
	}
34
35
	/**
36
	 * ProfileAnnotatorDecorator::addPropertyValues
37
	 */
38
	protected function addPropertyValues() {
39
40
		list( $sort, $order ) = $this->doSerializeSortKeys( $this->query );
41
42
		$options = array(
43
			'limit'  => $this->query->getLimit(),
44
			'offset' => $this->query->getOffset(),
45
			'sort'   => $sort,
46
			'order'  => $order,
47
			'mode'   => $this->query->getQueryMode()
48
		);
49
50
		$this->getSemanticData()->addPropertyObjectValue(
51
			new DIProperty( '_ASKPA' ),
52
			new DIBlob( json_encode( $options ) )
53
		);
54
	}
55
56
	private function doSerializeSortKeys( $query ) {
57
58
		$sort = array();
59
		$order = array();
60
61
		if ( $query->getSortKeys() === null ) {
62
			return array( $sort, $order );
63
		}
64
65
		foreach ( $query->getSortKeys() as $key => $value ) {
66
			$sort[] = $key;
67
			$order[] = strtolower( $value );
68
		}
69
70
		return array( $sort, $order );
71
	}
72
73
}
74