JsonTypeEncoder::encode()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 2
cts 2
cp 1
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