LuaAskResultProcessor::extractLuaDataFromDVData()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 6
cts 6
cp 1
rs 9.7666
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
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 16
	public function __construct( QueryResult $queryResult ) {
49 16
		$this->queryResult = $queryResult;
50 16
		$this->msgTrue = explode( ',', wfMessage( 'smw_true_words' )->text() . ',true,t,yes,y' );
51 16
		$this->numericIndex = 1;
52 16
	}
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 4
	public function getProcessedResult() {
63
64 4
		$result = null;
65
66 4
		if ( $this->queryResult->getCount() ) {
67
68 4
			$result = [];
69
70 4
			while ( $resultRow = $this->queryResult->getNext() ) {
71 4
				$result[] = $this->getDataFromQueryResultRow( $resultRow );
72
			}
73
		}
74
75 4
		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 5
	public function getDataFromQueryResultRow( array $resultRow ) {
89
90 5
		$rowData = [];
91
92
		/** @var ResultArray $resultArray */
93 5
		foreach ( $resultRow as $resultArray ) {
94
95
			// get key and data
96 5
			list ( $key, $data ) = $this->getDataFromResultArray( $resultArray );
97
98 5
			$rowData[$key] = $data;
99
		}
100 5
		return $rowData;
101
	}
102
103
	/**
104
	 * Extracts the data of a single printRequest / query field
105
	 *
106
	 * @param ResultArray $resultArray
107
	 *
108
	 * @uses getKeyFromPrintRequest, getValueFromDataValue, extractLuaDataFromDVData
109
	 *
110
	 * @return array array ( int|string, array )
111
	 */
112 6
	public function getDataFromResultArray( ResultArray $resultArray ) {
113
114
		// first, extract the key (label), if any
115 6
		$key = $this->getKeyFromPrintRequest( $resultArray->getPrintRequest() );
116
117 6
		$resultArrayData = [];
118
119
		// then get all data that is stored in this printRequest / query field
120
		/** @var DataValue $dataValue */
121 6
		while ( ( $dataValue = $resultArray->getNextDataValue() ) !== false ) {
122
123 6
			$resultArrayData[] = $this->getValueFromDataValue( $dataValue );
124
		}
125
126 6
		$resultArrayData = $this->extractLuaDataFromDVData( $resultArrayData );
127
128 6
		return [ $key, $resultArrayData ];
129
	}
130
131
	/**
132
	 * Takes an smw query print request and tries to retrieve the label
133
	 * falls back to {@see getNumericIndex} if none was found
134
	 *
135
	 * @param PrintRequest $printRequest
136
	 *
137
	 * @uses getNumericIndex
138
	 *
139
	 * @return int|string
140
	 */
141 7
	public function getKeyFromPrintRequest( PrintRequest $printRequest ) {
142
143 7
		if ( $printRequest->getLabel() !== '' ) {
144 7
			return $printRequest->getText( SMW_OUTPUT_WIKI );
145
		}
146
147 1
		return $this->getNumericIndex();
148
	}
149
150
	/**
151
	 * Extracts the data of a single value of the current printRequest / query field
152
	 * The value is formatted according to the type of the property
153
	 *
154
	 * @param DataValue $dataValue
155
	 *
156
	 * @return mixed
157
	 */
158 10
	public function getValueFromDataValue( DataValue $dataValue ) {
159
160 10
		switch ( $dataValue->getTypeID() ) {
161 10
			case '_boo':
162
				// boolean value found, convert it
163 3
				$value = in_array( strtolower( $dataValue->getWikiValue() ), $this->msgTrue );
164 3
				break;
165 9
			case '_num':
166
				// number value found
167
				/** @var \SMWNumberValue $dataValue */
168 2
				$value = $dataValue->getNumber();
169 2
				$value = ( $value == (int)$value ) ? intval( $value ) : $value;
170 2
				break;
171
			default:
172
				# FIXME ignores parameter link=none|subject
173 8
				$value = $dataValue->getShortText( SMW_OUTPUT_WIKI, new \Linker() );
174
		}
175
176 10
		return $value;
177
	}
178
179
	/**
180
	 * Takes an array of data fields and returns either null, a single value or a lua table.
181
	 *
182
	 * @param array $resultArrayData
183
	 *
184
	 * @return mixed
185
	 */
186 9
	public function extractLuaDataFromDVData( $resultArrayData ) {
187
188 9
		if ( empty( $resultArrayData ) ) {
189
			// this key has no value(s). set to null
190 1
			return null;
191 8
		} elseif ( count( $resultArrayData ) == 1 ) {
192
			// there was only one semantic value found. reduce the array to this value
193 7
			return array_shift( $resultArrayData );
194
		} else {
195
			// $key has multiple values. keep the array
196
			// Note: we do not un-shift it (remember: lua array counting starts with 1), but we defer
197
			// conversion into a lua table to a later step
198 1
			return $resultArrayData;
199
		}
200
	}
201
202
	/**
203
	 * Returns current numeric index (and increases it afterwards)
204
	 *
205
	 * @uses $numericIndex
206
	 *
207
	 * @return int
208
	 */
209 2
	public function getNumericIndex() {
210 2
		return $this->numericIndex++;
211
	}
212
}
213