JsonTypeEncoder::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Serialization\Encoder;
6
7
final class JsonTypeEncoder implements TypeEncoderInterface
8
{
9
    /**
10
     * @var bool
11
     */
12
    private $prettyPrint;
13
14
    public function __construct(bool $prettyPrint = false)
15
    {
16
        $this->prettyPrint = $prettyPrint;
17 2
    }
18
19 2
    public function getContentType(): string
20 2
    {
21
        return 'application/json';
22
    }
23
24
    public function encode(array $data): string
25 1
    {
26
        $options = JSON_PRESERVE_ZERO_FRACTION | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES;
27 1
        if ($this->prettyPrint) {
28
            $options = $options | JSON_PRETTY_PRINT;
29
        }
30
31
        return json_encode($data, $options);
32
    }
33
}
34