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.  */
  38.  
  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. {
  47.     /**
  48.      * SVN Revision meta data for this class
  49.      */
  50.     const SVN_REVISION '$Revision: 19 $';
  51.  
  52.     /**
  53.      * SVN ID meta data for this class
  54.      */
  55.     const SVN_ID '$Id: Response.php 19 2009-08-12 14:08:42Z donovan.jimenez $';
  56.  
  57.     /**
  58.      * Holds the raw response used in construction
  59.      *
  60.      * @var string 
  61.      */
  62.     protected $_rawResponse;
  63.  
  64.     /**
  65.      * Parsed values from the passed in http headers
  66.      *
  67.      * @var string 
  68.      */
  69.  
  70.     /**
  71.      * Whether the raw response has been parsed
  72.      *
  73.      * @var boolean 
  74.      */
  75.     protected $_isParsed = false;
  76.  
  77.     /**
  78.      * Parsed representation of the data
  79.      *
  80.      * @var mixed 
  81.      */
  82.     protected $_parsedData;
  83.  
  84.     /**
  85.      * Data parsing flags.  Determines what extra processing should be done
  86.      * after the data is initially converted to a data structure.
  87.      *
  88.      * @var boolean 
  89.      */
  90.     protected $_createDocuments = true,
  91.             $_collapseSingleValueArrays = true;
  92.  
  93.     /**
  94.      * Constructor. Takes the raw HTTP response body and the exploded HTTP headers
  95.      *
  96.      * @param string $rawResponse 
  97.      * @param array $httpHeaders 
  98.      * @param boolean $createDocuments Whether to convert the documents json_decoded as stdClass instances to Apache_Solr_Document instances
  99.      * @param boolean $collapseSingleValueArrays Whether to make multivalued fields appear as single values
  100.      */
  101.     public function __construct($rawResponse$httpHeaders array()$createDocuments true$collapseSingleValueArrays true)
  102.     {
  103.         //Assume 0, 'Communication Error', utf-8, and  text/plain
  104.         $status 0;
  105.         $statusMessage 'Communication Error';
  106.         $type 'text/plain';
  107.         $encoding 'UTF-8';
  108.  
  109.         //iterate through headers for real status, type, and encoding
  110.         if (is_array($httpHeaders&& count($httpHeaders0)
  111.         {
  112.             //look at the first headers for the HTTP status code
  113.             //and message (errors are usually returned this way)
  114.             //
  115.             //HTTP 100 Continue response can also be returned before
  116.             //the REAL status header, so we need look until we find
  117.             //the last header starting with HTTP
  118.             //
  119.             //the spec: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.1
  120.             //
  121.             //Thanks to Daniel Andersson for pointing out this oversight
  122.             while (isset($httpHeaders[0]&& substr($httpHeaders[0]04== 'HTTP')
  123.             {
  124.                 $parts explode(' 'substr($httpHeaders[0]9)2);
  125.  
  126.                 $status $parts[0];
  127.                 $statusMessage trim($parts[1]);
  128.  
  129.                 array_shift($httpHeaders);
  130.             }
  131.  
  132.             //Look for the Content-Type response header and determine type
  133.             //and encoding from it (if possible - such as 'Content-Type: text/plain; charset=UTF-8')
  134.             foreach ($httpHeaders as $header)
  135.             {
  136.                 if (strncasecmp($header'Content-Type:'13== 0)
  137.                 {
  138.                     //split content type value into two parts if possible
  139.                     $parts explode(';'substr($header13)2);
  140.  
  141.                     $type trim($parts[0]);
  142.  
  143.                     if ($parts[1])
  144.                     {
  145.                         //split the encoding section again to get the value
  146.                         $parts explode('='$parts[1]2);
  147.  
  148.                         if ($parts[1])
  149.                         {
  150.                             $encoding trim($parts[1]);
  151.                         }
  152.                     }
  153.  
  154.                     break;
  155.                 }
  156.             }
  157.         }
  158.  
  159.         $this->_rawResponse = $rawResponse;
  160.         $this->_type = $type;
  161.         $this->_encoding = $encoding;
  162.         $this->_httpStatus = $status;
  163.         $this->_httpStatusMessage = $statusMessage;
  164.         $this->_createDocuments = (bool) $createDocuments;
  165.         $this->_collapseSingleValueArrays = (bool) $collapseSingleValueArrays;
  166.     }
  167.  
  168.     /**
  169.      * Get the HTTP status code
  170.      *
  171.      * @return integer 
  172.      */
  173.     public function getHttpStatus()
  174.     {
  175.         return $this->_httpStatus;
  176.     }
  177.  
  178.     /**
  179.      * Get the HTTP status message of the response
  180.      *
  181.      * @return string 
  182.      */
  183.     public function getHttpStatusMessage()
  184.     {
  185.         return $this->_httpStatusMessage;
  186.     }
  187.  
  188.     /**
  189.      * Get content type of this Solr response
  190.      *
  191.      * @return string 
  192.      */
  193.     public function getType()
  194.     {
  195.         return $this->_type;
  196.     }
  197.  
  198.     /**
  199.      * Get character encoding of this response. Should usually be utf-8, but just in case
  200.      *
  201.      * @return string 
  202.      */
  203.     public function getEncoding()
  204.     {
  205.         return $this->_encoding;
  206.     }
  207.  
  208.     /**
  209.      * Get the raw response as it was given to this object
  210.      *
  211.      * @return string 
  212.      */
  213.     public function getRawResponse()
  214.     {
  215.         return $this->_rawResponse;
  216.     }
  217.  
  218.     /**
  219.      * Magic get to expose the parsed data and to lazily load it
  220.      *
  221.      * @param unknown_type $key 
  222.      * @return unknown 
  223.      */
  224.     public function __get($key)
  225.     {
  226.         if (!$this->_isParsed)
  227.         {
  228.             $this->_parseData();
  229.             $this->_isParsed = true;
  230.         }
  231.  
  232.         if (isset($this->_parsedData->$key))
  233.         {
  234.             return $this->_parsedData->$key;
  235.         }
  236.  
  237.         return null;
  238.     }
  239.  
  240.     /**
  241.      * Parse the raw response into the parsed_data array for access
  242.      */
  243.     protected function _parseData()
  244.     {
  245.         //An alternative would be to use Zend_Json::decode(...)
  246.         $data json_decode($this->_rawResponse);
  247.  
  248.         //if we're configured to collapse single valued arrays or to convert them to Apache_Solr_Document objects
  249.         //and we have response documents, then try to collapse the values and / or convert them now
  250.         if (($this->_createDocuments || $this->_collapseSingleValueArrays&& isset($data->response&& is_array($data->response->docs))
  251.         {
  252.             $documents array();
  253.  
  254.             foreach ($data->response->docs as $originalDocument)
  255.             {
  256.                 if ($this->_createDocuments)
  257.                 {
  258.                     $document new Apache_Solr_Document();
  259.                 }
  260.                 else
  261.                 {
  262.                     $document $originalDocument;
  263.                 }
  264.  
  265.                 foreach ($originalDocument as $key => $value)
  266.                 {
  267.                     //If a result is an array with only a single
  268.                     //value then its nice to be able to access
  269.                     //it as if it were always a single value
  270.                     if ($this->_collapseSingleValueArrays && is_array($value&& count($value<= 1)
  271.                     {
  272.                         $value array_shift($value);
  273.                     }
  274.  
  275.                     $document->$key $value;
  276.                 }
  277.  
  278.                 $documents[$document;
  279.             }
  280.  
  281.             $data->response->docs $documents;
  282.         }
  283.  
  284.         $this->_parsedData = $data;
  285.     }
  286. }

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