One more fix for issue #55
[plcapi.git] / src / Encoder.php
index 2955f44..ebf3060 100644 (file)
@@ -4,6 +4,10 @@ namespace PhpXmlRpc;
 
 use PhpXmlRpc\Helper\XMLParser;
 
+/**
+ * A helper class to easily convert between Value objects and php native values
+ * @todo implement an interface
+ */
 class Encoder
 {
     /**
@@ -33,13 +37,13 @@ class Encoder
         switch ($xmlrpcVal->kindOf()) {
             case 'scalar':
                 if (in_array('extension_api', $options)) {
-                    reset($xmlrpcVal->me);
-                    list($typ, $val) = each($xmlrpcVal->me);
+                    $val = reset($xmlrpcVal->me);
+                    $typ = key($xmlrpcVal->me);
                     switch ($typ) {
                         case 'dateTime.iso8601':
                             $xmlrpcVal->scalar = $val;
                             $xmlrpcVal->type = 'datetime';
-                            $xmlrpcVal->timestamp = \PhpXmlRpc\Helper\Date::iso8601_decode($val);
+                            $xmlrpcVal->timestamp = \PhpXmlRpc\Helper\Date::iso8601Decode($val);
 
                             return $xmlrpcVal;
                         case 'base64':
@@ -60,26 +64,24 @@ class Encoder
                         $out = strtotime($out);
                     }
                     if (is_int($out)) {
-                        $result = new \Datetime();
+                        $result = new \DateTime();
                         $result->setTimestamp($out);
 
                         return $result;
-                    } elseif (is_a($out, 'Datetime')) {
+                    } elseif (is_a($out, 'DateTimeInterface')) {
                         return $out;
                     }
                 }
 
                 return $xmlrpcVal->scalarval();
             case 'array':
-                $size = $xmlrpcVal->arraysize();
                 $arr = array();
-                for ($i = 0; $i < $size; $i++) {
-                    $arr[] = $this->decode($xmlrpcVal->arraymem($i), $options);
+                foreach($xmlrpcVal as $value) {
+                    $arr[] = $this->decode($value, $options);
                 }
 
                 return $arr;
             case 'struct':
-                $xmlrpcVal->structreset();
                 // If user said so, try to rebuild php objects for specific struct vals.
                 /// @todo should we raise a warning for class not found?
                 // shall we check for proper subclass of xmlrpc value instead of
@@ -88,14 +90,14 @@ class Encoder
                     && class_exists($xmlrpcVal->_php_class)
                 ) {
                     $obj = @new $xmlrpcVal->_php_class();
-                    while (list($key, $value) = $xmlrpcVal->structeach()) {
+                    foreach ($xmlrpcVal as $key => $value) {
                         $obj->$key = $this->decode($value, $options);
                     }
 
                     return $obj;
                 } else {
                     $arr = array();
-                    while (list($key, $value) = $xmlrpcVal->structeach()) {
+                    foreach ($xmlrpcVal as $key => $value) {
                         $arr[$key] = $this->decode($value, $options);
                     }
 
@@ -105,7 +107,7 @@ class Encoder
                 $paramCount = $xmlrpcVal->getNumParams();
                 $arr = array();
                 for ($i = 0; $i < $paramCount; $i++) {
-                    $arr[] = $this->decode($xmlrpcVal->getParam($i));
+                    $arr[] = $this->decode($xmlrpcVal->getParam($i), $options);
                 }
 
                 return $arr;
@@ -155,7 +157,7 @@ class Encoder
             // </G_Giunta_2001-02-29>
             case 'array':
                 // PHP arrays can be encoded to either xmlrpc structs or arrays,
-                // depending on wheter they are hashes or plain 0..n integer indexed
+                // depending on whether they are hashes or plain 0..n integer indexed
                 // A shorter one-liner would be
                 // $tmp = array_diff(array_keys($phpVal), range(0, count($phpVal)-1));
                 // but execution time skyrockets!
@@ -178,12 +180,11 @@ class Encoder
             case 'object':
                 if (is_a($phpVal, 'PhpXmlRpc\Value')) {
                     $xmlrpcVal = $phpVal;
-                } elseif (is_a($phpVal, 'DateTime')) {
+                } elseif (is_a($phpVal, 'DateTimeInterface')) {
                     $xmlrpcVal = new Value($phpVal->format('Ymd\TH:i:s'), Value::$xmlrpcStruct);
                 } else {
                     $arr = array();
-                    reset($phpVal);
-                    while (list($k, $v) = each($phpVal)) {
+                    foreach($phpVal as $k => $v) {
                         $arr[$k] = $this->encode($v, $options);
                     }
                     $xmlrpcVal = new Value($arr, Value::$xmlrpcStruct);
@@ -209,11 +210,11 @@ class Encoder
                 } else {
                     $xmlrpcVal = new Value();
                 }
+                break;
             // catch "user function", "unknown type"
             default:
                 // giancarlo pinerolo <ping@alt.it>
-                // it has to return
-                // an empty object in case, not a boolean.
+                // it has to return an empty object in case, not a boolean.
                 $xmlrpcVal = new Value();
                 break;
         }
@@ -225,12 +226,14 @@ class Encoder
      * Convert the xml representation of a method response, method request or single
      * xmlrpc value into the appropriate object (a.k.a. deserialize).
      *
+     * Q: is this a good name for this method? It does something quite different from 'decode' after all (returning objects vs returns plain php values)...
+     *
      * @param string $xmlVal
      * @param array $options
      *
      * @return mixed false on error, or an instance of either Value, Request or Response
      */
-    public function decode_xml($xmlVal, $options = array())
+    public function decodeXml($xmlVal, $options = array())
     {
         // 'guestimate' encoding
         $valEncoding = XMLParser::guessEncoding('', $xmlVal);
@@ -244,8 +247,7 @@ class Encoder
             if (!in_array($valEncoding, array('UTF-8', 'US-ASCII')) && !XMLParser::hasEncoding($xmlVal)) {
                 if ($valEncoding == 'ISO-8859-1') {
                     $xmlVal = utf8_encode($xmlVal);
-                }
-                else {
+                } else {
                     if (extension_loaded('mbstring')) {
                         $xmlVal = mb_convert_encoding($xmlVal, 'UTF-8', $valEncoding);
                     } else {
@@ -290,10 +292,12 @@ class Encoder
         }
         switch ($xmlRpcParser->_xh['rt']) {
             case 'methodresponse':
-                $v = &$xmlRpcParser->_xh['value'];
+                $v = $xmlRpcParser->_xh['value'];
                 if ($xmlRpcParser->_xh['isf'] == 1) {
-                    $vc = $v->structmem('faultCode');
-                    $vs = $v->structmem('faultString');
+                    /** @var Value $vc */
+                    $vc = $v['faultCode'];
+                    /** @var Value $vs */
+                    $vs = $v['faultString'];
                     $r = new Response(0, $vc->scalarval(), $vs->scalarval());
                 } else {
                     $r = new Response($v);
@@ -301,12 +305,12 @@ class Encoder
 
                 return $r;
             case 'methodcall':
-                $m = new Request($xmlRpcParser->_xh['method']);
+                $req = new Request($xmlRpcParser->_xh['method']);
                 for ($i = 0; $i < count($xmlRpcParser->_xh['params']); $i++) {
-                    $m->addParam($xmlRpcParser->_xh['params'][$i]);
+                    $req->addParam($xmlRpcParser->_xh['params'][$i]);
                 }
 
-                return $m;
+                return $req;
             case 'value':
                 return $xmlRpcParser->_xh['value'];
             default: