ValueEncoder::exportValue()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 12
rs 9.9666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Remorhaz\JSON\Data\Export;
6
7
use Remorhaz\JSON\Data\Value\ValueInterface;
8
use Throwable;
9
10
use function json_encode;
11
12
use const JSON_THROW_ON_ERROR;
13
use const JSON_UNESCAPED_SLASHES;
14
use const JSON_UNESCAPED_UNICODE;
15
16
/**
17
 * @todo Don't use decoder
18
 */
19
final class ValueEncoder implements ValueEncoderInterface
20
{
21
22
    private $decoder;
23
24
    public function __construct(ValueDecoderInterface $decoder)
25
    {
26
        $this->decoder = $decoder;
27
    }
28
29
    public function exportValue(ValueInterface $value): string
30
    {
31
        $decodedValue = $this
32
            ->decoder
33
            ->exportValue($value);
34
        try {
35
            return json_encode(
36
                $decodedValue,
37
                JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR
38
            );
39
        } catch (Throwable $e) {
40
            throw new Exception\EncodingFailedException($decodedValue, $e);
41
        }
42
    }
43
}
44