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

ValueEncoder   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A exportValue() 0 12 2
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 ValueEncoder implements ValueEncoderInterface
17
{
18
19
    private $decoder;
20
21
    public function __construct(ValueDecoderInterface $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