File Source for Response.php

# Source for file Response.php

Documentation is available at Response.php

1. <?php
2. /**
3. * Copyright (c) 2007-2011, Servigistics, 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 Servigistics, 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-2011 Servigistics, Inc. (http://servigistics.com)
31. * @license http://solr-php-client.googlecode.com/svn/trunk/COPYING New BSD
32. * @version $Id: Response.php 54 2011-02-04 16:29:18Z donovan.jimenez $
33. *
34. * @package Apache
35. * @subpackage Solr
36. * @author Donovan Jimenez <djimenez@conduit-it.com>
37. */
39. require_once(dirname(__FILE__) . '/ParserException.php');
41. /**
42. * Represents a Solr response. Parses the raw response into a set of stdClass objects
43. * and associative arrays for easy access.
44. *
45. * Currently requires json_decode which is bundled with PHP >= 5.2.0, Alternatively can be
46. * installed with PECL. Zend Framework also includes a purely PHP solution.
47. */
48. class Apache_Solr_Response
49. {
50. /**
51. * SVN Revision meta data for this class
52. */
53. const SVN_REVISION = '$Revision: 54 $';
55. /**
56. * SVN ID meta data for this class
57. */
58. const SVN_ID = '$Id: Response.php 54 2011-02-04 16:29:18Z donovan.jimenez $';
60. /**
61. * Holds the raw response used in construction
62. *
63. * @var Apache_Solr_HttpTransport_Response HTTP response
64. */
65. protected $_response;
67. /**
68. * Whether the raw response has been parsed
69. *
70. * @var boolean
71. */
72. protected $_isParsed = false;
74. /**
75. * Parsed representation of the data
76. *
77. * @var mixed
78. */
79. protected $_parsedData;
81. /**
82. * Data parsing flags. Determines what extra processing should be done
83. * after the data is initially converted to a data structure.
84. *
85. * @var boolean
86. */
87. protected $_createDocuments = true,
88. $_collapseSingleValueArrays = true;
90. /**
91. * Constructor. Takes the raw HTTP response body and the exploded HTTP headers
92. *
93. * @return Apache_Solr_HttpTransport_Response HTTP response
94. * @param boolean $createDocuments Whether to convert the documents json_decoded as stdClass instances to Apache_Solr_Document instances
95. * @param boolean $collapseSingleValueArrays Whether to make multivalued fields appear as single values
96. */
97. public function __construct(Apache_Solr_HttpTransport_Response $response, $createDocuments = true, $collapseSingleValueArrays = true)
98. {
99. $this->_response = $response;
100. $this->_createDocuments = (bool) $createDocuments;
101. $this->_collapseSingleValueArrays = (bool) $collapseSingleValueArrays;
102. }
104. /**
105. * Get the HTTP status code
106. *
107. * @return integer
108. */
109. public function getHttpStatus()
110. {
111. return $this->_response-> getStatusCode();
112. }
114. /**
115. * Get the HTTP status message of the response
116. *
117. * @return string
118. */
119. public function getHttpStatusMessage()
120. {
121. return $this->_response-> getStatusMessage();
122. }
124. /**
125. * Get content type of this Solr response
126. *
127. * @return string
128. */
129. public function getType()
130. {
131. return $this->_response-> getMimeType();
132. }
134. /**
135. * Get character encoding of this response. Should usually be utf-8, but just in case
136. *
137. * @return string
138. */
139. public function getEncoding()
140. {
141. return $this->_response-> getEncoding();
142. }
144. /**
145. * Get the raw response as it was given to this object
146. *
147. * @return string
148. */
149. public function getRawResponse()
150. {
151. return $this->_response-> getBody();
152. }
154. /**
155. * Magic get to expose the parsed data and to lazily load it
156. *
157. * @param string $key
158. * @return mixed
159. */
160. public function __get($key)
161. {
162. if (!$this->_isParsed)
163. {
164. $this->_parseData();
165. $this->_isParsed = true;
166. }
168. if (isset($this->_parsedData->$key))
169. {
170. return $this->_parsedData->$key;
171. }
173. return null;
174. }
176. /**
177. * Magic function for isset function on parsed data
178. *
179. * @param string $key
180. * @return boolean
181. */
182. public function __isset($key)
183. {
184. if (!$this->_isParsed)
185. {
186. $this->_parseData();
187. $this->_isParsed = true;
188. }
190. return isset($this->_parsedData->$key);
191. }
193. /**
194. * Parse the raw response into the parsed_data array for access
195. *
196. * @throws Apache_Solr_ParserException If the data could not be parsed
197. */
198. protected function _parseData()
199. {
200. //An alternative would be to use Zend_Json::decode(...)
201. $data = json_decode($this->_response-> getBody());
203. // check that we receive a valid JSON response - we should never receive a null
204. if ($data === null)
205. {
206. throw new Apache_Solr_ParserException('Solr response does not appear to be valid JSON, please examine the raw response with getRawResponse() method');
207. }
209. //if we're configured to collapse single valued arrays or to convert them to Apache_Solr_Document objects
210. //and we have response documents, then try to collapse the values and / or convert them now
211. if (($this->_createDocuments || $this->_collapseSingleValueArrays) && isset($data->response) && is_array($data->response->docs))
212. {
213. $documents = array();
215. foreach ($data->response->docs as $originalDocument)
216. {
217. if ($this->_createDocuments)
218. {
219. $document = new Apache_Solr_Document();
220. }
221. else
222. {
223. $document = $originalDocument;
224. }
226. foreach ($originalDocument as $key => $value)
227. {
228. //If a result is an array with only a single
229. //value then its nice to be able to access
230. //it as if it were always a single value
231. if ($this->_collapseSingleValueArrays && is_array($value) && count($value) <= 1)
232. {
233. $value = array_shift($value);
234. }
236. $document->$key = $value;
237. }
239. $documents[] = $document;
240. }
242. $data->response->docs = $documents;
243. }
245. $this->_parsedData = $data;
246. }
247. }

Documentation generated on Wed, 04 May 2011 11:01:15 -0400 by phpDocumentor 1.4.3