- support for datetime objects and timestamps for xmrlcp datetimes;
authorggiunta <ggiunta@013ecfd8-0664-425d-a759-9c98391dc3f9>
Fri, 31 Jul 2009 22:13:02 +0000 (22:13 +0000)
committerggiunta <ggiunta@013ecfd8-0664-425d-a759-9c98391dc3f9>
Fri, 31 Jul 2009 22:13:02 +0000 (22:13 +0000)
- remove some more php 5.3 warnings;

git-svn-id: https://svn.code.sf.net/p/phpxmlrpc/code/trunk/xmlrpc@36 013ecfd8-0664-425d-a759-9c98391dc3f9

ChangeLog
lib/xmlrpc.inc
test/benchmark.php
test/testsuite.php

index 5d5c11a..706f959 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,16 @@
+2009-07-31 - G. Giunta (giunta.gaetano@gmail.com)\r
+\r
+       * xmlrpc.inc: add support for dateTime objects in both in php_xmlrpc_encode\r
+       and as parameter for constructor of xmlrpcvals; add support for timestamps\r
+       as parameter for constructor of xmlrpcvals; add option 'dates_as_objects' to\r
+       php_xmlrpc_decode to return dateTime objects for xmlrpc datetimes\r
+\r
+       * benchmark.php, xmlrpc_wrappers.inc: remove usage of split(), deprecated in\r
+       php 5.3\r
+\r
 2009-07-26 - G. Giunta (giunta.gaetano@gmail.com)\r
 \r
-       * serverphp: remove usage of ereg*(), deprecated in php 5.3\r
+       * server.php: remove usage of ereg*(), deprecated in php 5.3\r
 \r
 2009-07-16 - G. Giunta (giunta.gaetano@gmail.com) thanks Jean-Jacques Sarton\r
 \r
index a4c9ef2..8974fb2 100644 (file)
                                else\r
                                {\r
                                        $GLOBALS['_xh']['rt'] = strtolower($name);\r
+                                       $GLOBALS['_xh']['rt'] = strtolower($name);\r
                                }\r
                        }\r
                        else\r
 \r
                        if(!fputs($fp, $op, strlen($op)))\r
                        {\r
-                       fclose($fp);\r
+                               fclose($fp);\r
                                $this->errstr='Write error';\r
                                $r=new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['http_error'], $this->errstr);\r
                                return $r;\r
@@ -2187,7 +2188,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                * Given an open file handle, read all data available and parse it as axmlrpc response.\r
                * NB: the file handle is not closed by this function.\r
                * NNB: might have trouble in rare cases to work on network streams, as we\r
-        *      check for a read of 0 bytes instead of feof($fp).\r
+               *      check for a read of 0 bytes instead of feof($fp).\r
                *      But since checking for feof(null) returns false, we would risk an\r
                *      infinite loop in that case, because we cannot trust the caller\r
                *      to give us a valid pointer to an open file...\r
@@ -2757,8 +2758,8 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                        }\r
 \r
                        // coerce booleans into correct values\r
-                       // NB: we should iether do it for datetimes, integers and doubles, too,\r
-                       // or just plain remove this check, implemnted on booleans only...\r
+                       // NB: we should either do it for datetimes, integers and doubles, too,\r
+                       // or just plain remove this check, implemented on booleans only...\r
                        if($type==$GLOBALS['xmlrpcBoolean'])\r
                        {\r
                                if(strcasecmp($val,'true')==0 || $val==1 || ($val==true && strcasecmp($val,'false')))\r
@@ -2920,22 +2921,41 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                                                        $rs.="<${typ}>".(int)$val."</${typ}>";\r
                                                        break;\r
                                                case $GLOBALS['xmlrpcDouble']:\r
-                                               // avoid using standard conversion of float to string because it is locale-dependent,\r
-                                               // and also because the xmlrpc spec forbids exponential notation\r
-                                               // sprintf('%F') would be most likely ok but it is only available since PHP 4.3.10 and PHP 5.0.3.\r
-                                               // The code below tries its best at keeping max precision while avoiding exp notation,\r
-                                               // but there is of course no limit in the number of decimal places to be used...\r
-                                               $rs.="<${typ}>".preg_replace('/\\.?0+$/','',number_format((double)$val, 128, '.', ''))."</${typ}>";\r
+                                                       // avoid using standard conversion of float to string because it is locale-dependent,\r
+                                                       // and also because the xmlrpc spec forbids exponential notation\r
+                                                       // sprintf('%F') would be most likely ok but it is only available since PHP 4.3.10 and PHP 5.0.3.\r
+                                                       // The code below tries its best at keeping max precision while avoiding exp notation,\r
+                                                       // but there is of course no limit in the number of decimal places to be used...\r
+                                                       $rs.="<${typ}>".preg_replace('/\\.?0+$/','',number_format((double)$val, 128, '.', ''))."</${typ}>";\r
+                                                       break;\r
+                                               case $GLOBALS['xmlrpcDateTime']:\r
+                                                       if (is_string($val))\r
+                                                       {\r
+                                                               $rs.="<${typ}>${val}</${typ}>";\r
+                                                       }\r
+                                                       else if(is_a($val, 'DateTime'))\r
+                                                       {\r
+                                                               $rs.="<${typ}>".$val->format('Ymd\TH:i:s')."</${typ}>";\r
+                                                       }\r
+                                                       else if(is_int($val))\r
+                                                       {\r
+                                                               $rs.="<${typ}>".strftime("%Y%m%dT%H:%M:%S", $val)."</${typ}>";\r
+                                                       }\r
+                                                       else\r
+                                                       {\r
+                                                               // not really a good idea here: but what shall we output anyway? left for backward compat...\r
+                                                               $rs.="<${typ}>${val}</${typ}>";\r
+                                                       }\r
                                                        break;\r
                                                case $GLOBALS['xmlrpcNull']:\r
-                                                   if ($GLOBALS['xmlrpc_null_apache_encoding'])\r
-                                                   {\r
-                                                       $rs.="<ex:nil/>";\r
-                                                   }\r
-                                                   else\r
-                            {\r
-                                $rs.="<nil/>";\r
-                            }\r
+                                                       if ($GLOBALS['xmlrpc_null_apache_encoding'])\r
+                                                       {\r
+                                                               $rs.="<ex:nil/>";\r
+                                                       }\r
+                                                       else\r
+                                                       {\r
+                                                               $rs.="<nil/>";\r
+                                                       }\r
                                                        break;\r
                                                default:\r
                                                        // no standard type value should arrive here, but provide a possibility\r
@@ -3235,7 +3255,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
        * @author Dan Libby (dan@libby.com)\r
        *\r
        * @param xmlrpcval $xmlrpc_val\r
-       * @param array $options if 'decode_php_objs' is set in the options array, xmlrpc structs can be decoded into php objects\r
+       * @param array $options if 'decode_php_objs' is set in the options array, xmlrpc structs can be decoded into php objects; if 'dates_as_objects' is set xmlrpc datetimes are decoded as php DateTime objects (standard is\r
        * @return mixed\r
        */\r
        function php_xmlrpc_decode($xmlrpc_val, $options=array())\r
@@ -3262,6 +3282,27 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                                                        return $xmlrpc_val->scalarval();\r
                                        }\r
                                }\r
+                               if (in_array('dates_as_objects', $options) && $xmlrpc_val->scalartyp() == 'dateTime.iso8601')\r
+                               {\r
+                                       // we return a Datetime object instead of a string\r
+                                       // since now the constructor of xmlrpcval accepts safely strings, ints and datetimes,\r
+                                       // we cater to all 3 cases here\r
+                                       $out = $xmlrpc_val->scalarval();\r
+                                       if (is_string($out))\r
+                                       {\r
+                                               $out= strtotime($out);\r
+                                       }\r
+                                       if (is_int($out))\r
+                                       {\r
+                                               $result = new Datetime();\r
+                                               $result->setTimestamp($out);\r
+                                               return $result;\r
+                                       }\r
+                                       elseif (is_a($out, 'Datetime'))\r
+                                       {\r
+                                               return $out;\r
+                                       }\r
+                               }\r
                                return $xmlrpc_val->scalarval();\r
                        case 'array':\r
                                $size = $xmlrpc_val->arraysize();\r
@@ -3391,6 +3432,10 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                                {\r
                                        $xmlrpc_val = $php_val;\r
                                }\r
+                               else if(is_a($php_val, 'DateTime'))\r
+                               {\r
+                                       $xmlrpc_val = new xmlrpcval($php_val->format('Ymd\TH:i:s'), $GLOBALS['xmlrpcStruct']);\r
+                               }\r
                                else\r
                                {\r
                                        $arr = array();\r
@@ -3602,7 +3647,7 @@ xmlrpc_encode_entitites($this->errstr, $GLOBALS['xmlrpc_internalencoding'], $cha
                // Note: we do not check for invalid chars in VALUE:\r
                //   this had better be done using pure ereg as below\r
                // Note 2: we might be removing whitespace/tabs that ought to be left in if\r
-        //   the received charset is a quoted string. But nobody uses such charset names...\r
+               //   the received charset is a quoted string. But nobody uses such charset names...\r
 \r
                /// @todo this test will pass if ANY header has charset specification, not only Content-Type. Fix it?\r
                $matches = array();\r
index 7b58544..0238ce0 100644 (file)
        $msgs=array();
        for ($i = 0; $i < 25; $i++)
                $msgs[] = $msg;
-       $server = split(':', $LOCALSERVER);
+       $server = explode(':', $LOCALSERVER);
        if(count($server) > 1)
        {
                $c = new xmlrpc_client($URI, $server[0], $server[1]);
index 2918de8..2873698 100644 (file)
@@ -222,6 +222,20 @@ And turned it into nylon';
                        }
                }
 
+        function testDateTime()
+        {
+            $time = time();
+            $t1 = new xmlrpcval($time, 'dateTime.iso8601');
+            $t2 = new xmlrpcval(iso8601_encode($time), 'dateTime.iso8601');
+            $this->assertEquals($t1->serialize(), $t2->serialize());
+            if (class_exists('DateTime'))
+            {
+                $datetime = new DateTime();
+                $t3 = new xmlrpcval($datetime->setTimestamp($time), 'dateTime.iso8601');
+                $this->assertEquals($t1->serialize(), $t3->serialize());
+            }
+        }
+
                function testCountEntities()
                {
                        $sendstring = "h'fd>onc>>l>>rw&bpu>q>e<v&gxs<ytjzkami<";
@@ -1345,6 +1359,7 @@ $f = '<?xml version="1.0" encoding="utf-8"?><methodResponse><params><param><valu
        $suite->addTest(new LocalhostTests('testBoolean'));
        $suite->addTest(new LocalhostTests('testCountEntities'));
        $suite->addTest(new LocalhostTests('testBase64'));
+       $suite->addTest(new LocalhostTests('testDateTime'));
        $suite->addTest(new LocalhostTests('testServerMulticall'));
        $suite->addTest(new LocalhostTests('testClientMulticall1'));
        $suite->addTest(new LocalhostTests('testClientMulticall2'));