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 / Value.php < prev   
Encoding:
PHP Script  |  2008-07-02  |  11.6 KB  |  288 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-2005 Sergio Carvalho
  35. * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
  36. * @version    CVS: $Id: Value.php,v 1.6 2006/12/02 18:09:49 sergiosgc 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/Value.php';
  45. // }}}
  46.  
  47. /**
  48.  * XML_RPC value abstract class. All XML_RPC value classes inherit from XML_RPC2_Value
  49.  *
  50.  * @category   XML
  51.  * @package    XML_RPC2
  52.  * @author     Sergio Carvalho <sergio.carvalho@portugalmail.com>  
  53.  * @copyright  2004-2005 Sergio Carvalho
  54.  * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
  55.  * @link       http://pear.php.net/package/XML_RPC2 
  56.  */
  57. abstract class XML_RPC2_Backend_Php_Value extends XML_RPC2_Value
  58. {
  59.     // {{{ properties
  60.     
  61.     /**
  62.      * native value
  63.      * 
  64.      * @var mixed
  65.      */
  66.     private $_nativeValue = null;
  67.     
  68.     // }}}
  69.     // {{{ getNativeValue()
  70.     
  71.     /**
  72.      * nativeValue property getter
  73.      *
  74.      * @return mixed The current nativeValue
  75.      */
  76.     public function getNativeValue() 
  77.     {
  78.         return $this->_nativeValue;
  79.     } 
  80.  
  81.     // }}}
  82.     // {{{ setNativeValue()
  83.     
  84.     /**
  85.      * nativeValue setter
  86.      *
  87.      * @param mixed $value
  88.      */
  89.     protected function setNativeValue($value)
  90.     {
  91.         $this->_nativeValue = $value;
  92.     }
  93.     
  94.     // }}}  
  95.     // {{{ createFromNative()
  96.     
  97.     /**
  98.      * Choose a XML_RPC2_Value subclass appropriate for the 
  99.      * given value and create it.
  100.      * 
  101.      * This method tries to find the most adequate XML-RPC datatype to hold
  102.      * a given PHP native type. Note that distinguishing some datatypes may be
  103.      * difficult:
  104.      *  - Timestamps are represented by PHP integers, so an XML_RPC2_Value_Datetime is never returned
  105.      *  - Indexed arrays and associative arrays are the same native PHP type. In this case:
  106.      *    a) The array's indexes start at 0 or 1 and increase monotonically with step 1, or
  107.      *    b) they do not
  108.      *    in the first case, an XML_RPC2_Value_Array is returned. In the second, a XML_RPC2_Value_Struct is returned.
  109.      *  - PHP Objects are serialized and represented in an XML_RPC2_Value_Base64
  110.      *
  111.      * Whenever native object automatic detection proves inaccurate, use XML_RPC2_Value::createFromNative providing 
  112.      * a valid explicit type as second argument
  113.      * 
  114.      * the appropriate XML_RPC2_Value child class instead.
  115.      *
  116.      * @param mixed The native value
  117.      * @param string The xml-rpc target encoding type, as per the xmlrpc spec (optional)
  118.      * @throws XML_RPC2_InvalidTypeEncodeException When the native value has a type that can't be translated to XML_RPC
  119.      * @return A new XML_RPC2_Value instance
  120.      * @see XML_RPC_Client::__call
  121.      * @see XML_RPC_Server
  122.      */
  123.     public static function createFromNative($nativeValue, $explicitType = null) 
  124.     {
  125.         if (is_null($explicitType)) {
  126.             switch (gettype($nativeValue)) {
  127.                 case 'boolean':
  128.                     $explicitType = 'boolean';
  129.                     break;
  130.                 case 'integer':
  131.                     $explicitType = 'int';
  132.                     break;
  133.                 case 'double':
  134.                     $explicitType = 'double';
  135.                     break;
  136.                 case 'string':
  137.                     $explicitType = 'string';
  138.                     break;
  139.                 case 'array':
  140.                     $explicitType = 'array';
  141.                     $keys = array_keys($nativeValue);
  142.                     if (($keys[0] !== 0) && ($keys[0] !== 1)) $explicitType = 'struct';
  143.                     $i=0;
  144.                     do {
  145.                         $previous = $keys[$i];
  146.                         $i++;
  147.                         if (array_key_exists($i, $keys) && ($keys[$i] !== $keys[$i - 1] + 1)) $explicitType = 'struct';
  148.                     } while (array_key_exists($i, $keys) && $explicitType == 'array');
  149.                     break;
  150.                 case 'object':
  151.                     if ((strtolower(get_class($nativeValue)) == 'stdclass') && (isset($nativeValue->xmlrpc_type))) {
  152.                         // In this case, we have a "stdclass native value" (emulate xmlrpcext)
  153.                         // the type 'base64' or 'datetime' is given by xmlrpc_type public property 
  154.                         $explicitType = $nativeValue->xmlrpc_type;
  155.                     } else {
  156.                         $nativeValue = serialize($nativeValue);
  157.                         $explicitType = 'base64';
  158.                     }
  159.                     break;
  160.                 case 'resource':
  161.                 case 'NULL':
  162.                 case 'unknown type':
  163.                     throw new XML_RPC2_InvalidTypeEncodeException(sprintf('Impossible to encode value \'%s\' from type \'%s\'. No analogous type in XML_RPC.', 
  164.                         (string) $nativeValue,
  165.                         gettype($nativeValue)));
  166.                 default:
  167.                     throw new XML_RPC2_InvalidTypeEncodeException(sprintf('Unexpected PHP native type returned by gettype: \'%s\', for value \'%s\'',
  168.                         gettype($nativeValue),
  169.                         (string) $nativeValue));
  170.             }
  171.         }        
  172.         $explicitType = ucfirst(strtolower($explicitType));
  173.         switch ($explicitType) {
  174.             case 'I4':
  175.             case 'Int':
  176.             case 'Boolean':
  177.             case 'Double':
  178.             case 'String':
  179.                 require_once 'XML/RPC2/Backend/Php/Value/Scalar.php';
  180.                 return XML_RPC2_Backend_Php_Value_Scalar::createFromNative($nativeValue);
  181.                 break;
  182.             case 'Datetime.iso8601':
  183.             case 'Datetime':    
  184.                 require_once 'XML/RPC2/Backend/Php/Value/Datetime.php';
  185.                 return new XML_RPC2_Backend_Php_Value_Datetime($nativeValue);
  186.                 break;
  187.             case 'Base64':
  188.                 require_once 'XML/RPC2/Backend/Php/Value/Base64.php';
  189.                 return new XML_RPC2_Backend_Php_Value_Base64($nativeValue);
  190.                 break;
  191.             case 'Array':
  192.                 require_once 'XML/RPC2/Backend/Php/Value/Array.php';
  193.                 return new XML_RPC2_Backend_Php_Value_Array($nativeValue);
  194.                 break;
  195.             case 'Struct':
  196.                 require_once 'XML/RPC2/Backend/Php/Value/Struct.php';
  197.                 return new XML_RPC2_Backend_Php_Value_Struct($nativeValue);
  198.                 break;
  199.             default:
  200.                 throw new XML_RPC2_InvalidTypeEncodeException(sprintf('Unexpected explicit encoding type \'%s\'', $explicitType));
  201.         }
  202.     }
  203.     
  204.     // }}}
  205.     // {{{ createFromDecode()
  206.     
  207.     /**
  208.      * Decode an encoded value and build the applicable XML_RPC2_Value subclass
  209.      * 
  210.      * @param SimpleXMLElement The encoded XML-RPC value
  211.      * @return mixed the corresponding XML_RPC2_Value object
  212.      */
  213.     public static function createFromDecode($simpleXML) 
  214.     {
  215.         // TODO Remove reparsing of XML fragment, when SimpleXML proves more solid. Currently it segfaults when
  216.         // xpath is used both in an element and in one of its children
  217.         $simpleXML = simplexml_load_string($simpleXML->asXML());
  218.         
  219.         $valueType = $simpleXML->xpath('./*');
  220.         if (count($valueType) == 1) { // Usually we must check the node name
  221.             $nodename = dom_import_simplexml($valueType[0])->nodeName;
  222.             switch ($nodename) {
  223.                 case 'i4':
  224.                 case 'int':
  225.                     $nativeType = 'Integer';
  226.                     break;
  227.                 case 'boolean':
  228.                     $nativeType = 'Boolean';
  229.                     break;
  230.                 case 'double':
  231.                     $nativeType = 'Double';
  232.                     break;
  233.                 case 'string':
  234.                     $nativeType = 'String';
  235.                     break;
  236.                 case 'dateTime.iso8601':
  237.                     $nativeType = 'Datetime';
  238.                     break;
  239.                 case 'base64':
  240.                     $nativeType = 'Base64';
  241.                     break;
  242.                 case 'array':
  243.                     $nativeType = 'Array';
  244.                     break;
  245.                 case 'struct':
  246.                     $nativeType = 'Struct';
  247.                     break;
  248.                 default:
  249.                     throw new XML_RPC2_DecodeException(sprintf('Unable to decode XML-RPC value. Value type is not recognized \'%s\'', $nodename));
  250.             }
  251.         } elseif (count($valueType) == 0) { // Default type is string
  252.             $nodename = null;
  253.             $nativeType = 'String';
  254.         } else {
  255.             throw new XML_RPC2_DecodeException(sprintf('Unable to decode XML-RPC value. Value presented %s type nodes: %s.', count($valueType), $simpleXML->asXML()));
  256.         }
  257.         require_once(sprintf('XML/RPC2/Backend/Php/Value/%s.php', $nativeType));
  258.         $nativeType = 'XML_RPC2_Backend_Php_Value_' . $nativeType;
  259.         return self::createFromNative(@call_user_func(array($nativeType, 'decode'), $simpleXML), $nodename);
  260.     }
  261.     
  262.     // }}}
  263.     // {{{ encode()
  264.         
  265.     /**
  266.      * Encode the instance into XML, for transport
  267.      * 
  268.      * @return string The encoded XML-RPC value,
  269.      */
  270.      // Declaration commented because of: http://pear.php.net/bugs/bug.php?id=8499
  271. //    public abstract function encode();
  272.     
  273.     // }}}
  274.     // {{{ decode()
  275.     
  276.     /**
  277.      * decode. Decode transport XML and set the instance value accordingly
  278.      * 
  279.      * @param mixed The encoded XML-RPC value,
  280.      */
  281.      // Declaration commented because of: http://pear.php.net/bugs/bug.php?id=8499
  282. //    public static abstract function decode($xml);
  283.     
  284.     // }}}
  285. }
  286.  
  287. ?>
  288.