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

NodeValueFactory::createValue()   A

Complexity

Conditions 6
Paths 8

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 9
c 0
b 0
f 0
nc 8
nop 2
dl 0
loc 18
ccs 10
cts 10
cp 1
crap 6
rs 9.2222
1
<?php
2
declare(strict_types=1);
3
4
namespace Remorhaz\JSON\Data\Value\DecodedJson;
5
6
use Remorhaz\JSON\Data\Iterator\ValueIteratorFactory;
7
use Remorhaz\JSON\Data\Iterator\ValueIteratorFactoryInterface;
8
use function is_array;
9
use function is_scalar;
10
use Remorhaz\JSON\Data\Path\Path;
11
use Remorhaz\JSON\Data\Value\NodeValueInterface;
12
use Remorhaz\JSON\Data\Path\PathInterface;
13
use stdClass;
14
15
final class NodeValueFactory implements NodeValueFactoryInterface
16
{
17
18
    private $valueIteratorFactory;
19
20 31
    public static function create(): NodeValueFactoryInterface
21
    {
22 31
        return new self(new ValueIteratorFactory);
23
    }
24
25 31
    public function __construct(ValueIteratorFactoryInterface $valueIteratorFactory)
26
    {
27 31
        $this->valueIteratorFactory = $valueIteratorFactory;
28 31
    }
29
30
    /**
31
     * {@inheritDoc}
32
     *
33
     * @param array|bool|float|int|stdClass|string|null $data
34
     * @param PathInterface|null $path
35
     * @return NodeValueInterface
36
     */
37 31
    public function createValue($data, ?PathInterface $path = null): NodeValueInterface
38
    {
39 31
        if (!isset($path)) {
40 7
            $path = new Path;
41
        }
42 31
        if (null === $data || is_scalar($data)) {
43 23
            return new NodeScalarValue($data, $path);
44
        }
45
46 10
        if (is_array($data)) {
47 4
            return new NodeArrayValue($data, $path, $this, $this->valueIteratorFactory);
48
        }
49
50 6
        if ($data instanceof stdClass) {
0 ignored issues
show
introduced by
$data is always a sub-type of stdClass.
Loading history...
51 4
            return new NodeObjectValue($data, $path, $this, $this->valueIteratorFactory);
52
        }
53
54 2
        throw new Exception\InvalidNodeDataException($data, $path);
55
    }
56
}
57