JsonBinaryDecoderFormatter::formatName()   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 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace MySQLReplication\JsonBinaryDecoder;
5
6
class JsonBinaryDecoderFormatter
7
{
8
    public $jsonString = '';
9
10
    public function formatValueBool(bool $bool): void
11
    {
12
        $this->jsonString .= var_export($bool, true);
13
    }
14
15
    public function formatValueNumeric(int $val): void
16
    {
17
        $this->jsonString .= $val;
18
    }
19
20
    public function formatValue($val): void
21
    {
22
        $this->jsonString .= '"' . self::escapeJsonString($val) . '"';
23
    }
24
25
    /**
26
     * Some characters needs to be escaped
27
     * @see http://www.json.org/
28
     * @see https://stackoverflow.com/questions/1048487/phps-json-encode-does-not-escape-all-json-control-characters
29
     */
30
    private static function escapeJsonString($value): string
31
    {
32
        return str_replace(
33
            ["\\", '/', '"', "\n", "\r", "\t", "\x08", "\x0c"],
34
            ["\\\\", "\\/", "\\\"", "\\n", "\\r", "\\t", "\\f", "\\b"],
35
            (string)$value
36
        );
37
    }
38
39
    public function formatEndObject(): void
40
    {
41
        $this->jsonString .= '}';
42
    }
43
44
    public function formatBeginArray(): void
45
    {
46
        $this->jsonString .= '[';
47
    }
48
49
    public function formatEndArray(): void
50
    {
51
        $this->jsonString .= ']';
52
    }
53
54
    public function formatBeginObject(): void
55
    {
56
        $this->jsonString .= '{';
57
    }
58
59
    public function formatNextEntry(): void
60
    {
61
        $this->jsonString .= ',';
62
    }
63
64
    public function formatName(string $name): void
65
    {
66
        $this->jsonString .= '"' . $name . '":';
67
    }
68
69
    public function formatValueNull(): void
70
    {
71
        $this->jsonString .= 'null';
72
    }
73
74
    public function getJsonString(): string
75
    {
76
        return $this->jsonString;
77
    }
78
}
79