web/lib/Zend/XmlRpc/Value.php
changeset 807 877f952ae2bd
parent 207 621fa6caec0c
child 1230 68c69c656a2c
equal deleted inserted replaced
805:5e7a0fedabdf 807:877f952ae2bd
    13  * to license@zend.com so we can send you a copy immediately.
    13  * to license@zend.com so we can send you a copy immediately.
    14  *
    14  *
    15  * @category   Zend
    15  * @category   Zend
    16  * @package    Zend_XmlRpc
    16  * @package    Zend_XmlRpc
    17  * @subpackage Value
    17  * @subpackage Value
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    18  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    20  * @version    $Id: Value.php 22024 2010-04-27 18:08:24Z matthew $
    20  * @version    $Id: Value.php 24593 2012-01-05 20:35:02Z matthew $
    21  */
    21  */
    22 
    22 
    23 /**
    23 /**
    24  * Represent a native XML-RPC value entity, used as parameters for the methods
    24  * Represent a native XML-RPC value entity, used as parameters for the methods
    25  * called by the Zend_XmlRpc_Client object and as the return value for those calls.
    25  * called by the Zend_XmlRpc_Client object and as the return value for those calls.
    29  *
    29  *
    30  * Using this function, users/Zend_XmlRpc_Client object can create the Zend_XmlRpc_Value objects
    30  * Using this function, users/Zend_XmlRpc_Client object can create the Zend_XmlRpc_Value objects
    31  * from PHP variables, XML string or by specifing the exact XML-RPC natvie type
    31  * from PHP variables, XML string or by specifing the exact XML-RPC natvie type
    32  *
    32  *
    33  * @package    Zend_XmlRpc
    33  * @package    Zend_XmlRpc
    34  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    34  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    35  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    35  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    36  */
    36  */
    37 abstract class Zend_XmlRpc_Value
    37 abstract class Zend_XmlRpc_Value
    38 {
    38 {
    39     /**
    39     /**
   250                 require_once 'Zend/XmlRpc/Value/Exception.php';
   250                 require_once 'Zend/XmlRpc/Value/Exception.php';
   251                 throw new Zend_XmlRpc_Value_Exception('Given type is not a '. __CLASS__ .' constant');
   251                 throw new Zend_XmlRpc_Value_Exception('Given type is not a '. __CLASS__ .' constant');
   252         }
   252         }
   253     }
   253     }
   254 
   254 
       
   255     /**
       
   256      * Get XML-RPC type for a PHP native variable
       
   257      *
       
   258      * @static
       
   259      * @param mixed $value
       
   260      * @return string
       
   261      */
       
   262     public static function getXmlRpcTypeByValue($value)
       
   263     {
       
   264         if (is_object($value)) {
       
   265             if ($value instanceof Zend_XmlRpc_Value) {
       
   266                 return $value->getType();
       
   267             } elseif (($value instanceof Zend_Date) || ($value instanceof DateTime)) {
       
   268                 return self::XMLRPC_TYPE_DATETIME;
       
   269             }
       
   270             return self::getXmlRpcTypeByValue(get_object_vars($value));
       
   271         } elseif (is_array($value)) {
       
   272             if (!empty($value) && is_array($value) && (array_keys($value) !== range(0, count($value) - 1))) {
       
   273                 return self::XMLRPC_TYPE_STRUCT;
       
   274             }
       
   275             return self::XMLRPC_TYPE_ARRAY;
       
   276         } elseif (is_int($value)) {
       
   277             return ($value > PHP_INT_MAX) ? self::XMLRPC_TYPE_I8 : self::XMLRPC_TYPE_INTEGER;
       
   278         } elseif (is_double($value)) {
       
   279             return self::XMLRPC_TYPE_DOUBLE;
       
   280         } elseif (is_bool($value)) {
       
   281             return self::XMLRPC_TYPE_BOOLEAN;
       
   282         } elseif (is_null($value)) {
       
   283             return self::XMLRPC_TYPE_NIL;
       
   284         } elseif (is_string($value)) {
       
   285             return self::XMLRPC_TYPE_STRING;
       
   286         }
       
   287         throw new Zend_XmlRpc_Value_Exception(sprintf(
       
   288             'No matching XMLRPC type found for php type %s.',
       
   289             gettype($value)
       
   290         ));
       
   291     }
   255 
   292 
   256     /**
   293     /**
   257      * Transform a PHP native variable into a XML-RPC native value
   294      * Transform a PHP native variable into a XML-RPC native value
   258      *
   295      *
   259      * @param mixed $value The PHP variable for convertion
   296      * @param mixed $value The PHP variable for convertion
   261      * @return Zend_XmlRpc_Value
   298      * @return Zend_XmlRpc_Value
   262      * @static
   299      * @static
   263      */
   300      */
   264     protected static function _phpVarToNativeXmlRpc($value)
   301     protected static function _phpVarToNativeXmlRpc($value)
   265     {
   302     {
   266         switch (gettype($value)) {
   303         // @see http://framework.zend.com/issues/browse/ZF-8623
   267             case 'object':
   304         if (is_object($value)) {
   268                 // Check to see if it's an XmlRpc value
   305             if ($value instanceof Zend_XmlRpc_Value) {
   269                 if ($value instanceof Zend_XmlRpc_Value) {
   306                 return $value;
   270                     return $value;
   307             }
   271                 }
   308             if ($value instanceof Zend_Crypt_Math_BigInteger) {
   272 
   309                 require_once 'Zend/XmlRpc/Value/Exception.php';
   273                 if ($value instanceof Zend_Crypt_Math_BigInteger) {
   310                 throw new Zend_XmlRpc_Value_Exception(
   274                     require_once 'Zend/XmlRpc/Value/BigInteger.php';
   311                     'Using Zend_Crypt_Math_BigInteger to get an ' .
   275                     return new Zend_XmlRpc_Value_BigInteger($value);
   312                     'instance of Zend_XmlRpc_Value_BigInteger is not ' .
   276                 }
   313                     'available anymore.'
   277 
   314                 );
   278                 if ($value instanceof Zend_Date or $value instanceof DateTime) {
   315             }
   279                     require_once 'Zend/XmlRpc/Value/DateTime.php';
   316         }
   280                     return new Zend_XmlRpc_Value_DateTime($value);
   317 
   281                 }
   318         switch (self::getXmlRpcTypeByValue($value))
   282 
   319         {
   283                 // Otherwise, we convert the object into a struct
   320             case self::XMLRPC_TYPE_DATETIME:
   284                 $value = get_object_vars($value);
   321                 require_once 'Zend/XmlRpc/Value/DateTime.php';
   285                 // Break intentionally omitted
   322                 return new Zend_XmlRpc_Value_DateTime($value);
   286             case 'array':
   323 
   287                 // Default native type for a PHP array (a simple numeric array) is 'array'
   324             case self::XMLRPC_TYPE_ARRAY:
   288                 require_once 'Zend/XmlRpc/Value/Array.php';
   325                 require_once 'Zend/XmlRpc/Value/Array.php';
   289                 $obj = 'Zend_XmlRpc_Value_Array';
   326                 return new Zend_XmlRpc_Value_Array($value);
   290 
   327 
   291                 // Determine if this is an associative array
   328             case self::XMLRPC_TYPE_STRUCT:
   292                 if (!empty($value) && is_array($value) && (array_keys($value) !== range(0, count($value) - 1))) {
   329                 require_once 'Zend/XmlRpc/Value/Struct.php';
   293                     require_once 'Zend/XmlRpc/Value/Struct.php';
   330                 return new Zend_XmlRpc_Value_Struct($value);
   294                     $obj = 'Zend_XmlRpc_Value_Struct';
   331 
   295                 }
   332             case self::XMLRPC_TYPE_INTEGER:
   296                 return new $obj($value);
       
   297 
       
   298             case 'integer':
       
   299                 require_once 'Zend/XmlRpc/Value/Integer.php';
   333                 require_once 'Zend/XmlRpc/Value/Integer.php';
   300                 return new Zend_XmlRpc_Value_Integer($value);
   334                 return new Zend_XmlRpc_Value_Integer($value);
   301 
   335 
   302             case 'double':
   336             case self::XMLRPC_TYPE_DOUBLE:
   303                 require_once 'Zend/XmlRpc/Value/Double.php';
   337                 require_once 'Zend/XmlRpc/Value/Double.php';
   304                 return new Zend_XmlRpc_Value_Double($value);
   338                 return new Zend_XmlRpc_Value_Double($value);
   305 
   339 
   306             case 'boolean':
   340             case self::XMLRPC_TYPE_BOOLEAN:
   307                 require_once 'Zend/XmlRpc/Value/Boolean.php';
   341                 require_once 'Zend/XmlRpc/Value/Boolean.php';
   308                 return new Zend_XmlRpc_Value_Boolean($value);
   342                 return new Zend_XmlRpc_Value_Boolean($value);
   309 
   343 
   310             case 'NULL':
   344             case self::XMLRPC_TYPE_NIL:
   311             case 'null':
       
   312                 require_once 'Zend/XmlRpc/Value/Nil.php';
   345                 require_once 'Zend/XmlRpc/Value/Nil.php';
   313                 return new Zend_XmlRpc_Value_Nil();
   346                 return new Zend_XmlRpc_Value_Nil;
   314 
   347 
   315             case 'string':
   348             case self::XMLRPC_TYPE_STRING:
   316                 // Fall through to the next case
   349                 // Fall through to the next case
   317             default:
   350             default:
   318                 // If type isn't identified (or identified as string), it treated as string
   351                 // If type isn't identified (or identified as string), it treated as string
   319                 require_once 'Zend/XmlRpc/Value/String.php';
   352                 require_once 'Zend/XmlRpc/Value/String.php';
   320                 return new Zend_XmlRpc_Value_String($value);
   353                 return new Zend_XmlRpc_Value_String($value);
   472             $type = self::XMLRPC_TYPE_STRING;
   505             $type = self::XMLRPC_TYPE_STRING;
   473         }
   506         }
   474     }
   507     }
   475 
   508 
   476     /**
   509     /**
   477      * @param $xml
   510      * @param string $xml
   478      * @return void
   511      * @return void
   479      */
   512      */
   480     protected function _setXML($xml)
   513     protected function _setXML($xml)
   481     {
   514     {
   482         $this->_xml = $this->getGenerator()->stripDeclaration($xml);
   515         $this->_xml = $this->getGenerator()->stripDeclaration($xml);