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

Encoder   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
dl 0
loc 22
ccs 0
cts 11
cp 0
rs 10
c 1
b 0
f 0
wmc 3

2 Methods

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