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

Documentation generated on Tue, 02 Sep 2008 10:45:04 -0400 by phpDocumentor 1.4.0