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

ZendParser::parse()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 35
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
dl 0
loc 35
ccs 0
cts 26
cp 0
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 19
nc 5
nop 1
crap 56
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