Completed
Push — master ( b8dc6e...0bbbbb )
by Márk
9s
created

ZendParser   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 3
dl 0
loc 41
ccs 0
cts 26
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C parse() 0 35 7
1
<?php
2
3
namespace Fxmlrpc\Serialization\Parser;
4
5
use Fxmlrpc\Serialization\Exception\FaultException;
6
use Fxmlrpc\Serialization\Exception\ParserException;
7
use Fxmlrpc\Serialization\Parser;
8
use Zend\XmlRpc\Response;
9
10
/**
11
 * Parser to parse XML responses into its PHP representation using XML RPC extension.
12
 *
13
 * @author Márk Sági-Kazár <[email protected]>
14
 */
15
final class ZendParser implements Parser
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20
    public function parse($xmlString)
21
    {
22
        $response = new Response();
23
24
        try {
25
            $response->loadXml($xmlString);
26
        } catch (\Exception $e) {
27
            throw new ParserException($e->getMessage(), $e->getCode(), $e);
28
        }
29
30
        if ($response->isFault()) {
31
            $fault = $response->getFault();
32
33
            throw new FaultException($fault->getMessage(), $fault->getCode());
34
        }
35
36
        $result = $response->getReturnValue();
37
38
        $toBeVisited = [&$result];
39
40
        while (isset($toBeVisited[0]) && $value = &$toBeVisited[0]) {
41
            $type = gettype($value);
42
            if ($type === 'array') {
43
                foreach ($value as &$element) {
44
                    $toBeVisited[] = &$element;
45
                }
46
47
                reset($value); // Reset all array pointers
48
            }
49
50
            array_shift($toBeVisited);
51
        }
52
53
        return $result;
54
    }
55
}
56