NodeValueFactory::createValue()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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