X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=src%2FEncoder.php;h=b015115a4a935008e6b3df41f811b56fe799235e;hb=ed70fcf801d85f7687e3381ad549bed634962828;hp=86e518e2bdbefcc534930d862b0b500bf90e1e65;hpb=cc67a43993662a5a3f92801b96b89ee6e3998532;p=plcapi.git diff --git a/src/Encoder.php b/src/Encoder.php index 86e518e..b015115 100644 --- a/src/Encoder.php +++ b/src/Encoder.php @@ -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,8 +37,8 @@ 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; @@ -60,21 +64,20 @@ 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->count(); $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; @@ -154,7 +157,7 @@ class Encoder // 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! @@ -177,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); @@ -208,6 +210,7 @@ class Encoder } else { $xmlrpcVal = new Value(); } + break; // catch "user function", "unknown type" default: // giancarlo pinerolo @@ -290,8 +293,10 @@ class Encoder case 'methodresponse': $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);