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

NodeValueFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace Remorhaz\JSON\Data\Value\EncodedJson;
5
6
use function json_decode;
7
use const JSON_THROW_ON_ERROR;
8
use Remorhaz\JSON\Data\Path\PathInterface;
9
use Remorhaz\JSON\Data\Value\DecodedJson\NodeValueFactory as DecodedJsonNodeValueFactory;
10
use Remorhaz\JSON\Data\Value\DecodedJson\NodeValueFactoryInterface as DecodedJsonNodeValueFactoryInterface;
11
use Remorhaz\JSON\Data\Value\NodeValueInterface;
12
use Throwable;
13
14
final class NodeValueFactory implements NodeValueFactoryInterface
15
{
16
17
    private $decodedJsonNodeValueFactory;
18
19
    public static function create(): NodeValueFactoryInterface
20
    {
21
        return new self(DecodedJsonNodeValueFactory::create());
22
    }
23
24
    public function __construct(DecodedJsonNodeValueFactoryInterface $decodedJsonNodeValueFactory)
25
    {
26
        $this->decodedJsonNodeValueFactory = $decodedJsonNodeValueFactory;
27
    }
28
29
    public function createValue(string $json, ?PathInterface $path = null): NodeValueInterface
30
    {
31
        try {
32
            $decodedData = json_decode($json, false, 512, JSON_THROW_ON_ERROR);
33
        } catch (Throwable $e) {
34
            throw new Exception\JsonDecodingFailedException($json, $e);
35
        }
36
37
        return $this
38
            ->decodedJsonNodeValueFactory
39
            ->createValue($decodedData, $path);
40
    }
41
}
42