SafeJson::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cushon\HealthBundle\Encoder\Json;
6
7
use Ergebnis\Json\Printer\PrinterInterface;
8
9
use function Safe\json_encode;
10
11
class SafeJson implements Encoder
12
{
13
    public const DEFAULT_INDENT = 2;
14
15
    private PrinterInterface $printer;
16
17
    private string $indent;
18
19
    /**
20
     * @param PrinterInterface $printer
21
     * @param int $indent
22
     */
23
    public function __construct(PrinterInterface $printer, int $indent = self::DEFAULT_INDENT)
24
    {
25
        $this->printer = $printer;
26
        $this->indent = str_repeat(' ', $indent);
27
    }
28
29
    /**
30
     * @inheritDoc
31
     */
32
    public function encode(mixed $data): string
33
    {
34
        return $this->printer->print(
35
            json_encode($data),
36
            $this->indent
37
        );
38
    }
39
}
40