1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fxmlrpc\Serialization\Serializer; |
4
|
|
|
|
5
|
|
|
use Fxmlrpc\Serialization\Exception\InvalidTypeException; |
6
|
|
|
use Fxmlrpc\Serialization\Exception\SerializerException; |
7
|
|
|
use Fxmlrpc\Serialization\Serializer; |
8
|
|
|
use Fxmlrpc\Serialization\Value\Base64; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Serializer creates XML from native PHP types using XML RPC extension. |
12
|
|
|
* |
13
|
|
|
* @author Márk Sági-Kazár <[email protected]> |
14
|
|
|
*/ |
15
|
|
View Code Duplication |
final class Zend1Serializer implements Serializer |
|
|
|
|
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* {@inheritdoc} |
19
|
|
|
*/ |
20
|
|
|
public function serialize($method, array $params = []) |
21
|
|
|
{ |
22
|
|
|
$toBeVisited = [&$params]; |
23
|
|
|
|
24
|
|
|
while (isset($toBeVisited[0]) && $value = &$toBeVisited[0]) { |
25
|
|
|
$type = gettype($value); |
26
|
|
|
|
27
|
|
|
if ($type === 'array') { |
28
|
|
|
// Zend converts non-zero-indexed arrays to structs |
29
|
|
|
if ((array_keys($value) !== range(0, count($value) - 1)) && (array_keys($value) == range(1, count($value)))) { |
30
|
|
|
$value = array_values($value); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
foreach ($value as &$child) { |
34
|
|
|
$toBeVisited[] = &$child; |
35
|
|
|
} |
36
|
|
|
} elseif ($type === 'object') { |
37
|
|
|
if ($value instanceof \DateTime) { |
38
|
|
|
$value = \Zend_XmlRpc_Value::getXmlRpcValue($value->format('Ymd\TH:i:s'), \Zend_XmlRpc_Value::XMLRPC_TYPE_DATETIME); |
39
|
|
|
} elseif ($value instanceof Base64) { |
40
|
|
|
$value = \Zend_XmlRpc_Value::getXmlRpcValue($value->getDecoded(), \Zend_XmlRpc_Value::XMLRPC_TYPE_BASE64); |
41
|
|
|
} else { |
42
|
|
|
$value = get_object_vars($value); |
43
|
|
|
} |
44
|
|
|
} elseif ($type === 'resource') { |
45
|
|
|
throw new InvalidTypeException($value); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
array_shift($toBeVisited); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$request = new \Zend_XmlRpc_Request($method, $params); |
52
|
|
|
|
53
|
|
|
try { |
54
|
|
|
return $request->saveXml(); |
55
|
|
|
} catch (\Exception $e) { |
56
|
|
|
throw new SerializerException($e->getMessage(), $e->getCode(), $e); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.