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

Encoder::exportValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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