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

Documentation generated on Wed, 11 Mar 2009 17:34:15 -0400 by phpDocumentor 1.4.2