Completed
Push — master ( 3132df...b84890 )
by mw
03:02
created

LuaAskResultProcessor::extractLuaDataFromDVData()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 15
ccs 8
cts 8
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SMW\Scribunto;
4
5
use SMWQueryResult as QueryResult;
6
use SMWResultArray as ResultArray;
7
use SMWDataValue as DataValue;
8
use SMW\Query\PrintRequest;
9
10
/**
11
 * Class LuaAskResultProcessor
12
 *
13
 * @license GNU GPL v2+
14
 * @since 1.0
15
 *
16
 * @author Tobias Oetterer
17
 *
18
 * @package SMW\Scribunto
19
 */
20
class LuaAskResultProcessor {
21
22
	/**
23
	 * Holds all possible representations of "true" in this smw instance
24
	 *
25
	 * @var array
26
	 */
27
	private $msgTrue;
28
29
	/**
30
	 * This counter serves as fallback, if no label for printout is specified
31
	 *
32
	 * @var int
33
	 */
34
	private $numericIndex;
35
36
	/**
37
	 * Holds the query result for this object
38
	 *
39
	 * @var QueryResult
40
	 */
41
	private $queryResult;
42
43
	/**
44
	 * LuaAskResultProcessor constructor.
45
	 *
46
	 * @param QueryResult $queryResult
47
	 */
48 13
	public function __construct( QueryResult $queryResult ) {
49 13
		$this->queryResult = $queryResult;
50 13
		$this->msgTrue = explode( ',', wfMessage( 'smw_true_words' )->text() . ',true,t,yes,y' );
51 13
		$this->numericIndex = 1;
52 13
	}
53
54
	/**
55
	 * Extracts the data in {@see $queryResult} and returns it as a table
56
	 * usable in lua context
57
	 *
58
	 * @uses getDataFromQueryResultRow
59
	 *
60
	 * @return array|null
61
	 */
62 1
	public function getQueryResultAsTable() {
63
64 1
		$result = null;
65
66 1
		if ( $this->queryResult->getCount() ) {
67
68 1
			$result = array();
69
70 1
			while ( $resultRow = $this->queryResult->getNext() ) {
71 1
				$result[] = $this->getDataFromQueryResultRow( $resultRow );
72 1
			}
73 1
		}
74
75 1
		return $result;
76
	}
77
78
	/**
79
	 * Extracts the data of a single result row in the {@see $queryResult}
80
	 * and returns it as a table usable in lua context
81
	 *
82
	 * @param array $resultRow result row as an array of {@see ResultArray} objects
83
	 *
84
	 * @uses getDataFromResultArray
85
	 *
86
	 * @return array
87
	 */
88 2
	public function getDataFromQueryResultRow( array $resultRow ) {
89
90 2
		$rowData = array();
91
92
		/** @var ResultArray $resultArray */
93 2
		foreach ( $resultRow as $resultArray ) {
94
95
			// get key and data
96 2
			list ( $key, $data ) = $this->getDataFromResultArray( $resultArray );
97
98 2
			$rowData[$key] = $data;
99 2
		}
100
101 2
		return $rowData;
102
	}
103
104
	/**
105
	 * Extracts the data of a single printRequest / query field
106
	 *
107
	 * @param ResultArray $resultArray
108
	 *
109
	 * @uses getKeyFromPrintRequest, getValueFromDataValue, extractLuaDataFromDVData
110
	 *
111
	 * @return array array ( int|string, array )
112
	 */
113 3
	public function getDataFromResultArray( ResultArray $resultArray ) {
114
115
		// first, extract the key (label), if any
116 3
		$key = $this->getKeyFromPrintRequest( $resultArray->getPrintRequest() );
117
118 3
		$resultArrayData = array();
119
120
		// then get all data that is stored in this printRequest / query field
121
		/** @var DataValue $dataValue */
122 3
		while ( ( $dataValue = $resultArray->getNextDataValue() ) !== false ) {
123
124 3
			$resultArrayData[] = $this->getValueFromDataValue( $dataValue );
125 3
		}
126
127 3
		$resultArrayData = $this->extractLuaDataFromDVData( $resultArrayData );
128
129 3
		return array( $key, $resultArrayData );
130
	}
131
132
	/**
133
	 * Takes an smw query print request and tries to retrieve the label
134
	 * falls back to [@see getNumericIndex} if non found
135
	 *
136
	 * @param PrintRequest $printRequest
137
	 *
138
	 * @uses getNumericIndex
139
	 *
140
	 * @return int|string
141
	 */
142 4
	public function getKeyFromPrintRequest( PrintRequest $printRequest ) {
143
144 4
		if ( $printRequest->getLabel() !== '' ) {
145 4
			return $printRequest->getText( SMW_OUTPUT_WIKI );
146
		}
147
148 1
		return $this->getNumericIndex();
149
	}
150
151
	/**
152
	 * Extracts the data of a single value of the current printRequest / query field
153
	 * The value is formatted according to the type of the property
154
	 *
155
	 * @param DataValue $dataValue
156
	 *
157
	 * @return mixed
158
	 */
159 7
	public function getValueFromDataValue( DataValue $dataValue ) {
160
161 7
		switch ( $dataValue->getTypeID() ) {
162 7
			case '_boo':
163
				// boolean value found, convert it
164 1
				$value = in_array( strtolower( $dataValue->getShortText( SMW_OUTPUT_WIKI ) ), $this->msgTrue );
165 1
				break;
166 6
			case '_num':
167
				// number value found
168
				/** @var \SMWNumberValue $dataValue */
169 1
				$value = $dataValue->getNumber();
170 1
				$value = ( $value == (int)$value ) ? intval( $value ) : $value;
171 1
				break;
172 5
			default:
173
				# FIXME ignores parameter link=none|subject
174 5
				$value = $dataValue->getShortText( SMW_OUTPUT_WIKI, new \Linker() );
175 7
		}
176
177 7
		return $value;
178
	}
179
180
	/**
181
	 * Takes an array of data fields and returns either null, a single value or a lua table.
182
	 *
183
	 * @param array $resultArrayData
184
	 *
185
	 * @return mixed
186
	 */
187 6
	public function extractLuaDataFromDVData( $resultArrayData ) {
188
189 6
		if ( empty( $resultArrayData ) ) {
190
			// this key has no value(s). set to null
191 1
			$resultArrayData = null;
192 6
		} elseif ( count( $resultArrayData ) == 1 ) {
193
			// there was only one semantic value found. reduce the array to this value
194 4
			$resultArrayData = array_shift( $resultArrayData );
195 4
		} else {
196
			// $key has multiple values. keep the array, but un-shift it (remember: lua array counting starts with 1)
197 1
			array_unshift( $resultArrayData, null );
198
		}
199
200 6
		return $resultArrayData;
201
	}
202
203
	/**
204
	 * Returns current numeric index (and increases it afterwards)
205
	 *
206
	 * @uses $numericIndex
207
	 *
208
	 * @return int
209
	 */
210 2
	public function getNumericIndex() {
211 2
		return $this->numericIndex++;
212
	}
213
214
}
215