Completed
Push — master ( cb0ced...7754cf )
by Edward
04:45
created

Decoder::exportEvents()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 16
c 0
b 0
f 0
nc 6
nop 1
dl 0
loc 28
ccs 0
cts 17
cp 0
crap 42
rs 9.1111
1
<?php
2
declare(strict_types=1);
3
4
namespace Remorhaz\JSON\Data\Export;
5
6
use Remorhaz\JSON\Data\Iterator\ValueIteratorFactoryInterface;
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 Decoder implements DecoderInterface
13
{
14
15
    private $valueIteratorFactory;
16
17
    public function __construct(ValueIteratorFactoryInterface $valueIteratorFactory)
18
    {
19
        $this->valueIteratorFactory = $valueIteratorFactory;
20
    }
21
22
    public function exportValue(ValueInterface $value)
23
    {
24
        if ($value instanceof ScalarValueInterface) {
25
            return $value->getData();
26
        }
27
28
        if ($value instanceof ArrayValueInterface) {
29
            $result = [];
30
            foreach ($value->createChildIterator() as $index => $element) {
31
                $result[$index] = $this->exportValue($element);
32
            }
33
34
            return $result;
35
        }
36
37
        if ($value instanceof ObjectValueInterface) {
38
            $result = (object) [];
39
            foreach ($value->createChildIterator() as $name => $property) {
40
                $result->{$name} = $this->exportValue($property);
41
            }
42
43
            return $result;
44
        }
45
46
        throw new Exception\UnexpectedValueException($value);
47
    }
48
}
49