Passed
Push — master ( 4f4697...c69a90 )
by Edward
04:40
created

NodeValueFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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