File indexing completed on 2024-06-16 05:30:25

0001 <?php
0002 
0003 /**
0004  * Copyright (c) 2007-2011, Servigistics, Inc.
0005  * All rights reserved.
0006  *
0007  * Redistribution and use in source and binary forms, with or without
0008  * modification, are permitted provided that the following conditions are met:
0009  *
0010  *  - Redistributions of source code must retain the above copyright notice,
0011  *    this list of conditions and the following disclaimer.
0012  *  - Redistributions in binary form must reproduce the above copyright
0013  *    notice, this list of conditions and the following disclaimer in the
0014  *    documentation and/or other materials provided with the distribution.
0015  *  - Neither the name of Servigistics, Inc. nor the names of
0016  *    its contributors may be used to endorse or promote products derived from
0017  *    this software without specific prior written permission.
0018  *
0019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0020  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0021  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0022  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0023  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0024  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0025  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0026  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0027  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0028  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0029  * POSSIBILITY OF SUCH DAMAGE.
0030  *
0031  * @copyright Copyright 2007-2011 Servigistics, Inc. (http://servigistics.com)
0032  * @license http://solr-php-client.googlecode.com/svn/trunk/COPYING New BSD
0033  * @version $Id: Response.php 54 2011-02-04 16:29:18Z donovan.jimenez $
0034  *
0035  * @package Apache
0036  * @subpackage Solr
0037  * @author Donovan Jimenez <djimenez@conduit-it.com>
0038  */
0039 require_once 'Zend/Service/Solr/ParserException.php';
0040 
0041 /**
0042  * Represents a Solr response.  Parses the raw response into a set of stdClass objects
0043  * and associative arrays for easy access.
0044  *
0045  * Currently requires json_decode which is bundled with PHP >= 5.2.0, Alternatively can be
0046  * installed with PECL.  Zend Framework also includes a purely PHP solution.
0047  */
0048 class Zend_Service_Solr_Response {
0049     /**
0050      * SVN Revision meta data for this class
0051      */
0052     const SVN_REVISION = '$Revision: 54 $';
0053 
0054     /**
0055      * SVN ID meta data for this class
0056      */
0057     const SVN_ID = '$Id: Response.php 54 2011-02-04 16:29:18Z donovan.jimenez $';
0058 
0059     /**
0060      * Holds the raw response used in construction
0061      *
0062      * @var Zend_Service_Solr_HttpTransport_Response HTTP response
0063      */
0064     protected $_response;
0065 
0066     /**
0067      * Whether the raw response has been parsed
0068      *
0069      * @var boolean
0070      */
0071     protected $_isParsed = false;
0072 
0073     /**
0074      * Parsed representation of the data
0075      *
0076      * @var mixed
0077      */
0078     protected $_parsedData;
0079 
0080     /**
0081      * Data parsing flags.  Determines what extra processing should be done
0082      * after the data is initially converted to a data structure.
0083      *
0084      * @var boolean
0085      */
0086     protected $_createDocuments = true,
0087     $_collapseSingleValueArrays = true;
0088 
0089     /**
0090      * Constructor. Takes the raw HTTP response body and the exploded HTTP headers
0091      *
0092      * @return Zend_Service_Solr_HttpTransport_Response HTTP response
0093      * @param boolean $createDocuments Whether to convert the documents json_decoded as stdClass instances to Zend_Service_Solr_Document instances
0094      * @param boolean $collapseSingleValueArrays Whether to make multivalued fields appear as single values
0095      */
0096     public function __construct(Zend_Service_Solr_HttpTransport_Response $response, $createDocuments = true, $collapseSingleValueArrays = true) {
0097         $this->_response = $response;
0098         $this->_createDocuments = (bool) $createDocuments;
0099         $this->_collapseSingleValueArrays = (bool) $collapseSingleValueArrays;
0100     }
0101 
0102     /**
0103      * Get the HTTP status code
0104      *
0105      * @return integer
0106      */
0107     public function getHttpStatus() {
0108         return $this->_response->getStatusCode();
0109     }
0110 
0111     /**
0112      * Get the HTTP status message of the response
0113      *
0114      * @return string
0115      */
0116     public function getHttpStatusMessage() {
0117         return $this->_response->getStatusMessage();
0118     }
0119 
0120     /**
0121      * Get content type of this Solr response
0122      *
0123      * @return string
0124      */
0125     public function getType() {
0126         return $this->_response->getMimeType();
0127     }
0128 
0129     /**
0130      * Get character encoding of this response. Should usually be utf-8, but just in case
0131      *
0132      * @return string
0133      */
0134     public function getEncoding() {
0135         return $this->_response->getEncoding();
0136     }
0137 
0138     /**
0139      * Get the raw response as it was given to this object
0140      *
0141      * @return string
0142      */
0143     public function getRawResponse() {
0144         return $this->_response->getBody();
0145     }
0146 
0147     /**
0148      * Magic get to expose the parsed data and to lazily load it
0149      *
0150      * @param string $key
0151      * @return mixed
0152      */
0153     public function __get($key) {
0154         if (!$this->_isParsed) {
0155             $this->_parseData();
0156             $this->_isParsed = true;
0157         }
0158 
0159         if (isset($this->_parsedData->$key)) {
0160             return $this->_parsedData->$key;
0161         }
0162 
0163         return null;
0164     }
0165 
0166     /**
0167      * Magic function for isset function on parsed data
0168      *
0169      * @param string $key
0170      * @return boolean
0171      */
0172     public function __isset($key) {
0173         if (!$this->_isParsed) {
0174             $this->_parseData();
0175             $this->_isParsed = true;
0176         }
0177 
0178         return isset($this->_parsedData->$key);
0179     }
0180 
0181     /**
0182      * Parse the raw response into the parsed_data array for access
0183      *
0184      * @throws Zend_Service_Solr_ParserException If the data could not be parsed
0185      */
0186     protected function _parseData() {
0187         //An alternative would be to use Zend_Json::decode(...)
0188         $data = json_decode($this->_response->getBody());
0189 
0190         // check that we receive a valid JSON response - we should never receive a null
0191         if ($data === null) {
0192             throw new Zend_Service_Solr_ParserException('Solr response does not appear to be valid JSON, please examine the raw response with getRawResponse() method');
0193         }
0194 
0195         //if we're configured to collapse single valued arrays or to convert them to Zend_Service_Solr_Document objects
0196         //and we have response documents, then try to collapse the values and / or convert them now
0197         if (($this->_createDocuments || $this->_collapseSingleValueArrays) && isset($data->response) && is_array($data->response->docs)) {
0198             $documents = array();
0199 
0200             foreach ($data->response->docs as $originalDocument) {
0201                 if ($this->_createDocuments) {
0202                     $document = new Zend_Service_Solr_Document();
0203                 } else {
0204                     $document = $originalDocument;
0205                 }
0206 
0207                 foreach ($originalDocument as $key => $value) {
0208                     //If a result is an array with only a single
0209                     //value then its nice to be able to access
0210                     //it as if it were always a single value
0211                     if ($this->_collapseSingleValueArrays && is_array($value) && count($value) <= 1) {
0212                         $value = array_shift($value);
0213                     }
0214 
0215                     $document->$key = $value;
0216                 }
0217 
0218                 $documents[] = $document;
0219             }
0220 
0221             $data->response->docs = $documents;
0222         }
0223 
0224         $this->_parsedData = $data;
0225     }
0226 
0227 }