Completed
Push — master ( dd8aad...6526e1 )
by mw
04:06
created

JsonResponseParser::isPredefinedPropertyByKey()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 9.2
cc 4
eloc 2
nc 4
nop 2
crap 4
1
<?php
2
3
namespace SEQL\ByAskApiHttpRequest;
4
5
use SEQL\DataValueDeserializer;
6
use SMW\DIProperty;
7
use SMW\DIWikiPage;
8
9
/**
10
 * @license GNU GPL v2+
11
 * @since 1.0
12
 *
13
 * @author mwjames
14
 */
15
class JsonResponseParser {
16
17
	/**
18
	 * @var DataValueDeserializer
19
	 */
20
	private $dataValueDeserializer;
21
22
	/**
23
	 * @var array
24
	 */
25
	private $subjectList = array();
26
27
	/**
28
	 * @var array
29
	 */
30
	private $printRequestPropertyList = array();
31
32
	/**
33
	 * @var boolean
34
	 */
35
	private $furtherResults = false;
36
37
	/**
38
	 * @var array
39
	 */
40
	private $printouts = array();
41
42
	/**
43
	 * @var array
44
	 */
45
	private $internalLabelToKeyMap = array();
46
47
	/**
48
	 * @var string
49
	 */
50
	private $rawResponseResult = array();
51
52
	/**
53
	 * @since 1.0
54
	 *
55
	 * @param DataValueDeserializer $dataValueDeserializer
56
	 */
57 9
	public function __construct( DataValueDeserializer $dataValueDeserializer ) {
58 9
		$this->dataValueDeserializer = $dataValueDeserializer;
59 9
	}
60
61
	/**
62
	 * @since 1.0
63
	 *
64
	 * @param DIProperty $property
65
	 *
66
	 * @return DIProperty
67
	 */
68 1
	public function findPropertyFromInMemoryExternalRepositoryCache( DIProperty $property ) {
69 1
		return isset( $this->printRequestPropertyList[$property->getKey()] ) ? $this->printRequestPropertyList[$property->getKey()] : $property;
70
	}
71
72
	/**
73
	 * @since 1.0
74
	 *
75
	 * @param DIWikiPage[]
76
	 */
77 5
	public function getResultSubjectList() {
78 5
		return $this->subjectList;
79
	}
80
81
	/**
82
	 * @since 1.0
83
	 *
84
	 * @param []
85
	 */
86
	public function getPrintouts() {
87
		return $this->printouts;
88
	}
89
90
	/**
91
	 * @since 1.0
92
	 *
93
	 * @param []
94
	 */
95
	public function getPrintRequestPropertyList() {
96
		return $this->printRequestPropertyList;
97
	}
98
99
	/**
100
	 * @since 1.0
101
	 *
102
	 * @return boolean
103
	 */
104 8
	public function hasFurtherResults() {
105 8
		return $this->furtherResults;
106
	}
107
108
	/**
109
	 * @since 1.0
110
	 *
111
	 * @return array
112
	 */
113 3
	public function getRawResponseResult() {
114 3
		return $this->rawResponseResult;
115
	}
116
117
	/**
118
	 * @since 1.0
119
	 *
120
	 * @param DIWikiPage $subject
121
	 * @param DIProperty $property
122
	 *
123
	 * @return array
124
	 */
125
	public function getPropertyValuesFor( DIWikiPage $subject, DIProperty $property ) {
126
127
		$key  = $property->getKey();
128
		$hash = $subject->getHash();
129
130
		if ( isset( $this->internalLabelToKeyMap[$property->getKey()] ) ) {
131
			$key = $this->internalLabelToKeyMap[$property->getKey()];
132
		}
133
134
		return isset( $this->printouts[$hash][$key] ) ? $this->printouts[$hash][$key] : array();
135
	}
136
137
	/**
138
	 * @since 1.0
139
	 *
140
	 * @param array $result
141
	 */
142 8
	public function doParse( array $result ) {
143
144 8
		if ( isset( $result['query'] ) ) {
145 8
			$this->rawResponseResult = $result['query'] ;
146 8
		}
147
148 8
		foreach ( $result as $key => $item ) {
149
150 8
			if ( $key === 'query-continue-offset' ) {
151 4
				$this->furtherResults = true;
152 4
				continue;
153
			}
154
155 8
			if ( !isset( $item['printrequests'] ) || !isset( $item['results'] ) ) {
156 3
				continue;
157
			}
158
159 5
			foreach ( $item['printrequests'] as $k => $value ) {
160 5
				$this->addPrintRequestToPropertyList( $value );
161 5
			}
162
163 5
			foreach ( $item['results'] as $k => $value ) {
164 5
				$this->addResultsToPrintoutList( $value );
165 5
			}
166 8
		}
167 8
	}
168
169 5
	private function addResultsToPrintoutList( $value ) {
170
171 5
		$subject = $this->dataValueDeserializer->newDiWikiPage( $value );
172
173 5
		$hash = $subject->getHash();
174 5
		$this->subjectList[] = $subject;
175
176 5
		if ( !isset( $value['printouts'] ) ) {
177
			return;
178
		}
179
180 5
		foreach ( $value['printouts'] as $pk => $pvalues ) {
181
182 5
			$property = DIProperty::newFromUserLabel( $pk );
183 5
			$pk = $property->getKey();
184
185
			// Need to match the property to its possible internal
186
			// representation (_INST etc.()
187 5
			if ( isset( $this->internalLabelToKeyMap[$pk] ) ) {
188 1
				$pk = $this->internalLabelToKeyMap[$pk];
189 1
			}
190
191 5
			if ( !isset( $this->printRequestPropertyList[$pk] ) ) {
192
				continue;
193
			}
194
195 5
			$property = $this->printRequestPropertyList[$pk];
196
197 5
			foreach ( $pvalues as $pvalue ) {
198
199 5
				if ( !isset( $this->printouts[$hash][$pk] ) ) {
200 5
					$this->printouts[$hash][$pk] = array();
201 5
				}
202
203
				// Unique row value display
204 5
				$vhash = md5( json_encode( $pvalue ) );
205
206 5
				if ( !isset( $this->printouts[$hash][$pk][$vhash] ) ) {
207 5
					$this->printouts[$hash][$pk][$vhash] = $this->dataValueDeserializer->newDataValueFrom( $property, $pvalue );
208 5
				}
209 5
			}
210 5
		}
211 5
	}
212
213 5
	private function addPrintRequestToPropertyList( $value ) {
214
215 5
		if ( $value['label'] === '' ) {
216 5
			return;
217
		}
218
219 5
		if ( $value['mode'] == 0 ) {
220 1
			$property = new DIProperty( '_INST' );
221 1
			$this->internalLabelToKeyMap[$value['label']] = $property->getKey();
222 1
		} else {
223 4
			$property = DIProperty::newFromUserLabel( $value['label'] );
224 4
			$property->setPropertyTypeId( $value['typeid'] );
225
		}
226
227 5
		$property->setInterwiki( $this->dataValueDeserializer->getQuerySource() );
228 5
		$this->printRequestPropertyList[$property->getKey()] = $property;
229 5
	}
230
231
}
232