ValueDecoder   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 28
rs 10
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A exportValue() 0 25 6
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