JsonBinaryDecoderFormatter   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 17
dl 0
loc 71
ccs 0
cts 24
cp 0
rs 10
c 2
b 0
f 0
wmc 12

12 Methods

Rating   Name   Duplication   Size   Complexity  
A formatEndArray() 0 3 1
A formatBeginObject() 0 3 1
A formatNextEntry() 0 3 1
A formatValueBool() 0 3 1
A getJsonString() 0 3 1
A formatValue() 0 3 1
A formatName() 0 3 1
A escapeJsonString() 0 6 1
A formatValueNumeric() 0 3 1
A formatEndObject() 0 3 1
A formatBeginArray() 0 3 1
A formatValueNull() 0 3 1
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
}