Passed
Push — feature/v4 ( 69e935...62277d )
by Samuel
05:01
created

JsonBuilder::reset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ivory\GoogleMap\Helper\JsonBuilder;
6
7
use Symfony\Component\PropertyAccess\PropertyAccessor;
8
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
9
10
class JsonBuilder
11
{
12
    /**
13
     * @var PropertyAccessorInterface
14
     */
15
    private $accessor;
16
17
    /**
18
     * @var array
19
     */
20
    private $values;
21
22
    /**
23
     * @var array
24
     */
25
    private $escapes;
26
27
    /**
28
     * @var int
29
     */
30
    private $jsonEncodeOptions;
31
32
33 157
    public function __construct(PropertyAccessorInterface $propertyAccessor = null)
34
    {
35 157
        $this->accessor = $propertyAccessor ?: new PropertyAccessor();
36
37 157
        $this->reset();
38 157
    }
39
40 51
    public function getJsonEncodeOptions(): int
41
    {
42 51
        return $this->jsonEncodeOptions;
43
    }
44
45 49
    public function setJsonEncodeOptions(int $jsonEncodeOptions): JsonBuilder
46
    {
47 49
        $this->jsonEncodeOptions = $jsonEncodeOptions;
48
49 49
        return $this;
50
    }
51
52 8
    public function hasValues(): bool
53
    {
54 8
        return !empty($this->values);
55
    }
56
57 5
    public function getValues(): array
58
    {
59 5
        return $this->values;
60
    }
61
62 37
    public function setValues(array $values, string $pathPrefix = null): JsonBuilder
63
    {
64 37
        foreach ($values as $key => $value) {
65 30
            $path = sprintf('%s[%s]', $pathPrefix, $key);
66
67 30
            if (is_array($value) && !empty($value)) {
68 4
                $this->setValues($value, $path);
69
            } else {
70 30
                $this->setValue($path, $value);
71
            }
72
        }
73
74 37
        return $this;
75
    }
76
77
    /**
78
     * @param mixed $value
79
     */
80 68
    public function setValue(string $path, $value, bool $escapeValue = true): JsonBuilder
81
    {
82 68
        if (!$escapeValue) {
83 46
            $placeholder = uniqid('ivory', true);
84 46
            $this->escapes[sprintf('"%s"', $placeholder)] = $value;
85
86 46
            $value = $placeholder;
87
        }
88
89 68
        $this->values[$path] = $value;
90
91 68
        return $this;
92
    }
93
94 1
    public function removeValue(string $path): JsonBuilder
95
    {
96 1
        unset($this->values[$path]);
97 1
        unset($this->escapes[$path]);
98
99 1
        return $this;
100
    }
101
102 157
    public function reset(): JsonBuilder
103
    {
104 157
        $this->values = [];
105 157
        $this->escapes = [];
106 157
        $this->jsonEncodeOptions = 0;
107
108 157
        return $this;
109
    }
110
111 67
    public function build(): string
112
    {
113 67
        $json = [];
114
115 67
        foreach ($this->values as $path => $value) {
116 63
            $this->accessor->setValue($json, $path, $value);
117
        }
118
119 67
        return str_replace(
120 67
            array_keys($this->escapes),
121 67
            array_values($this->escapes),
122 67
            json_encode($json, $this->jsonEncodeOptions)
123
        );
124
    }
125
}
126