NodeValueFactory::createValue()   A
last analyzed

Complexity

Conditions 6
Paths 8

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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