Source for file Response.php

Documentation is available at Response.php

  1. <?php
  2. /**
  3.  * @copyright Copyright 2007 Conduit Internet Technologies, Inc. (http://conduit-it.com)
  4.  * @license Apache Licence, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
  5.  *
  6.  *  Licensed under the Apache License, Version 2.0 (the "License");
  7.  *  you may not use this file except in compliance with the License.
  8.  *  You may obtain a copy of the License at
  9.  *
  10.  *      http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  *  Unless required by applicable law or agreed to in writing, software
  13.  *  distributed under the License is distributed on an "AS IS" BASIS,
  14.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15.  *  See the License for the specific language governing permissions and
  16.  *  limitations under the License.
  17.  *
  18.  * @package Apache
  19.  * @subpackage Solr
  20.  * @author Donovan Jimenez <djimenez@conduit-it.com>
  21.  */
  22.  
  23. /**
  24.  * Represents a Solr response.  Parses the raw response into a set of stdClass objects
  25.  * and associative arrays for easy access.
  26.  *
  27.  * Currently requires json_decode which is bundled with PHP >= 5.2.0, Alternatively can be
  28.  * installed with PECL.  Zend Framework also includes a purely PHP solution.
  29.  *
  30.  * @todo When Solr 1.3 is released, possibly convert to use PHP or Serialized PHP output writer
  31.  */
  32. {
  33.     /**
  34.      * Holds the raw response used in construction
  35.      *
  36.      * @var string 
  37.      */
  38.     private $_rawResponse;
  39.  
  40.     /**
  41.      * Parsed values from the passed in http headers. Assumes UTF-8 XML (default Solr response format)
  42.      *
  43.      * @var string 
  44.      */
  45.     private $_httpStatus 200;
  46.     private $_httpStatusMessage '';
  47.     private $_type 'text/xml';
  48.     private $_encoding 'UTF-8';
  49.  
  50.     private $_isParsed false;
  51.     private $_parsedData;
  52.  
  53.     /**
  54.      * Constructor. Takes the raw HTTP response body and the exploded HTTP headers
  55.      *
  56.      * @param string $rawResponse 
  57.      * @param array $httpHeaders 
  58.      */
  59.     public function __construct($rawResponse$httpHeaders array())
  60.     {
  61.         //Assume 0, 'Communication Error', utf-8, and  text/xml
  62.         $status 0;
  63.         $statusMessage 'Communication Error';
  64.         $type 'text/plain';
  65.         $encoding 'UTF-8';
  66.  
  67.         //iterate through headers for real status, type, and encoding
  68.         if (is_array($httpHeaders&& count($httpHeaders0)
  69.         {
  70.             //look at the first header for the HTTP status code
  71.             //and message (errors are usually returned this way)
  72.             if (substr($httpHeaders[0]04== 'HTTP')
  73.             {
  74.                 $parts split(' 'substr($httpHeaders[0]9)2);
  75.  
  76.                 $status $parts[0];
  77.                 $statusMessage trim($parts[1]);
  78.  
  79.                 array_shift($httpHeaders);
  80.             }
  81.  
  82.             //Look for the Content-Type response header and determine type
  83.             //and encoding from it (if possible)
  84.             foreach ($httpHeaders as $header)
  85.             {
  86.                 if (substr($header013== 'Content-Type:')
  87.                 {
  88.                     //split content type into two parts if possible
  89.                     $parts split(';'substr($header13)2);
  90.  
  91.                     $type trim($parts[0]);
  92.  
  93.                     if ($parts[1])
  94.                     {
  95.                         //split the encoding section again to get the value
  96.                         $parts split('='$parts[1]2);
  97.  
  98.                         if ($parts[1])
  99.                         {
  100.                             $encoding trim($parts[1]);
  101.                         }
  102.                     }
  103.  
  104.                     break;
  105.                 }
  106.             }
  107.         }
  108.  
  109.         $this->_rawResponse $rawResponse;
  110.         $this->_type $type;
  111.         $this->_encoding $encoding;
  112.         $this->_httpStatus $status;
  113.         $this->_httpStatusMessage $statusMessage;
  114.     }
  115.  
  116.     /**
  117.      * Get the HTTP status code
  118.      *
  119.      * @return integer 
  120.      */
  121.     public function getHttpStatus()
  122.     {
  123.         return $this->_httpStatus;
  124.     }
  125.  
  126.     /**
  127.      * Get the HTTP status message of the response
  128.      *
  129.      * @return string 
  130.      */
  131.     public function getHttpStatusMessage()
  132.     {
  133.         return $this->_httpStatusMessage;
  134.     }
  135.  
  136.     /**
  137.      * Get content type of this Solr response
  138.      *
  139.      * @return string 
  140.      */
  141.     public function getType()
  142.     {
  143.         return $this->_type;
  144.     }
  145.  
  146.     /**
  147.      * Get character encoding of this response. Should usually be utf-8, but just in case
  148.      *
  149.      * @return string 
  150.      */
  151.     public function getEncoding()
  152.     {
  153.         return $this->_encoding;
  154.     }
  155.  
  156.     /**
  157.      * Get the raw response as it was given to this object
  158.      *
  159.      * @return string 
  160.      */
  161.     public function getRawResponse()
  162.     {
  163.         return $this->_rawResponse;
  164.     }
  165.  
  166.     /**
  167.      * Magic get to expose the parsed data and to lazily load it
  168.      *
  169.      * @param unknown_type $key 
  170.      * @return unknown 
  171.      */
  172.     public function __get($key)
  173.     {
  174.         if (!$this->_isParsed)
  175.         {
  176.             $this->_parseData();
  177.             $this->_isParsed true;
  178.         }
  179.  
  180.         if (isset($this->_parsedData->$key))
  181.         {
  182.             return $this->_parsedData->$key;
  183.         }
  184.  
  185.         return null;
  186.     }
  187.  
  188.     /**
  189.      * Parse the raw response into the parsed_data array for access
  190.      */
  191.     private function _parseData()
  192.     {
  193.         //An alternative would be to use Zend_Json::decode(...)
  194.         $data json_decode($this->_rawResponse);
  195.  
  196.         //convert $data->response->docs[*] to be Solr_Document objects
  197.         if (isset($data->response&& is_array($data->response->docs))
  198.         {
  199.             $documents array();
  200.  
  201.             foreach ($data->response->docs as $doc)
  202.             {
  203.                 $document new Apache_Solr_Document();
  204.  
  205.                 foreach ($doc as $key => $value)
  206.                 {
  207.                     //If a result is an array with only a single
  208.                     //value then its nice to be able to access
  209.                     //it as if it were always a single value
  210.                     if (is_array($value&& count($value<= 1)
  211.                     {
  212.                         $value array_shift($value);
  213.                     }
  214.  
  215.                     $document->$key $value;
  216.                 }
  217.  
  218.                 $documents[$document;
  219.             }
  220.  
  221.             $data->response->docs $documents;
  222.         }
  223.  
  224.         //correct facet counts to make sense
  225.         //converts array([field value], [facet count], [field value], [facet count] ...) format
  226.         //to array([field value] => [facet count], ... ) format
  227.         if (isset($data->facet_counts&& isset($data->facet_counts->facet_fields))
  228.         {
  229.             foreach ($data->facet_counts->facet_fields as $key => $facet_array)
  230.             {
  231.                 $new_facet_array array();
  232.  
  233.                 while (count($facet_array0)
  234.                 {
  235.                     $new_facet_array[array_shift($facet_array)array_shift($facet_array);
  236.                 }
  237.  
  238.                 $data->facet_counts->facet_fields->$key $new_facet_array;
  239.             }
  240.         }
  241.  
  242.         $this->_parsedData $data;
  243.     }
  244. }

Documentation generated on Tue, 02 Oct 2007 12:55:38 -0400 by phpDocumentor 1.4.0