Completed
Push — master ( d73f9a...cbc1cd )
by mw
23:09 queued 16:04
created

LuaAskResultProcessor::getNumericIndex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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 15
	public function __construct( QueryResult $queryResult ) {
49 15
		$this->queryResult = $queryResult;
50 15
		$this->msgTrue = explode( ',', wfMessage( 'smw_true_words' )->text() . ',true,t,yes,y' );
51 15
		$this->numericIndex = 1;
52 15
	}
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 3
	public function getQueryResultAsTable() {
63
64 3
		$result = null;
65
66 3
		if ( $this->queryResult->getCount() ) {
67
68 3
			$result = array();
69
70 3
			while ( $resultRow = $this->queryResult->getNext() ) {
71 3
				$result[] = $this->getDataFromQueryResultRow( $resultRow );
72 3
			}
73 3
		}
74
75 3
		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 4
	public function getDataFromQueryResultRow( array $resultRow ) {
89
90 4
		$rowData = array();
91
92
		/** @var ResultArray $resultArray */
93 4
		foreach ( $resultRow as $resultArray ) {
94
95
			// get key and data
96 4
			list ( $key, $data ) = $this->getDataFromResultArray( $resultArray );
97
98 4
			$rowData[$key] = $data;
99 4
		}
100
101 4
		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 5
	public function getDataFromResultArray( ResultArray $resultArray ) {
114
115
		// first, extract the key (label), if any
116 5
		$key = $this->getKeyFromPrintRequest( $resultArray->getPrintRequest() );
117
118 5
		$resultArrayData = array();
119
120
		// then get all data that is stored in this printRequest / query field
121
		/** @var DataValue $dataValue */
122 5
		while ( ( $dataValue = $resultArray->getNextDataValue() ) !== false ) {
123
124 5
			$resultArrayData[] = $this->getValueFromDataValue( $dataValue );
125 5
		}
126
127 5
		$resultArrayData = $this->extractLuaDataFromDVData( $resultArrayData );
128
129 5
		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 none found
135
	 *
136
	 * @param PrintRequest $printRequest
137
	 *
138
	 * @uses getNumericIndex
139
	 *
140
	 * @return int|string
141
	 */
142 6
	public function getKeyFromPrintRequest( PrintRequest $printRequest ) {
143
144 6
		if ( $printRequest->getLabel() !== '' ) {
145 6
			return $printRequest->getText( SMW_OUTPUT_WIKI );
146
		}
147
148 2
		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 9
	public function getValueFromDataValue( DataValue $dataValue ) {
160
161 9
		switch ( $dataValue->getTypeID() ) {
162 9
			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 8
			case '_num':
167
				// number value found
168
				/** @var \SMWNumberValue $dataValue */
169 2
				$value = $dataValue->getNumber();
170 2
				$value = ( $value == (int)$value ) ? intval( $value ) : $value;
171 2
				break;
172 7
			default:
173
				# FIXME ignores parameter link=none|subject
174 7
				$value = $dataValue->getShortText( SMW_OUTPUT_WIKI, new \Linker() );
175 9
		}
176
177 9
		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 8
	public function extractLuaDataFromDVData( $resultArrayData ) {
188
189 8
		if ( empty( $resultArrayData ) ) {
190
			// this key has no value(s). set to null
191 1
			$resultArrayData = null;
192 8
		} elseif ( count( $resultArrayData ) == 1 ) {
193
			// there was only one semantic value found. reduce the array to this value
194 6
			$resultArrayData = array_shift( $resultArrayData );
195 6
		} else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
196
			// $key has multiple values. keep the array
197
			// Note: we do not un-shift it (remember: lua array counting starts with 1), but the defer to
198
			// conversion to a lua table to a later step
199
		}
200
201 8
		return $resultArrayData;
202
	}
203
204
	/**
205
	 * Returns current numeric index (and increases it afterwards)
206
	 *
207
	 * @uses $numericIndex
208
	 *
209
	 * @return int
210
	 */
211 3
	public function getNumericIndex() {
212 3
		return $this->numericIndex++;
213
	}
214
215
}
216