Completed
Push — master ( ea4e13...3e4ffd )
by
unknown
21:07 queued 13s
created

src/ByHttpRequest/CannedResultArray.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace SEQL\ByHttpRequest;
4
5
use SMW\DataValueFactory;
6
use SMW\DIProperty;
7
use SMW\DIWikiPage;
8
use SMW\Localizer;
9
use SMW\Query\PrintRequest;
10
use SMWDataValue as DataValue;
11
use SMWResultArray as ResultArray;
12
13
/**
14
 * @license GNU GPL v2+
15
 * @since 1.0
16
 *
17
 * @author mwjames
18
 */
19
class CannedResultArray extends ResultArray {
20
21
	/**
22
	 * @var PrintRequest
23
	 */
24
	private $mPrintRequest;
25
26
	/**
27
	 * @var DIWikiPage
28
	 */
29
	private $mResult;
30
31
	/**
32
	 * @var JsonResponseParser
33
	 */
34
	private $jsonResponseParser;
35
36
	/**
37
	 * @var DataValue[]|false
38
	 */
39
	private $mContent;
40
41
	/**
42
	 * @since 1.0
43
	 *
44
	 * @param DIWikiPage $resultPage
45
	 * @param PrintRequest $printRequest
46
	 * @param JsonResponseParser $jsonResponseParser
47
	 */
48 7
	public function __construct( DIWikiPage $resultPage, PrintRequest $printRequest, JsonResponseParser $jsonResponseParser ) {
49 7
		$this->mResult = $resultPage;
50 7
		$this->mPrintRequest = $printRequest;
51 7
		$this->jsonResponseParser = $jsonResponseParser;
52 7
		$this->mContent = false;
53 7
	}
54
55
	/**
56
	 * @see ResultArray::getResultSubject
57
	 *
58
	 * @return DIWikiPage
59
	 */
60 1
	public function getResultSubject() {
61 1
		return $this->mResult;
62
	}
63
64
	/**
65
	 * @see ResultArray::getContent
66
	 *
67
	 * @return SMWDataItem[]|false
68
	 */
69 4
	public function getContent() {
70 4
		$this->loadContent();
71
72 4
		if ( !$this->mContent ) {
73
			return $this->mContent;
74
		}
75
76 4
		$content = array();
77
78 4
		foreach ( $this->mContent as $value ) {
79 4
			$content[] = $value instanceof DataValue ? $value->getDataItem() : $value;
80 4
		}
81
82 4
		return $content;
83
	}
84
85
	/**
86
	 * @see ResultArray::getPrintRequest
87
	 *
88
	 * @return PrintRequest
89
	 */
90
	public function getPrintRequest() {
91
		return $this->mPrintRequest;
92
	}
93
94
	/**
95
	 * @see ResultArray::getNextDataItem
96
	 *
97
	 * @since 1.6
98
	 *
99
	 * @return SMWDataItem|false
100
	 */
101 4
	public function getNextDataItem() {
102
103 4
		$this->loadContent();
104 4
		$result = current( $this->mContent );
105 4
		next( $this->mContent );
106
107 4
		return $result instanceof DataValue ? $result->getDataItem() : $result;
108
	}
109
110
	/**
111
	 * @see ResultArray::reset
112
	 *
113
	 * @since 1.7.1
114
	 *
115
	 * @return SMWDataItem|false
116
	 */
117 4
	public function reset() {
118
119 4
		$this->loadContent();
120 4
		$result = reset( $this->mContent );
121
122 4
		return $result instanceof DataValue ? $result->getDataItem() : $result;
123
	}
124
125
	/**
126
	 * @see ResultArray::getNextDataValue
127
	 *
128
	 * @since 1.6
129
	 *
130
	 * @return SMWDataValue|false
131
	 */
132 5
	public function getNextDataValue() {
133
134 5
		$this->loadContent();
135 5
		$content = current( $this->mContent );
136 5
		next( $this->mContent );
137
138 5
		if ( $content === false ) {
139
			return false;
140
		}
141
142 5
		$property = $this->getMatchablePropertyFromPrintRequest();
143
144
		// Units of measurement can not be assumed to be declared on a wiki
145
		// therefore don't try to recreate a DataValue and use the DV created
146
		// from the raw API response
147 5
		if ( $this->mPrintRequest->getMode() === PrintRequest::PRINT_PROP &&
148 5
		    $property->findPropertyTypeId() === '_qty' ) {
149 1
			return $content;
150
		}
151
152 4
		if ( $this->mPrintRequest->getMode() === PrintRequest::PRINT_PROP &&
153 4
		    strpos( $property->findPropertyTypeId(), '_rec' ) !== false ) {
154
155
			if ( $this->mPrintRequest->getParameter( 'index' ) === false ) {
156
				return $content;
157
			}
158
159
			$pos = $this->mPrintRequest->getParameter( 'index' ) - 1;
160
			$dataItems = $content->getDataItems();
161
162
			if ( !array_key_exists( $pos, $dataItems ) ) {
163
				return $content;
164
			}
165
166
			$diProperties = $content->getPropertyDataItems();
167
			$content = $dataItems[$pos];
168
169
			if ( array_key_exists( $pos, $diProperties ) &&
170
				!is_null( $diProperties[$pos] ) ) {
171
				$diProperty = $diProperties[$pos];
172
			} else {
173
				$diProperty = null;
174
			}
175
176 4
		} elseif ( $this->mPrintRequest->getMode() == PrintRequest::PRINT_PROP ) {
177 1
			$diProperty = $property;
178 1
		} else {
179 3
			$diProperty = null;
180
		}
181
182 4
		if ( $content instanceof DataValue ) {
183 1
			$content = $content->getDataItem();
184 1
		}
185
186 4
		$dataValue = DataValueFactory::getInstance()->newDataItemValue( $content, $diProperty );
187
188
		// Allow the DV formatter to access a specific language code
189 4
		$dataValue->setOption(
190 4
			'content.language',
191 4
			Localizer::getInstance()->getPreferredContentLanguage( $this->mResult )->getCode()
192 4
		);
193
194 4
		$dataValue->setOption(
195 4
			'user.language',
196 4
			Localizer::getInstance()->getUserLanguage()->getCode()
197 4
		);
198
199 4
		$dataValue->setContextPage(
200 4
			$this->mResult
0 ignored issues
show
$this->mResult is of type object<SMW\DIWikiPage>, but the function expects a null|object<SMWDIWikiPage>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
201 4
		);
202
203 4
		if ( $this->mPrintRequest->getOutputFormat() ) {
204
			$dataValue->setOutputFormat( $this->mPrintRequest->getOutputFormat() );
205
		}
206
207 4
		return $dataValue;
208
	}
209
210
	/**
211
	 * Load results of the given print request and result subject. This is only
212
	 * done when needed.
213
	 */
214 5
	protected function loadContent() {
215 5
		if ( $this->mContent !== false ) {
216 4
			return;
217
		}
218
219 5
		$this->mContent = array();
220
221 5
		switch ( $this->mPrintRequest->getMode() ) {
222 5
			case PrintRequest::PRINT_THIS:
223 2
				$this->mContent = array( $this->mResult );
224 2
			break;
225 3
			case PrintRequest::PRINT_CCAT:
226 3
			case PrintRequest::PRINT_CATS:
227
228 1
				$this->mContent = $this->jsonResponseParser->getPropertyValuesFor(
229 1
					$this->mResult,
230 1
					new DIProperty( '_INST' )
231 1
				);
232
233 1
			break;
234 2
			case PrintRequest::PRINT_PROP:
235 2
				$propertyValue = $this->mPrintRequest->getData();
236
237 2
				if ( $propertyValue->isValid() ) {
238 2
					$this->mContent = $this->jsonResponseParser->getPropertyValuesFor(
239 2
						$this->mResult,
240 2
						$this->getMatchablePropertyFromPrintRequest()
241 2
					);
242 2
				}
243
244 2
			break;
245
			default:
246
				$this->mContent = array(); // Unknown print request.
247 5
		}
248
249 5
		reset( $this->mContent );
250 5
	}
251
252 5
	private function getMatchablePropertyFromPrintRequest() {
253
254 5
		if ( $this->mPrintRequest->getMode() !== PrintRequest::PRINT_PROP ) {
255 3
			return null;
256
		}
257
258 2
		$property = $this->mPrintRequest->getData()->getDataItem();
259
260
		// The API may not deploy the natural property key (until 0.8+) hence something
261
		// like |?Has population=Population (in K) does not return a valid result from
262
		// the parser because "Has population" cannot be connected to "Population (in K)"
263
		// without the extra "key" field therefore construct a new property to match the
264
		// label
265 2
		if ( $this->mPrintRequest->getLabel() !== '' && $this->mPrintRequest->getLabel() !== $property->getLabel() ) {
266 1
			return $this->jsonResponseParser->findPropertyFromInMemoryExternalRepositoryCache(
267 1
				DIProperty::newFromUserLabel( $this->mPrintRequest->getLabel() )
268 1
			);
269
		}
270
271 1
		return $this->jsonResponseParser->findPropertyFromInMemoryExternalRepositoryCache(
272
			$property
273 1
		);
274
	}
275
276
}
277