Completed
Push — master ( 4700f8...b32d7e )
by Tomasz
02:09
created

XmlFormat::serializeValue()   C

Complexity

Conditions 7
Paths 11

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 20
cts 20
cp 1
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 18
nc 11
nop 8
crap 7

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
namespace Thunder\Serializard\Format;
3
4
use Thunder\Serializard\NormalizerContainer\NormalizerContainerInterface as Normalizers;
5
use Thunder\Serializard\HydratorContainer\HydratorContainerInterface as Hydrators;
6
use Thunder\Serializard\NormalizerContext\NormalizerContextInterface;
7
8
/**
9
 * @author Tomasz Kowalczyk <[email protected]>
10
 */
11
final class XmlFormat implements FormatInterface
12
{
13
    /** @var callable */
14
    private $rootProvider;
15
16 10
    public function __construct(callable $rootProvider)
17
    {
18 10
        $this->rootProvider = $rootProvider;
19 10
    }
20
21 2
    public function serialize($var, Normalizers $normalizers, NormalizerContextInterface $context)
22
    {
23 2
        return $this->doSerialize($var, $normalizers, $context);
24
    }
25
26 2
    private function doSerialize($var, Normalizers $normalizers, NormalizerContextInterface $context, \DOMNode $doc = null, $parent = null, $key = null, array $state = array(), array $classes = array())
27
    {
28 2
        $isRoot = ($doc === null);
29 2
        $doc = $doc ?: new \DOMDocument('1.0', 'utf-8');
30 2
        $doc->formatOutput = true;
0 ignored issues
show
Bug introduced by
The property formatOutput does not seem to exist in DOMNode.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
31 2
        $parent = $parent ?: $doc;
32
33 2
        $this->serializeValue($var, $normalizers, $context, $doc, $parent, $key, $state, $classes);
34
35 2
        return $isRoot ? $doc->saveXML() : null;
36
    }
37
38 2
    private function serializeValue($var, Normalizers $normalizers, NormalizerContextInterface $context, \DOMNode $doc, $parent, $key, array $state = array(), array $classes = array())
39
    {
40
        /** @var \DOMDocument|\DOMElement $doc */
41
        /** @var \DOMDocument|\DOMElement $parent */
42 2
        if(is_object($var)) {
43 2
            $handler = $normalizers->getHandler(get_class($var));
44 2
            $arr = $handler($var);
45
46 2
            $hash = spl_object_hash($var);
47 2
            $classes[] = get_class($var);
48 2
            if(isset($state[$hash])) {
49 1
                throw new \RuntimeException('Nesting cycle: '.implode(' -> ', $classes));
50
            }
51 2
            $state[$hash] = 1;
52
53 2
            $this->doSerialize($arr, $normalizers, $context->withParent($var), $doc, $parent, $this->getRoot(get_class($var)), $state, $classes);
54
55 2
            return;
56
        }
57
58 2
        if(is_array($var)) {
59 2
            $item = $key ? $doc->createElement($key) : $parent;
60 2
            foreach($var as $index => $value) {
61 2
                $this->doSerialize($value, $normalizers, $context, $doc, $item, $index, $state, $classes);
62 2
            }
63 2
            !$key ?: $parent->appendChild($item);
64
65 2
            return;
66
        }
67
68 2
        $parent->appendChild($doc->createElement($key, $var));
69 2
    }
70
71 1
    public function unserialize($var, $class, Hydrators $hydrators)
72
    {
73 1
        $doc = new \DOMDocument();
74 1
        $doc->loadXML($var);
75 1
        $data = $this->parse($doc, $doc);
76 1
        $hydrator = $hydrators->getHandler($class);
77
78 1
        return $hydrator($data[$this->getRoot($class)], $hydrators);
79
    }
80
81 1
    private function parse(\DOMDocument $doc, $parent = null)
82
    {
83 1
        $ret = array();
84
        /** @var \DOMElement $parent */
85
        /** @var \DOMElement $node */
86 1
        $tags = array();
87 1
        foreach($parent->childNodes as $node) {
88 1
            if($node->nodeName === '#text') {
89 1
                continue;
90
            }
91 1
            if($node->childNodes->length === 1 && $node->childNodes->item(0) instanceof \DOMText) {
92 1
                $ret[$node->tagName] = $node->childNodes->item(0)->nodeValue;
93 1
                continue;
94
            }
95 1
            $result = $this->parse($doc, $node);
96 1
            if(array_key_exists($node->tagName, $ret)) {
97 1
                $tags[] = $node->tagName;
98 1
                $ret = array($ret[$node->tagName]);
99 1
                $ret[] = $result;
100 1
                continue;
101
            }
102 1
            if(in_array($node->tagName, $tags, true)) {
103 1
                $ret[] = $result;
104 1
                continue;
105
            }
106 1
            $ret[$node->tagName] = $result;
107 1
        }
108
109 1
        return $ret;
110
    }
111
112 2
    private function getRoot($class)
113
    {
114 2
        return call_user_func($this->rootProvider, $class);
115
    }
116
}
117