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

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