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

NodeValueFactory   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 40
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
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 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