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
|
|
|
public function __construct(PropertyAccessorInterface $propertyAccessor = null) |
44
|
|
|
{ |
45
|
96 |
|
$this->accessor = $propertyAccessor ?: new PropertyAccessor(); |
46
|
|
|
|
47
|
96 |
|
$this->reset(); |
48
|
|
|
} |
49
|
96 |
|
|
50
|
96 |
|
public function getJsonEncodeOptions(): int |
51
|
|
|
{ |
52
|
|
|
return $this->jsonEncodeOptions; |
53
|
|
|
} |
54
|
|
|
|
55
|
12 |
|
public function setJsonEncodeOptions(int $jsonEncodeOptions): JsonBuilder |
56
|
|
|
{ |
57
|
12 |
|
$this->jsonEncodeOptions = $jsonEncodeOptions; |
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function hasValues(): bool |
63
|
|
|
{ |
64
|
|
|
return !empty($this->values); |
65
|
4 |
|
} |
66
|
|
|
|
67
|
4 |
|
public function getValues(): array |
68
|
|
|
{ |
69
|
4 |
|
return $this->values; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function setValues(array $values, string $pathPrefix = null): JsonBuilder |
73
|
|
|
{ |
74
|
|
|
foreach ($values as $key => $value) { |
75
|
24 |
|
$path = sprintf('%s[%s]', $pathPrefix, $key); |
76
|
|
|
|
77
|
24 |
|
if (is_array($value) && !empty($value)) { |
78
|
|
|
$this->setValues($value, $path); |
79
|
|
|
} else { |
80
|
|
|
$this->setValue($path, $value); |
81
|
|
|
} |
82
|
|
|
} |
83
|
20 |
|
|
84
|
|
|
return $this; |
85
|
20 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param mixed $value |
89
|
|
|
*/ |
90
|
|
|
public function setValue(string $path, $value, bool $escapeValue = true): JsonBuilder |
91
|
|
|
{ |
92
|
|
|
if (!$escapeValue) { |
93
|
|
|
$placeholder = uniqid('ivory', true); |
94
|
40 |
|
$this->escapes[sprintf('"%s"', $placeholder)] = $value; |
95
|
|
|
|
96
|
40 |
|
$value = $placeholder; |
97
|
40 |
|
} |
98
|
|
|
|
99
|
40 |
|
$this->values[$path] = $value; |
100
|
16 |
|
|
101
|
|
|
return $this; |
102
|
40 |
|
} |
103
|
|
|
|
104
|
|
|
public function removeValue(string $path): JsonBuilder |
105
|
|
|
{ |
106
|
40 |
|
unset($this->values[$path]); |
107
|
|
|
unset($this->escapes[$path]); |
108
|
|
|
|
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function reset(): JsonBuilder |
113
|
|
|
{ |
114
|
|
|
$this->values = []; |
115
|
|
|
$this->escapes = []; |
116
|
84 |
|
$this->jsonEncodeOptions = 0; |
117
|
|
|
|
118
|
84 |
|
return $this; |
119
|
36 |
|
} |
120
|
36 |
|
|
121
|
|
|
public function build(): string |
122
|
36 |
|
{ |
123
|
|
|
$json = []; |
124
|
|
|
|
125
|
84 |
|
foreach ($this->values as $path => $value) { |
126
|
|
|
$this->accessor->setValue($json, $path, $value); |
127
|
84 |
|
} |
128
|
|
|
|
129
|
|
|
return str_replace( |
130
|
|
|
array_keys($this->escapes), |
131
|
|
|
array_values($this->escapes), |
132
|
|
|
json_encode($json, $this->jsonEncodeOptions) |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|