# Source for file Response.php

Documentation is available at Response.php

1. <?php
2. /**
3. * Copyright (c) 2007-2009, Conduit Internet Technologies, Inc.
4. * All rights reserved.
5. *
6. * Redistribution and use in source and binary forms, with or without
7. * modification, are permitted provided that the following conditions are met:
8. *
9. * - Redistributions of source code must retain the above copyright notice,
10. * this list of conditions and the following disclaimer.
11. * - Redistributions in binary form must reproduce the above copyright
12. * notice, this list of conditions and the following disclaimer in the
13. * documentation and/or other materials provided with the distribution.
14. * - Neither the name of Conduit Internet Technologies, Inc. nor the names of
15. * its contributors may be used to endorse or promote products derived from
16. * this software without specific prior written permission.
17. *
18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28. * POSSIBILITY OF SUCH DAMAGE.
29. *
30. * @copyright Copyright 2007-2009 Conduit Internet Technologies, Inc. (http://conduit-it.com)
31. * @license New BSD (http://solr-php-client.googlecode.com/svn/trunk/COPYING)
32. * @version $Id: Response.php 19 2009-08-12 14:08:42Z donovan.jimenez $
33. *
34. * @package Apache
35. * @subpackage Solr
36. * @author Donovan Jimenez <djimenez@conduit-it.com>
37. */
39. /**
40. * Represents a Solr response. Parses the raw response into a set of stdClass objects
41. * and associative arrays for easy access.
42. *
43. * Currently requires json_decode which is bundled with PHP >= 5.2.0, Alternatively can be
44. * installed with PECL. Zend Framework also includes a purely PHP solution.
45. */
46. class Apache_Solr_Response
47. {
48. /**
49. * SVN Revision meta data for this class
50. */
51. const SVN_REVISION = '$Revision: 19 $';
53. /**
54. * SVN ID meta data for this class
55. */
56. const SVN_ID = '$Id: Response.php 19 2009-08-12 14:08:42Z donovan.jimenez $';
58. /**
59. * Holds the raw response used in construction
60. *
61. * @var string
62. */
63. protected $_rawResponse;
65. /**
66. * Parsed values from the passed in http headers
67. *
68. * @var string
69. */
70. protected $_httpStatus, $_httpStatusMessage, $_type, $_encoding;
72. /**
73. * Whether the raw response has been parsed
74. *
75. * @var boolean
76. */
77. protected $_isParsed = false;
79. /**
80. * Parsed representation of the data
81. *
82. * @var mixed
83. */
84. protected $_parsedData;
86. /**
87. * Data parsing flags. Determines what extra processing should be done
88. * after the data is initially converted to a data structure.
89. *
90. * @var boolean
91. */
92. protected $_createDocuments = true,
93. $_collapseSingleValueArrays = true;
95. /**
96. * Constructor. Takes the raw HTTP response body and the exploded HTTP headers
97. *
98. * @param string $rawResponse
99. * @param array $httpHeaders
100. * @param boolean $createDocuments Whether to convert the documents json_decoded as stdClass instances to Apache_Solr_Document instances
101. * @param boolean $collapseSingleValueArrays Whether to make multivalued fields appear as single values
102. */
103. public function __construct($rawResponse, $httpHeaders = array(), $createDocuments = true, $collapseSingleValueArrays = true)
104. {
105. //Assume 0, 'Communication Error', utf-8, and text/plain
106. $status = 0;
107. $statusMessage = 'Communication Error';
108. $type = 'text/plain';
109. $encoding = 'UTF-8';
111. //iterate through headers for real status, type, and encoding
112. if (is_array($httpHeaders) && count($httpHeaders) > 0)
113. {
114. //look at the first headers for the HTTP status code
115. //and message (errors are usually returned this way)
116. //
117. //HTTP 100 Continue response can also be returned before
118. //the REAL status header, so we need look until we find
119. //the last header starting with HTTP
120. //
121. //the spec: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.1
122. //
123. //Thanks to Daniel Andersson for pointing out this oversight
124. while (isset($httpHeaders[0]) && substr($httpHeaders[0], 0, 4) == 'HTTP')
125. {
126. $parts = explode(' ', substr($httpHeaders[0], 9), 2);
128. $status = $parts[0];
129. $statusMessage = trim($parts[1]);
131. array_shift($httpHeaders);
132. }
134. //Look for the Content-Type response header and determine type
135. //and encoding from it (if possible - such as 'Content-Type: text/plain; charset=UTF-8')
136. foreach ($httpHeaders as $header)
137. {
138. if (strncasecmp($header, 'Content-Type:', 13) == 0)
139. {
140. //split content type value into two parts if possible
141. $parts = explode(';', substr($header, 13), 2);
143. $type = trim($parts[0]);
145. if ($parts[1])
146. {
147. //split the encoding section again to get the value
148. $parts = explode('=', $parts[1], 2);
150. if ($parts[1])
151. {
152. $encoding = trim($parts[1]);
153. }
154. }
156. break;
157. }
158. }
159. }
161. $this->_rawResponse = $rawResponse;
162. $this->_type = $type;
163. $this->_encoding = $encoding;
164. $this->_httpStatus = $status;
165. $this->_httpStatusMessage = $statusMessage;
166. $this->_createDocuments = (bool) $createDocuments;
167. $this->_collapseSingleValueArrays = (bool) $collapseSingleValueArrays;
168. }
170. /**
171. * Get the HTTP status code
172. *
173. * @return integer
174. */
175. public function getHttpStatus()
176. {
177. return $this->_httpStatus;
178. }
180. /**
181. * Get the HTTP status message of the response
182. *
183. * @return string
184. */
185. public function getHttpStatusMessage()
186. {
187. return $this->_httpStatusMessage;
188. }
190. /**
191. * Get content type of this Solr response
192. *
193. * @return string
194. */
195. public function getType()
196. {
197. return $this->_type;
198. }
200. /**
201. * Get character encoding of this response. Should usually be utf-8, but just in case
202. *
203. * @return string
204. */
205. public function getEncoding()
206. {
207. return $this->_encoding;
208. }
210. /**
211. * Get the raw response as it was given to this object
212. *
213. * @return string
214. */
215. public function getRawResponse()
216. {
217. return $this->_rawResponse;
218. }
220. /**
221. * Magic get to expose the parsed data and to lazily load it
222. *
223. * @param unknown_type $key
224. * @return unknown
225. */
226. public function __get($key)
227. {
228. if (!$this->_isParsed)
229. {
230. $this->_parseData();
231. $this->_isParsed = true;
232. }
234. if (isset($this->_parsedData->$key))
235. {
236. return $this->_parsedData->$key;
237. }
239. return null;
240. }
242. /**
243. * Parse the raw response into the parsed_data array for access
244. */
245. protected function _parseData()
246. {
247. //An alternative would be to use Zend_Json::decode(...)
248. $data = json_decode($this->_rawResponse);
250. //if we're configured to collapse single valued arrays or to convert them to Apache_Solr_Document objects
251. //and we have response documents, then try to collapse the values and / or convert them now
252. if (($this->_createDocuments || $this->_collapseSingleValueArrays) && isset($data->response) && is_array($data->response->docs))
253. {
254. $documents = array();
256. foreach ($data->response->docs as $originalDocument)
257. {
258. if ($this->_createDocuments)
259. {
260. $document = new Apache_Solr_Document();
261. }
262. else
263. {
264. $document = $originalDocument;
265. }
267. foreach ($originalDocument as $key => $value)
268. {
269. //If a result is an array with only a single
270. //value then its nice to be able to access
271. //it as if it were always a single value
272. if ($this->_collapseSingleValueArrays && is_array($value) && count($value) <= 1)
273. {
274. $value = array_shift($value);
275. }
277. $document->$key = $value;
278. }
280. $documents[] = $document;
281. }
283. $data->response->docs = $documents;
284. }
286. $this->_parsedData = $data;
287. }
288. }

Documentation generated on Mon, 09 Nov 2009 18:15:42 -0500 by phpDocumentor 1.4.2