Completed
Push — master ( c16c79...ab8250 )
by Edward
04:36
created

ValueDecoder::exportValue()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 13
c 0
b 0
f 0
nc 6
nop 1
dl 0
loc 25
ccs 0
cts 14
cp 0
crap 42
rs 9.2222
1
<?php
2
declare(strict_types=1);
3
4
namespace Remorhaz\JSON\Data\Export;
5
6
use Remorhaz\JSON\Data\Value\ArrayValueInterface;
7
use Remorhaz\JSON\Data\Value\ObjectValueInterface;
8
use Remorhaz\JSON\Data\Value\ScalarValueInterface;
9
use Remorhaz\JSON\Data\Value\ValueInterface;
10
11
final class ValueDecoder implements ValueDecoderInterface
12
{
13
14
    public function exportValue(ValueInterface $value)
15
    {
16
        if ($value instanceof ScalarValueInterface) {
17
            return $value->getData();
18
        }
19
20
        if ($value instanceof ArrayValueInterface) {
21
            $result = [];
22
            foreach ($value->createChildIterator() as $index => $element) {
23
                $result[$index] = $this->exportValue($element);
24
            }
25
26
            return $result;
27
        }
28
29
        if ($value instanceof ObjectValueInterface) {
30
            $result = (object) [];
31
            foreach ($value->createChildIterator() as $name => $property) {
32
                $result->{$name} = $this->exportValue($property);
33
            }
34
35
            return $result;
36
        }
37
38
        throw new Exception\UnexpectedValueException($value);
39
    }
40
}
41