Response.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. */
  38. require_once(dirname(__FILE__) . '/ParserException.php');
  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: 54 $';
  52. /**
  53. * SVN ID meta data for this class
  54. */
  55. const SVN_ID = '$Id: Response.php 54 2011-02-04 16:29:18Z donovan.jimenez $';
  56. /**
  57. * Holds the raw response used in construction
  58. *
  59. * @var Apache_Solr_HttpTransport_Response HTTP response
  60. */
  61. protected $_response;
  62. /**
  63. * Whether the raw response has been parsed
  64. *
  65. * @var boolean
  66. */
  67. protected $_isParsed = false;
  68. /**
  69. * Parsed representation of the data
  70. *
  71. * @var mixed
  72. */
  73. protected $_parsedData;
  74. /**
  75. * Data parsing flags. Determines what extra processing should be done
  76. * after the data is initially converted to a data structure.
  77. *
  78. * @var boolean
  79. */
  80. protected $_createDocuments = true,
  81. $_collapseSingleValueArrays = true;
  82. /**
  83. * Constructor. Takes the raw HTTP response body and the exploded HTTP headers
  84. *
  85. * @return Apache_Solr_HttpTransport_Response HTTP response
  86. * @param boolean $createDocuments Whether to convert the documents json_decoded as stdClass instances to Apache_Solr_Document instances
  87. * @param boolean $collapseSingleValueArrays Whether to make multivalued fields appear as single values
  88. */
  89. public function __construct(Apache_Solr_HttpTransport_Response $response, $createDocuments = true, $collapseSingleValueArrays = true)
  90. {
  91. $this->_response = $response;
  92. $this->_createDocuments = (bool) $createDocuments;
  93. $this->_collapseSingleValueArrays = (bool) $collapseSingleValueArrays;
  94. }
  95. /**
  96. * Get the HTTP status code
  97. *
  98. * @return integer
  99. */
  100. public function getHttpStatus()
  101. {
  102. return $this->_response->getStatusCode();
  103. }
  104. /**
  105. * Get the HTTP status message of the response
  106. *
  107. * @return string
  108. */
  109. public function getHttpStatusMessage()
  110. {
  111. return $this->_response->getStatusMessage();
  112. }
  113. /**
  114. * Get content type of this Solr response
  115. *
  116. * @return string
  117. */
  118. public function getType()
  119. {
  120. return $this->_response->getMimeType();
  121. }
  122. /**
  123. * Get character encoding of this response. Should usually be utf-8, but just in case
  124. *
  125. * @return string
  126. */
  127. public function getEncoding()
  128. {
  129. return $this->_response->getEncoding();
  130. }
  131. /**
  132. * Get the raw response as it was given to this object
  133. *
  134. * @return string
  135. */
  136. public function getRawResponse()
  137. {
  138. return $this->_response->getBody();
  139. }
  140. /**
  141. * Magic get to expose the parsed data and to lazily load it
  142. *
  143. * @param string $key
  144. * @return mixed
  145. */
  146. public function __get($key)
  147. {
  148. if (!$this->_isParsed)
  149. {
  150. $this->_parseData();
  151. $this->_isParsed = true;
  152. }
  153. if (isset($this->_parsedData->$key))
  154. {
  155. return $this->_parsedData->$key;
  156. }
  157. return null;
  158. }
  159. /**
  160. * Magic function for isset function on parsed data
  161. *
  162. * @param string $key
  163. * @return boolean
  164. */
  165. public function __isset($key)
  166. {
  167. if (!$this->_isParsed)
  168. {
  169. $this->_parseData();
  170. $this->_isParsed = true;
  171. }
  172. return isset($this->_parsedData->$key);
  173. }
  174. /**
  175. * Parse the raw response into the parsed_data array for access
  176. *
  177. * @throws Apache_Solr_ParserException If the data could not be parsed
  178. */
  179. protected function _parseData()
  180. {
  181. //An alternative would be to use Zend_Json::decode(...)
  182. $data = json_decode($this->_response->getBody());
  183. // check that we receive a valid JSON response - we should never receive a null
  184. if ($data === null)
  185. {
  186. throw new Apache_Solr_ParserException('Solr response does not appear to be valid JSON, please examine the raw response with getRawResponse() method');
  187. }
  188. //if we're configured to collapse single valued arrays or to convert them to Apache_Solr_Document objects
  189. //and we have response documents, then try to collapse the values and / or convert them now
  190. if (($this->_createDocuments || $this->_collapseSingleValueArrays) && isset($data->response) && is_array($data->response->docs))
  191. {
  192. $documents = array();
  193. foreach ($data->response->docs as $originalDocument)
  194. {
  195. if ($this->_createDocuments)
  196. {
  197. $document = new Apache_Solr_Document();
  198. }
  199. else
  200. {
  201. $document = $originalDocument;
  202. }
  203. foreach ($originalDocument as $key => $value)
  204. {
  205. //If a result is an array with only a single
  206. //value then its nice to be able to access
  207. //it as if it were always a single value
  208. if ($this->_collapseSingleValueArrays && is_array($value) && count($value) <= 1)
  209. {
  210. $value = array_shift($value);
  211. }
  212. $document->$key = $value;
  213. }
  214. $documents[] = $document;
  215. }
  216. $data->response->docs = $documents;
  217. }
  218. $this->_parsedData = $data;
  219. }
  220. }