|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Ivory Google Map package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Eric GELOEN <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please read the LICENSE |
|
11
|
|
|
* file that was distributed with this source declaration. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Ivory\GoogleMap\Helper\Formatter; |
|
15
|
|
|
|
|
16
|
|
|
use Ivory\GoogleMap\Utility\VariableAwareInterface; |
|
17
|
|
|
|
|
18
|
|
|
class Formatter |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var bool */ |
|
21
|
|
|
private $debug; |
|
22
|
|
|
|
|
23
|
|
|
/** @var int */ |
|
24
|
|
|
private $indentationStep; |
|
25
|
|
|
|
|
26
|
386 |
|
public function __construct(bool $debug = false, int $indentationStep = 4) |
|
27
|
|
|
{ |
|
28
|
386 |
|
$this->setDebug($debug); |
|
29
|
386 |
|
$this->setIndentationStep($indentationStep); |
|
30
|
386 |
|
} |
|
31
|
|
|
|
|
32
|
50 |
|
public function isDebug(): bool |
|
33
|
|
|
{ |
|
34
|
50 |
|
return $this->debug; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
386 |
|
public function setDebug(bool $debug): void |
|
38
|
|
|
{ |
|
39
|
386 |
|
$this->debug = $debug; |
|
40
|
386 |
|
} |
|
41
|
|
|
|
|
42
|
2 |
|
public function getIndentationStep(): int |
|
43
|
|
|
{ |
|
44
|
2 |
|
return $this->indentationStep; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
386 |
|
public function setIndentationStep(int $indentationStep): void |
|
48
|
|
|
{ |
|
49
|
386 |
|
$this->indentationStep = $indentationStep; |
|
50
|
386 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** @param string|false|null $namespace */ |
|
53
|
79 |
|
public function renderClass(?string $name = null, $namespace = null): ?string |
|
54
|
|
|
{ |
|
55
|
79 |
|
if (null === $namespace) { |
|
56
|
68 |
|
$namespace = $this->renderProperty('google', 'maps'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
79 |
|
if (empty($namespace)) { |
|
60
|
4 |
|
return $name; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
75 |
|
return $this->renderProperty($namespace, $name); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** @param string|false|null $namespace */ |
|
67
|
25 |
|
public function renderConstant(string $class, string $value, $namespace = null): ?string |
|
68
|
|
|
{ |
|
69
|
25 |
|
return $this->renderClass($this->renderProperty($class, strtoupper($value)), $namespace); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** @param string|false|null $namespace */ |
|
73
|
43 |
|
public function renderObject( |
|
74
|
|
|
string $class, |
|
75
|
|
|
array $arguments = [], |
|
76
|
|
|
$namespace = null, |
|
77
|
|
|
bool $semicolon = false, |
|
78
|
|
|
bool $newLine = false |
|
79
|
|
|
): string { |
|
80
|
43 |
|
return $this->renderCall( |
|
81
|
43 |
|
'new '.$this->renderClass($class, $namespace), |
|
82
|
|
|
$arguments, |
|
83
|
|
|
$semicolon, |
|
84
|
|
|
$newLine |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
119 |
|
public function renderProperty(string $object, ?string $property = null): string |
|
89
|
|
|
{ |
|
90
|
119 |
|
if (!empty($property)) { |
|
91
|
117 |
|
$property = '.'.$property; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
119 |
|
return $object.$property; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** @param string[] $arguments */ |
|
98
|
22 |
|
public function renderObjectCall( |
|
99
|
|
|
VariableAwareInterface $object, |
|
100
|
|
|
string $method, |
|
101
|
|
|
array $arguments = [], |
|
102
|
|
|
bool $semicolon = false, |
|
103
|
|
|
bool $newLine = false |
|
104
|
|
|
): string { |
|
105
|
22 |
|
return $this->renderCall( |
|
106
|
22 |
|
$this->renderProperty($object->getVariable(), $method), |
|
|
|
|
|
|
107
|
|
|
$arguments, |
|
108
|
|
|
$semicolon, |
|
109
|
|
|
$newLine |
|
110
|
|
|
); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** @param string[] $arguments */ |
|
114
|
101 |
|
public function renderCall( |
|
115
|
|
|
?string $method, |
|
116
|
|
|
array $arguments = [], |
|
117
|
|
|
bool $semicolon = false, |
|
118
|
|
|
bool $newLine = false |
|
119
|
|
|
): string { |
|
120
|
101 |
|
return $this->renderCode( |
|
121
|
101 |
|
$method.$this->renderArguments($arguments), |
|
122
|
|
|
$semicolon, |
|
123
|
|
|
$newLine |
|
124
|
|
|
); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** @param string[] $arguments */ |
|
128
|
34 |
|
public function renderClosure( |
|
129
|
|
|
string $code = null, |
|
130
|
|
|
array $arguments = [], |
|
131
|
|
|
string $name = null, |
|
132
|
|
|
bool $semicolon = false, |
|
133
|
|
|
bool $newLine = false |
|
134
|
|
|
): string { |
|
135
|
34 |
|
$separator = $this->renderSeparator(); |
|
136
|
|
|
|
|
137
|
34 |
|
if (null !== $name) { |
|
138
|
18 |
|
$name = ' '.$name; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
34 |
|
return $this->renderCode($this->renderLines([ |
|
142
|
34 |
|
'function'.$name.$separator.$this->renderArguments($arguments).$separator.'{', |
|
143
|
34 |
|
$this->renderIndentation($code), |
|
144
|
34 |
|
'}', |
|
145
|
34 |
|
], !empty($code), $newLine && !$semicolon), $semicolon, $newLine && $semicolon); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
51 |
|
public function renderObjectAssignment( |
|
149
|
|
|
VariableAwareInterface $object, |
|
150
|
|
|
string $declaration, |
|
151
|
|
|
bool $semicolon = false, |
|
152
|
|
|
bool $newLine = false |
|
153
|
|
|
): string { |
|
154
|
51 |
|
return $this->renderAssignment($object->getVariable(), $declaration, $semicolon, $newLine); |
|
|
|
|
|
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
12 |
|
public function renderContainerAssignment( |
|
158
|
|
|
VariableAwareInterface $root, |
|
159
|
|
|
string $declaration, |
|
160
|
|
|
string $propertyPath = null, |
|
161
|
|
|
VariableAwareInterface $object = null, |
|
162
|
|
|
bool $semicolon = true, |
|
163
|
|
|
bool $newLine = true |
|
164
|
|
|
): string { |
|
165
|
12 |
|
return $this->renderAssignment( |
|
166
|
12 |
|
$this->renderContainerVariable($root, $propertyPath, $object), |
|
167
|
|
|
$declaration, |
|
168
|
|
|
$semicolon, |
|
169
|
|
|
$newLine |
|
170
|
|
|
); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
16 |
|
public function renderContainerVariable( |
|
174
|
|
|
VariableAwareInterface $root, |
|
175
|
|
|
string $propertyPath = null, |
|
176
|
|
|
VariableAwareInterface $object = null |
|
177
|
|
|
): string { |
|
178
|
16 |
|
$variable = $root->getVariable().'_container'; |
|
179
|
|
|
|
|
180
|
16 |
|
if (null !== $propertyPath) { |
|
181
|
6 |
|
$variable = $this->renderProperty($variable, $propertyPath); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
16 |
|
if (null !== $object) { |
|
185
|
6 |
|
$variable = $this->renderProperty($variable, $object->getVariable()); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
16 |
|
return $variable; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
82 |
|
public function renderAssignment( |
|
192
|
|
|
string $variable, |
|
193
|
|
|
string $declaration, |
|
194
|
|
|
bool $semicolon = false, |
|
195
|
|
|
bool $newLine = false |
|
196
|
|
|
): string { |
|
197
|
82 |
|
$separator = $this->renderSeparator(); |
|
198
|
|
|
|
|
199
|
82 |
|
return $this->renderCode($variable.$separator.'='.$separator.$declaration, $semicolon, $newLine); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
14 |
|
public function renderStatement( |
|
203
|
|
|
string $statement, |
|
204
|
|
|
string $code, |
|
205
|
|
|
?string $condition = null, |
|
206
|
|
|
?string $next = null, |
|
207
|
|
|
bool $newLine = true |
|
208
|
|
|
): string { |
|
209
|
14 |
|
$separator = $this->renderSeparator(); |
|
210
|
14 |
|
$statement .= $separator; |
|
211
|
|
|
|
|
212
|
14 |
|
if (!empty($condition)) { |
|
213
|
12 |
|
$statement .= $this->renderArguments([$condition]).$separator; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
14 |
|
if (!empty($next)) { |
|
217
|
7 |
|
$next = $separator.$next; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
14 |
|
return $this->renderLines([ |
|
221
|
14 |
|
$statement.'{', |
|
222
|
14 |
|
$this->renderIndentation($code), |
|
223
|
14 |
|
'}'.$next, |
|
224
|
14 |
|
], true, $newLine); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
166 |
|
public function renderCode(string $code, bool $semicolon = true, bool $newLine = true): string |
|
228
|
|
|
{ |
|
229
|
166 |
|
if ($semicolon) { |
|
230
|
63 |
|
$code .= ';'; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
166 |
|
return $this->renderLine($code, $newLine); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
80 |
|
public function renderIndentation(?string $code = null): string |
|
237
|
|
|
{ |
|
238
|
80 |
|
if ($this->debug && !empty($code)) { |
|
239
|
21 |
|
$indentation = str_repeat(' ', $this->indentationStep); |
|
240
|
21 |
|
$code = $indentation.str_replace("\n", "\n".$indentation, $code); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
80 |
|
return (string) $code; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** @param string[] $codes */ |
|
247
|
86 |
|
public function renderLines(array $codes, bool $newLine = true, bool $eolLine = true): string |
|
248
|
|
|
{ |
|
249
|
86 |
|
$result = ''; |
|
250
|
86 |
|
$count = count($codes); |
|
251
|
|
|
|
|
252
|
86 |
|
for ($index = 0; $index < $count; ++$index) { |
|
253
|
84 |
|
$result .= $this->renderLine($codes[$index], $newLine && $index !== $count - 1); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
86 |
|
return $this->renderLine($result, $eolLine); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
216 |
|
public function renderLine(string $code = null, bool $newLine = true): string |
|
260
|
|
|
{ |
|
261
|
216 |
|
if ($newLine && !empty($code) && $this->debug) { |
|
262
|
46 |
|
$code .= "\n"; |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
216 |
|
return (string) $code; |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** @param string|bool $argument */ |
|
269
|
70 |
|
public function renderEscape($argument): string |
|
270
|
|
|
{ |
|
271
|
70 |
|
return json_encode($argument, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
178 |
|
public function renderSeparator(): string |
|
275
|
|
|
{ |
|
276
|
178 |
|
return $this->debug ? ' ' : ''; |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
/** @param string[] $arguments */ |
|
280
|
119 |
|
private function renderArguments(array $arguments): string |
|
281
|
|
|
{ |
|
282
|
119 |
|
return '('.implode(','.$this->renderSeparator(), $arguments).')'; |
|
283
|
|
|
} |
|
284
|
|
|
} |
|
285
|
|
|
|