JsonBuilder   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 2
dl 0
loc 153
ccs 49
cts 49
cp 1
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A getJsonEncodeOptions() 0 4 1
A setJsonEncodeOptions() 0 6 1
A hasValues() 0 4 1
A getValues() 0 4 1
A setValues() 0 14 4
A setValue() 0 13 2
A removeValue() 0 7 1
A reset() 0 8 1
A build() 0 14 2
1
<?php
2
3
/*
4
 * This file is part of the Ivory Json Builder package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\JsonBuilder;
13
14
use Symfony\Component\PropertyAccess\PropertyAccessor;
15
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
16
17
/**
18
 * @author GeLo <[email protected]>
19
 */
20
class JsonBuilder
21
{
22
    /**
23
     * @var PropertyAccessorInterface
24
     */
25
    private $accessor;
26
27
    /**
28
     * @var array
29
     */
30
    private $values;
31
32
    /**
33
     * @var array
34
     */
35
    private $escapes;
36
37
    /**
38
     * @var int
39
     */
40
    private $jsonEncodeOptions;
41
42
    /**
43
     * @param PropertyAccessorInterface|null $propertyAccessor
44
     */
45 96
    public function __construct(PropertyAccessorInterface $propertyAccessor = null)
46
    {
47 96
        $this->accessor = $propertyAccessor ?: new PropertyAccessor();
48
49 96
        $this->reset();
50 96
    }
51
52
    /**
53
     * @return int
54
     */
55 12
    public function getJsonEncodeOptions()
56
    {
57 12
        return $this->jsonEncodeOptions;
58
    }
59
60
    /**
61
     * @param int $jsonEncodeOptions
62
     *
63
     * @return JsonBuilder
64
     */
65 5
    public function setJsonEncodeOptions($jsonEncodeOptions)
66
    {
67 4
        $this->jsonEncodeOptions = $jsonEncodeOptions;
68
69 5
        return $this;
70
    }
71
72
    /**
73
     * @return bool
74
     */
75 24
    public function hasValues()
76
    {
77 24
        return !empty($this->values);
78
    }
79
80
    /**
81
     * @return array
82
     */
83 20
    public function getValues()
84
    {
85 20
        return $this->values;
86
    }
87
88
    /**
89
     * @param array  $values
90
     * @param string $pathPrefix
91
     *
92
     * @return JsonBuilder
93
     */
94 40
    public function setValues(array $values, $pathPrefix = null)
95
    {
96 40
        foreach ($values as $key => $value) {
97 40
            $path = sprintf('%s[%s]', $pathPrefix, $key);
98
99 40
            if (is_array($value) && !empty($value)) {
100 16
                $this->setValues($value, $path);
101 8
            } else {
102 40
                $this->setValue($path, $value);
103
            }
104 20
        }
105
106 40
        return $this;
107
    }
108
109
    /**
110
     * @param string $path
111
     * @param mixed  $value
112
     * @param bool   $escapeValue
113
     *
114
     * @return JsonBuilder
115
     */
116 84
    public function setValue($path, $value, $escapeValue = true)
117
    {
118 84
        if (!$escapeValue) {
119 36
            $placeholder = uniqid('ivory', true);
120 36
            $this->escapes[sprintf('"%s"', $placeholder)] = $value;
121
122 36
            $value = $placeholder;
123 18
        }
124
125 84
        $this->values[$path] = $value;
126
127 84
        return $this;
128
    }
129
130
    /**
131
     * @param string $path
132
     *
133
     * @return JsonBuilder
134
     */
135 4
    public function removeValue($path)
136
    {
137 4
        unset($this->values[$path]);
138 4
        unset($this->escapes[$path]);
139
140 4
        return $this;
141
    }
142
143
    /**
144
     * @return JsonBuilder
145
     */
146 96
    public function reset()
147
    {
148 96
        $this->values = [];
149 96
        $this->escapes = [];
150 96
        $this->jsonEncodeOptions = 0;
151
152 96
        return $this;
153
    }
154
155
    /**
156
     * @return string
157
     */
158 72
    public function build()
159
    {
160 72
        $json = [];
161
162 72
        foreach ($this->values as $path => $value) {
163 64
            $this->accessor->setValue($json, $path, $value);
164 36
        }
165
166 72
        return str_replace(
167 72
            array_keys($this->escapes),
168 72
            array_values($this->escapes),
169 72
            json_encode($json, $this->jsonEncodeOptions)
170 36
        );
171
    }
172
}
173