Passed
Push — master ( 76b76b...ca78d8 )
by Edward
02:11
created

NodeValueFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 33
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 3 1
A createValue() 0 18 6
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
    public static function create(): NodeValueFactoryInterface
17
    {
18
        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
    public function createValue($data, ?PathInterface $path = null): NodeValueInterface
29
    {
30
        if (!isset($path)) {
31
            $path = new Path;
32
        }
33
        if (null === $data || is_scalar($data)) {
34
            return new NodeScalarValue($data, $path);
35
        }
36
37
        if (is_array($data)) {
38
            return new NodeArrayValue($data, $path, $this);
39
        }
40
41
        if ($data instanceof stdClass) {
0 ignored issues
show
introduced by
$data is always a sub-type of stdClass.
Loading history...
42
            return new NodeObjectValue($data, $path, $this);
43
        }
44
45
        throw new Exception\InvalidNodeDataException($data, $path);
46
    }
47
}
48