[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/lib/zend/Zend/XmlRpc/ -> Response.php (source)

   1  <?php
   2  /**
   3   * Zend Framework
   4   *
   5   * LICENSE
   6   *
   7   * This source file is subject to the new BSD license that is bundled
   8   * with this package in the file LICENSE.txt.
   9   * It is also available through the world-wide-web at this URL:
  10   * http://framework.zend.com/license/new-bsd
  11   * If you did not receive a copy of the license and are unable to
  12   * obtain it through the world-wide-web, please send an email
  13   * to [email protected] so we can send you a copy immediately.
  14   *
  15   * @category   Zend
  16   * @package    Zend_Controller
  17   * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18   * @license    http://framework.zend.com/license/new-bsd     New BSD License
  19   */
  20  
  21  /**
  22   * Zend_XmlRpc_Value
  23   */
  24  require_once 'Zend/XmlRpc/Value.php';
  25  
  26  /**
  27   * Zend_XmlRpc_Fault
  28   */
  29  require_once 'Zend/XmlRpc/Fault.php';
  30  
  31  /**
  32   * XmlRpc Response
  33   *
  34   * Container for accessing an XMLRPC return value and creating the XML response.
  35   *
  36   * @category Zend
  37   * @package  Zend_XmlRpc
  38   * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  39   * @license    http://framework.zend.com/license/new-bsd     New BSD License
  40   * @version $Id$
  41   */
  42  class Zend_XmlRpc_Response
  43  {
  44      /**
  45       * Return value
  46       * @var mixed
  47       */
  48      protected $_return;
  49  
  50      /**
  51       * Return type
  52       * @var string
  53       */
  54      protected $_type;
  55  
  56      /**
  57       * Response character encoding
  58       * @var string
  59       */
  60      protected $_encoding = 'UTF-8';
  61  
  62      /**
  63       * Fault, if response is a fault response
  64       * @var null|Zend_XmlRpc_Fault
  65       */
  66      protected $_fault = null;
  67  
  68      /**
  69       * Constructor
  70       *
  71       * Can optionally pass in the return value and type hinting; otherwise, the
  72       * return value can be set via {@link setReturnValue()}.
  73       *
  74       * @param mixed $return
  75       * @param string $type
  76       * @return void
  77       */
  78      public function __construct($return = null, $type = null)
  79      {
  80          $this->setReturnValue($return, $type);
  81      }
  82  
  83      /**
  84       * Set encoding to use in response
  85       *
  86       * @param string $encoding
  87       * @return Zend_XmlRpc_Response
  88       */
  89      public function setEncoding($encoding)
  90      {
  91          $this->_encoding = $encoding;
  92          Zend_XmlRpc_Value::setEncoding($encoding);
  93          return $this;
  94      }
  95  
  96      /**
  97       * Retrieve current response encoding
  98       *
  99       * @return string
 100       */
 101      public function getEncoding()
 102      {
 103          return $this->_encoding;
 104      }
 105  
 106      /**
 107       * Set the return value
 108       *
 109       * Sets the return value, with optional type hinting if provided.
 110       *
 111       * @param mixed $value
 112       * @param string $type
 113       * @return void
 114       */
 115      public function setReturnValue($value, $type = null)
 116      {
 117          $this->_return = $value;
 118          $this->_type = (string) $type;
 119      }
 120  
 121      /**
 122       * Retrieve the return value
 123       *
 124       * @return mixed
 125       */
 126      public function getReturnValue()
 127      {
 128          return $this->_return;
 129      }
 130  
 131      /**
 132       * Retrieve the XMLRPC value for the return value
 133       *
 134       * @return Zend_XmlRpc_Value
 135       */
 136      protected function _getXmlRpcReturn()
 137      {
 138          return Zend_XmlRpc_Value::getXmlRpcValue($this->_return);
 139      }
 140  
 141      /**
 142       * Is the response a fault response?
 143       *
 144       * @return boolean
 145       */
 146      public function isFault()
 147      {
 148          return $this->_fault instanceof Zend_XmlRpc_Fault;
 149      }
 150  
 151      /**
 152       * Returns the fault, if any.
 153       *
 154       * @return null|Zend_XmlRpc_Fault
 155       */
 156      public function getFault()
 157      {
 158          return $this->_fault;
 159      }
 160  
 161      /**
 162       * Load a response from an XML response
 163       *
 164       * Attempts to load a response from an XMLRPC response, autodetecting if it
 165       * is a fault response.
 166       *
 167       * @param string $response
 168       * @return boolean True if a valid XMLRPC response, false if a fault
 169       * response or invalid input
 170       */
 171      public function loadXml($response)
 172      {
 173          if (!is_string($response)) {
 174              $this->_fault = new Zend_XmlRpc_Fault(650);
 175              $this->_fault->setEncoding($this->getEncoding());
 176              return false;
 177          }
 178  
 179          try {
 180              $xml = new SimpleXMLElement($response);
 181          } catch (Exception $e) {
 182              // Not valid XML
 183              $this->_fault = new Zend_XmlRpc_Fault(651);
 184              $this->_fault->setEncoding($this->getEncoding());
 185              return false;
 186          }
 187  
 188          if (!empty($xml->fault)) {
 189              // fault response
 190              $this->_fault = new Zend_XmlRpc_Fault();
 191              $this->_fault->setEncoding($this->getEncoding());
 192              $this->_fault->loadXml($response);
 193              return false;
 194          }
 195  
 196          if (empty($xml->params)) {
 197              // Invalid response
 198              $this->_fault = new Zend_XmlRpc_Fault(652);
 199              $this->_fault->setEncoding($this->getEncoding());
 200              return false;
 201          }
 202  
 203          try {
 204              if (!isset($xml->params) || !isset($xml->params->param) || !isset($xml->params->param->value)) {
 205                  throw new Zend_XmlRpc_Value_Exception('Missing XML-RPC value in XML');
 206              }
 207              $valueXml = $xml->params->param->value->asXML();
 208              $value = Zend_XmlRpc_Value::getXmlRpcValue($valueXml, Zend_XmlRpc_Value::XML_STRING);
 209          } catch (Zend_XmlRpc_Value_Exception $e) {
 210              $this->_fault = new Zend_XmlRpc_Fault(653);
 211              $this->_fault->setEncoding($this->getEncoding());
 212              return false;
 213          }
 214  
 215          $this->setReturnValue($value->getValue());
 216          return true;
 217      }
 218  
 219      /**
 220       * Return response as XML
 221       *
 222       * @return string
 223       */
 224      public function saveXml()
 225      {
 226          $value = $this->_getXmlRpcReturn();
 227          $generator = Zend_XmlRpc_Value::getGenerator();
 228          $generator->openElement('methodResponse')
 229                    ->openElement('params')
 230                    ->openElement('param');
 231          $value->generateXml();
 232          $generator->closeElement('param')
 233                    ->closeElement('params')
 234                    ->closeElement('methodResponse');
 235  
 236          return $generator->flush();
 237      }
 238  
 239      /**
 240       * Return XML response
 241       *
 242       * @return string
 243       */
 244      public function __toString()
 245      {
 246          return $this->saveXML();
 247      }
 248  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1