1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSCKEditor Bundle. |
5
|
|
|
* |
6
|
|
|
* (c) 2018 - present Friends of Symfony |
7
|
|
|
* (c) 2009 - 2017 Eric GELOEN <[email protected]> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please read the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace FOS\CKEditorBundle\Builder; |
14
|
|
|
|
15
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessorInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author GeLo <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
final class JsonBuilder |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var PropertyAccessorInterface |
24
|
|
|
*/ |
25
|
|
|
private $propertyAccessor; |
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 = 0; |
41
|
|
|
|
42
|
|
|
public function __construct(PropertyAccessorInterface $propertyAccessor) |
43
|
|
|
{ |
44
|
|
|
$this->propertyAccessor = $propertyAccessor; |
45
|
|
|
|
46
|
|
|
$this->reset(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function getJsonEncodeOptions(): int |
50
|
|
|
{ |
51
|
|
|
return $this->jsonEncodeOptions; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function setJsonEncodeOptions(int $jsonEncodeOptions): self |
55
|
|
|
{ |
56
|
|
|
$this->jsonEncodeOptions = $jsonEncodeOptions; |
57
|
|
|
|
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function hasValues(): bool |
62
|
|
|
{ |
63
|
|
|
return !empty($this->values); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function getValues(): array |
67
|
|
|
{ |
68
|
|
|
return $this->values; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function setValues(array $values, string $pathPrefix = null): self |
72
|
|
|
{ |
73
|
|
|
foreach ($values as $key => $value) { |
74
|
|
|
$path = sprintf('%s[%s]', $pathPrefix, $key); |
75
|
|
|
|
76
|
|
|
if (\is_array($value) && !empty($value)) { |
77
|
|
|
$this->setValues($value, $path); |
78
|
|
|
} else { |
79
|
|
|
$this->setValue($path, $value); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param mixed $value |
88
|
|
|
*/ |
89
|
|
|
public function setValue(string $path, $value, bool $escapeValue = true): self |
90
|
|
|
{ |
91
|
|
|
if (!$escapeValue) { |
92
|
|
|
$placeholder = uniqid('friendsofsymfony', true); |
93
|
|
|
$this->escapes[sprintf('"%s"', $placeholder)] = $value; |
94
|
|
|
|
95
|
|
|
$value = $placeholder; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$this->values[$path] = $value; |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function removeValue(string $path): self |
104
|
|
|
{ |
105
|
|
|
unset($this->values[$path], $this->escapes[$path]); |
106
|
|
|
|
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function reset(): self |
111
|
|
|
{ |
112
|
|
|
$this->values = []; |
113
|
|
|
$this->escapes = []; |
114
|
|
|
$this->jsonEncodeOptions = 0; |
115
|
|
|
|
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function build(): string |
120
|
|
|
{ |
121
|
|
|
$values = []; |
122
|
|
|
|
123
|
|
|
foreach ($this->values as $path => $value) { |
124
|
|
|
$this->propertyAccessor->setValue($values, $path, $value); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$json = json_encode($values, $this->jsonEncodeOptions); |
128
|
|
|
|
129
|
|
|
\assert(\is_string($json)); |
130
|
|
|
|
131
|
|
|
return str_replace( |
132
|
|
|
array_keys($this->escapes), |
133
|
|
|
array_values($this->escapes), |
134
|
|
|
$json |
135
|
|
|
); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|