JsonTypeEncoder   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
dl 0
loc 25
ccs 5
cts 5
cp 1
rs 10
c 1
b 0
f 0
wmc 4

3 Methods

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