ValueDecoder::exportValue()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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