home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / XML / RPC2 / Backend / Php / Response.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  6.1 KB  |  143 lines

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
  4.  
  5. // LICENSE AGREEMENT. If folded, press za here to unfold and read license {{{ 
  6.  
  7. /**
  8. * +-----------------------------------------------------------------------------+
  9. * | Copyright (c) 2004-2006 Sergio Goncalves Carvalho                                |
  10. * +-----------------------------------------------------------------------------+
  11. * | This file is part of XML_RPC2.                                              |
  12. * |                                                                             |
  13. * | XML_RPC2 is free software; you can redistribute it and/or modify            |
  14. * | it under the terms of the GNU Lesser General Public License as published by |
  15. * | the Free Software Foundation; either version 2.1 of the License, or         |
  16. * | (at your option) any later version.                                         |
  17. * |                                                                             |
  18. * | XML_RPC2 is distributed in the hope that it will be useful,                 |
  19. * | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
  20. * | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
  21. * | GNU Lesser General Public License for more details.                         |
  22. * |                                                                             |
  23. * | You should have received a copy of the GNU Lesser General Public License    |
  24. * | along with XML_RPC2; if not, write to the Free Software                     |
  25. * | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA                    |
  26. * | 02111-1307 USA                                                              |
  27. * +-----------------------------------------------------------------------------+
  28. * | Author: Sergio Carvalho <sergio.carvalho@portugalmail.com>                  |
  29. * +-----------------------------------------------------------------------------+
  30. *
  31. * @category   XML
  32. * @package    XML_RPC2
  33. * @author     Sergio Carvalho <sergio.carvalho@portugalmail.com>  
  34. * @copyright  2004-2006 Sergio Carvalho
  35. * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
  36. * @version    CVS: $Id: Response.php,v 1.5 2006/01/22 02:00:41 fab Exp $
  37. * @link       http://pear.php.net/package/XML_RPC2
  38. */
  39.  
  40. // }}}
  41.  
  42. // dependencies {{{
  43. require_once 'XML/RPC2/Exception.php';
  44. require_once 'XML/RPC2/Backend/Php/Value.php';
  45. require_once 'XML/RPC2/Backend/Php/Value/Struct.php';
  46. // }}}
  47.  
  48. /**
  49.  * XML-RPC response backend class. 
  50.  *
  51.  * This class represents an XML_RPC request, exposing the methods 
  52.  * needed to encode/decode an xml-rpc response.
  53.  *
  54.  * @category   XML
  55.  * @package    XML_RPC2
  56.  * @author     Sergio Carvalho <sergio.carvalho@portugalmail.com>  
  57.  * @copyright  2004-2006 Sergio Carvalho
  58.  * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
  59.  * @link       http://pear.php.net/package/XML_RPC2 
  60.  */
  61. class XML_RPC2_Backend_Php_Response
  62. {
  63.     
  64.     // {{{ encode()
  65.     
  66.     /**
  67.      * Encode a normal XML-RPC response, containing the provided value
  68.      *
  69.      * You may supply a php-native value, or an XML_RPC2_Backend_Php_Value instance, to be returned. Usually providing a native value
  70.      * is more convenient. However, for some types, XML_RPC2_Backend_Php_Value::createFromNative can't properly choose the xml-rpc 
  71.      * type. In these cases, constructing an XML_RPC2_Backend_Php_Value and using it as param here is the only way to return the desired 
  72.      * type.
  73.      *
  74.      * @see http://www.xmlrpc.com/spec
  75.      * @see XML_RPC2_Backend_Php_Value::createFromNative
  76.      * @param mixed $param The result value which the response will envelop
  77.      * @param string $encoding encoding
  78.      * @return string The XML payload
  79.      */
  80.     public static function encode($param, $encoding = 'iso-8859-1') 
  81.     {
  82.         if (!$param instanceof XML_RPC2_Backend_Php_Value) {
  83.             $param = XML_RPC2_Backend_Php_Value::createFromNative($param);
  84.         }
  85.         $result  = '<?xml version="1.0" encoding="' .  $encoding . '"?>';
  86.         $result .= '<methodResponse><params><param><value>' . $param->encode() . '</value></param></params></methodResponse>';
  87.         return $result;
  88.     }
  89.     
  90.     // }}}
  91.     // {{{ encodeFault()
  92.     
  93.     /**
  94.      * Encode a fault XML-RPC response, containing the provided code and message
  95.      *
  96.      * @see http://www.xmlrpc.com/spec
  97.      * @param int $code Response code
  98.      * @param string $message Response message
  99.      * @param string $encoding encoding
  100.      * @return string The XML payload
  101.      */
  102.     public static function encodeFault($code, $message, $encoding = 'iso-8859-1')
  103.     {
  104.         $value = new XML_RPC2_Backend_Php_Value_Struct(array('faultCode' => (int) $code, 'faultString' => (string) $message));
  105.         $result  = '<?xml version="1.0" encoding="' .  $encoding . '"?>';
  106.         $result .= '<methodResponse><fault><value>' . $value->encode() . '</value></fault></methodResponse>';
  107.         return $result;
  108.     }
  109.     
  110.     // }}}
  111.     // {{{ decode()
  112.     
  113.     /**
  114.      * Parse a response and either return the native PHP result.
  115.      *
  116.      * This method receives an XML-RPC response document, in SimpleXML format, decodes it and returns the payload value.
  117.      *
  118.      * @param SimpleXmlElement $xml The Transport XML
  119.      * @return mixed The response payload
  120.      *
  121.      * @see http://www.xmlrpc.com/spec
  122.      * @throws XML_RPC2_FaultException Signals the decoded response was an XML-RPC fault
  123.      * @throws XML_RPC2_DecodeException Signals an ill formed payload response section
  124.      */
  125.     public static function decode(SimpleXMLElement $xml) 
  126.     {
  127.         $faultNode = $xml->xpath('/methodResponse/fault');
  128.         if (count($faultNode) == 1) {
  129.             throw XML_RPC2_FaultException::createFromDecode($faultNode[0]);
  130.         }
  131.         $paramValueNode = $xml->xpath('/methodResponse/params/param/value');
  132.         if (count($paramValueNode) == 1) {
  133.             return XML_RPC2_Backend_Php_Value::createFromDecode($paramValueNode[0])->getNativeValue();
  134.         }
  135.         throw new XML_RPC2_DecodeException('Unable to decode xml-rpc response. No fault nor params/param elements found');
  136.     }
  137.     
  138.     // }}}
  139.     
  140. }
  141.  
  142. ?>
  143.