Passed
Push — master ( 62918c...fbb71b )
by Dominik
02:02
created

XmlTransformer   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 90.48%

Importance

Changes 0
Metric Value
wmc 18
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 115
ccs 38
cts 42
cp 0.9048
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A transform() 0 13 1
B dataToNodes() 0 22 6
B getValueAsString() 0 12 5
B getType() 0 20 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Serialization\Transformer;
6
7
use Doctrine\Common\Inflector\Inflector;
8
9
final class XmlTransformer implements TransformerInterface
10
{
11
    /**
12
     * @var bool
13
     */
14
    private $formatOutput;
15
16
    /**
17
     * @var int
18
     */
19
    private $options;
20
21
    /**
22
     * @param bool $formatOutput
23
     * @param int  $options
24
     */
25 1
    public function __construct(bool $formatOutput = false, int $options = LIBXML_NOEMPTYTAG)
26
    {
27 1
        $this->formatOutput = $formatOutput;
28 1
        $this->options = $options;
29 1
    }
30
31
    /**
32
     * @param array $data
33
     *
34
     * @return string
35
     */
36 1
    public function transform(array $data): string
37
    {
38 1
        $document = new \DOMDocument('1.0', 'UTF-8');
39 1
        $document->formatOutput = $this->formatOutput;
40
41 1
        $responseNode = $document->createElement('response');
42
43 1
        $document->appendChild($responseNode);
44
45 1
        $this->dataToNodes($document, $responseNode, $data);
46
47 1
        return trim($document->saveXML(null, $this->options));
48
    }
49
50
    /**
51
     * @param \DOMDocument $document
52
     * @param \DOMNode     $listNode
53
     * @param array        $data
54
     */
55 1
    private function dataToNodes(\DOMDocument $document, \DOMNode $listNode, array $data)
56
    {
57 1
        foreach ($data as $key => $value) {
58 1
            if (is_array($value)) {
59 1
                $childNode = $document->createElement(is_int($key) ? Inflector::singularize($listNode->nodeName) : $key);
60 1
                $this->dataToNodes($document, $childNode, $value);
61
            } else {
62 1
                if (is_string($value)) {
63 1
                    $childNode = $document->createElement($key);
64 1
                    $childNode->appendChild($document->createCDATASection($value));
65 1
                    $childNode->setAttribute('type', 'string');
66 1
                } elseif (null === $value) {
67 1
                    $childNode = $document->createElement($key);
68
                } else {
69 1
                    $childNode = $document->createElement($key, $this->getValueAsString($value));
70 1
                    $childNode->setAttribute('type', $this->getType($value));
71
                }
72
            }
73
74 1
            $listNode->appendChild($childNode);
75
        }
76 1
    }
77
78
    /**
79
     * @param bool|float|int $value
80
     *
81
     * @return string|null
82
     *
83
     * @throws \InvalidArgumentException
84
     */
85 1
    private function getValueAsString($value): string
86
    {
87 1
        if (is_bool($value)) {
88 1
            return $value ? 'true' : 'false';
89
        }
90
91 1
        if (is_float($value) || is_int($value)) {
92 1
            return (string) $value;
93
        }
94
95
        throw new \InvalidArgumentException(sprintf('Unsupported data type: %s', gettype($value)));
96
    }
97
98
    /**
99
     * @param bool|float|int|string $value
100
     *
101
     * @return null|string
102
     */
103 1
    private function getType($value): string
104
    {
105 1
        if (is_bool($value)) {
106 1
            return 'boolean';
107
        }
108
109 1
        if (is_float($value)) {
110 1
            return 'float';
111
        }
112
113 1
        if (is_int($value)) {
114 1
            return 'integer';
115
        }
116
117
        if (is_string($value)) {
118
            return 'string';
119
        }
120
121
        throw new \InvalidArgumentException(sprintf('Unsupported data type: %s', gettype($value)));
122
    }
123
}
124