Passed
Pull Request — master (#27)
by Dominik
02:13
created

JsonxTypeDecoder::decodeNumberNode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Deserialization\Decoder;
6
7
use Chubbyphp\Deserialization\DeserializerRuntimeException;
8
9
/**
10
 * @link https://www.ibm.com/support/knowledgecenter/SS9H2Y_7.6.0/com.ibm.dp.doc/json_jsonx.html
11
 */
12
final class JsonxTypeDecoder implements TypeDecoderInterface
13
{
14
    const DATATYPE_OBJECT = 'object';
15
    const DATATYPE_ARRAY = 'array';
16
    const DATATYPE_BOOLEAN = 'boolean';
17
    const DATATYPE_STRING = 'string';
18
    const DATATYPE_NUMBER = 'number';
19
    const DATATYPE_NULL = 'null';
20
21
    /**
22
     * @return string
23
     */
24 4
    public function getContentType(): string
25
    {
26 4
        return 'application/jsonx';
27
    }
28
29
    /**
30
     * @param string $data
31
     *
32
     * @return array
33
     *
34
     * @throws DeserializerRuntimeException
35
     */
36 5
    public function decode(string $data): array
37
    {
38 5
        $document = new \DOMDocument();
39
40 5
        if (!@$document->loadXML($data)) {
41 1
            throw DeserializerRuntimeException::createNotParsable($this->getContentType());
42
        }
43
44 4
        return $this->decodeNode($document->documentElement);
45
    }
46
47
    /**
48
     * @param \DOMNode $node
49
     * @return array|bool|string|int|float|null
50
     */
51 4
    private function decodeNode(\DOMNode $node)
52
    {
53 4
        $nodeName = $node->nodeName;
54
55 4
        if (0 !== strpos($nodeName, 'json:')) {
56 1
            throw DeserializerRuntimeException::createNotParsable($this->getContentType());
57
        }
58
59 3
        $nodeType = substr($nodeName, 5);
60
61 3
        if (self::DATATYPE_OBJECT === $nodeType) {
62 2
            return $this->decodeObjectNode($node);
63
        }
64
65 3
        if (self::DATATYPE_ARRAY === $nodeType) {
66 1
            return $this->decodeArrayNode($node);
67
        }
68
69 3
        if (self::DATATYPE_BOOLEAN === $nodeType) {
70 2
            return $this->decodeBooleanNode($node);
71
        }
72
73 3
        if (self::DATATYPE_STRING === $nodeType) {
74 2
            return $this->decodeStringNode($node);
75
        }
76
77 3
        if (self::DATATYPE_NUMBER === $nodeType) {
78 2
            return $this->decodeNumberNode($node);
79
        }
80
81 2
        if (self::DATATYPE_NULL === $nodeType) {
82 1
            return null;
83
        }
84
85 1
        throw DeserializerRuntimeException::createNotParsable($this->getContentType());
86
    }
87
88
    /**
89
     * @param \DOMNode $node
90
     * @return array
91
     */
92 2 View Code Duplication
    private function decodeObjectNode(\DOMNode $node): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
93
    {
94 2
        $data = [];
95 2
        foreach ($node->childNodes as $childNode) {
96 2
            if ($childNode instanceof \DOMText) {
97 2
                continue;
98
            }
99
100 2
            $data[$childNode->getAttribute('name')] = $this->decodeNode($childNode);
101
        }
102
103 2
        return $data;
104
    }
105
106
    /**
107
     * @param \DOMNode $node
108
     * @return array
109
     */
110 1 View Code Duplication
    private function decodeArrayNode(\DOMNode $node): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
111
    {
112 1
        $data = [];
113 1
        foreach ($node->childNodes as $childNode) {
114 1
            if ($childNode instanceof \DOMText) {
115 1
                continue;
116
            }
117
118 1
            $data[] = $this->decodeNode($childNode);
119
        }
120
121 1
        return $data;
122
    }
123
124
    /**
125
     * @param \DOMNode $node
126
     * @return bool
127
     */
128 2
    private function decodeBooleanNode(\DOMNode $node): bool
129
    {
130 2
        return $node->nodeValue === 'true';
131
    }
132
133
    /**
134
     * @param \DOMNode $node
135
     * @return string
136
     */
137 2
    private function decodeStringNode(\DOMNode $node): string
138
    {
139 2
        return html_entity_decode($node->nodeValue, ENT_COMPAT | ENT_XML1, 'UTF-8');
140
    }
141
142
    /**
143
     * @param \DOMNoDOMNodedeList $node
144
     * @return int|float
145
     */
146 2
    private function decodeNumberNode(\DOMNode $node)
147
    {
148 2
        $value = $node->nodeValue;
149
150 2
        if ($value === (string) (int) $value) {
151 2
            return (int) $value;
152
        }
153
154 2
        return (float) $value;
155
    }
156
}
157