Passed
Push — master ( 40ff2a...9a8bc4 )
by Dominik
02:05
created

XmlFormatter::dataToNodes()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 15
cts 15
cp 1
rs 8.6737
c 0
b 0
f 0
cc 6
eloc 16
nc 5
nop 3
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Serialization\Formatter;
6
7
use Doctrine\Common\Inflector\Inflector;
8
9
final class XmlFormatter implements FormatterInterface
10
{
11
    /**
12
     * @var bool
13
     */
14
    private $formatOutput;
15
16
    /**
17
     * @param bool $formatOutput
18
     */
19 1
    public function __construct(bool $formatOutput = false)
20
    {
21 1
        $this->formatOutput = $formatOutput;
22 1
    }
23
24
    /**
25
     * @param array $data
26
     *
27
     * @return string
28
     */
29 1
    public function format(array $data): string
30
    {
31 1
        $document = new \DOMDocument('1.0', 'UTF-8');
32 1
        $document->formatOutput = $this->formatOutput;
33
34 1
        $this->dataToNodes($document, $document, $data);
35
36 1
        return trim($document->saveXML());
37
    }
38
39
    /**
40
     * @param \DOMDocument $document
41
     * @param \DOMNode     $listNode
42
     * @param array        $data
43
     */
44 1
    private function dataToNodes(\DOMDocument $document, \DOMNode $listNode, array $data)
45
    {
46 1
        foreach ($data as $key => $value) {
47 1
            if (is_array($value)) {
48 1
                $childNode = $document->createElement(is_int($key) ? Inflector::singularize($listNode->nodeName) : $key);
49 1
                $this->dataToNodes($document, $childNode, $value);
50
            } else {
51 1
                if (is_string($value)) {
52 1
                    $childNode = $document->createElement($key);
53 1
                    $childNode->appendChild($document->createCDATASection($value));
54 1
                    $childNode->setAttribute('type', 'string');
55 1
                } elseif (null === $value) {
56 1
                    $childNode = $document->createElement($key);
57
                } else {
58 1
                    $childNode = $document->createElement($key, $this->getValueAsString($value));
59 1
                    $childNode->setAttribute('type', $this->getType($value));
60
                }
61
            }
62
63 1
            $listNode->appendChild($childNode);
64
        }
65 1
    }
66
67
    /**
68
     * @param bool|float|int $value
69
     * @return string|null
70
     * @throws \InvalidArgumentException
71
     */
72 1
    private function getValueAsString($value): string
73
    {
74 1
        if (is_bool($value)) {
75 1
            return $value ? 'true' : 'false';
76
        }
77
78 1
        if (is_float($value) || is_int($value)) {
79 1
            return (string) $value;
80
        }
81
82
        throw new \InvalidArgumentException(sprintf('Unsupported data type: %s', gettype($value)));
83
    }
84
85
    /**
86
     * @param bool|float|int|string $value
87
     * @return null|string
88
     */
89 1
    private function getType($value): string
90
    {
91 1
        if (is_bool($value)) {
92 1
            return 'boolean';
93
        }
94
95 1
        if (is_float($value)) {
96 1
            return 'float';
97
        }
98
99 1
        if (is_int($value)) {
100 1
            return 'integer';
101
        }
102
103
        if (is_string($value)) {
104
            return 'string';
105
        }
106
107
        throw new \InvalidArgumentException(sprintf('Unsupported data type: %s', gettype($value)));
108
    }
109
}
110