|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Ivory Google Map 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 declaration. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Ivory\GoogleMap\Helper\Formatter; |
|
13
|
|
|
|
|
14
|
|
|
use Ivory\GoogleMap\Utility\VariableAwareInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @author GeLo <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class Formatter |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var bool |
|
23
|
|
|
*/ |
|
24
|
|
|
private $debug; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var int |
|
28
|
|
|
*/ |
|
29
|
|
|
private $indentationStep; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param bool $debug |
|
33
|
|
|
* @param int $indentationStep |
|
34
|
|
|
*/ |
|
35
|
1808 |
|
public function __construct($debug = false, $indentationStep = 4) |
|
36
|
|
|
{ |
|
37
|
1808 |
|
$this->setDebug($debug); |
|
38
|
1808 |
|
$this->setIndentationStep($indentationStep); |
|
39
|
1808 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @return bool |
|
43
|
|
|
*/ |
|
44
|
464 |
|
public function isDebug() |
|
45
|
|
|
{ |
|
46
|
464 |
|
return $this->debug; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param bool $debug |
|
51
|
|
|
*/ |
|
52
|
1808 |
|
public function setDebug($debug) |
|
53
|
|
|
{ |
|
54
|
1808 |
|
$this->debug = $debug; |
|
55
|
1808 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return int |
|
59
|
|
|
*/ |
|
60
|
8 |
|
public function getIndentationStep() |
|
61
|
|
|
{ |
|
62
|
8 |
|
return $this->indentationStep; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param int $indentationStep |
|
67
|
|
|
*/ |
|
68
|
1808 |
|
public function setIndentationStep($indentationStep) |
|
69
|
|
|
{ |
|
70
|
1808 |
|
$this->indentationStep = $indentationStep; |
|
71
|
1808 |
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param string|null $name |
|
75
|
|
|
* @param string|false|null $namespace |
|
76
|
|
|
* |
|
77
|
|
|
* @return string |
|
78
|
|
|
*/ |
|
79
|
580 |
|
public function renderClass($name = null, $namespace = null) |
|
80
|
|
|
{ |
|
81
|
580 |
|
if ($namespace === null) { |
|
82
|
536 |
|
$namespace = $this->renderProperty('google', 'maps'); |
|
83
|
268 |
|
} |
|
84
|
|
|
|
|
85
|
580 |
|
if (empty($namespace)) { |
|
86
|
52 |
|
return $name; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
564 |
|
return $this->renderProperty($namespace, $name); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param string $class |
|
94
|
|
|
* @param string $value |
|
95
|
|
|
* @param string|false|null $namespace |
|
96
|
|
|
* |
|
97
|
|
|
* @return string |
|
98
|
|
|
*/ |
|
99
|
320 |
|
public function renderConstant($class, $value, $namespace = null) |
|
100
|
|
|
{ |
|
101
|
320 |
|
return $this->renderClass($this->renderProperty($class, strtoupper($value)), $namespace); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param string $class |
|
106
|
|
|
* @param string[] $arguments |
|
107
|
|
|
* @param string|false|null $namespace |
|
108
|
|
|
* @param bool $semicolon |
|
109
|
|
|
* @param bool $newLine |
|
110
|
|
|
* |
|
111
|
|
|
* @return string |
|
112
|
|
|
*/ |
|
113
|
436 |
|
public function renderObject( |
|
114
|
|
|
$class, |
|
115
|
|
|
array $arguments = [], |
|
116
|
|
|
$namespace = null, |
|
117
|
|
|
$semicolon = false, |
|
118
|
|
|
$newLine = false |
|
119
|
|
|
) { |
|
120
|
436 |
|
return $this->renderCall( |
|
121
|
436 |
|
'new '.$this->renderClass($class, $namespace), |
|
122
|
218 |
|
$arguments, |
|
123
|
218 |
|
$semicolon, |
|
124
|
|
|
$newLine |
|
125
|
218 |
|
); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param string $object |
|
130
|
|
|
* @param string|null $property |
|
131
|
|
|
* |
|
132
|
|
|
* @return string |
|
133
|
|
|
*/ |
|
134
|
740 |
|
public function renderProperty($object, $property = null) |
|
135
|
|
|
{ |
|
136
|
740 |
|
if (!empty($property)) { |
|
137
|
732 |
|
$property = '.'.$property; |
|
138
|
366 |
|
} |
|
139
|
|
|
|
|
140
|
740 |
|
return $object.$property; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @param VariableAwareInterface $object |
|
145
|
|
|
* @param string $method |
|
146
|
|
|
* @param string[] $arguments |
|
147
|
|
|
* @param bool $semicolon |
|
148
|
|
|
* @param bool $newLine |
|
149
|
|
|
* |
|
150
|
|
|
* @return string |
|
151
|
|
|
*/ |
|
152
|
308 |
|
public function renderObjectCall( |
|
153
|
|
|
VariableAwareInterface $object, |
|
154
|
|
|
$method, |
|
155
|
|
|
array $arguments = [], |
|
156
|
|
|
$semicolon = false, |
|
157
|
|
|
$newLine = false |
|
158
|
|
|
) { |
|
159
|
308 |
|
return $this->renderCall( |
|
160
|
308 |
|
$this->renderProperty($object->getVariable(), $method), |
|
161
|
154 |
|
$arguments, |
|
162
|
154 |
|
$semicolon, |
|
163
|
|
|
$newLine |
|
164
|
154 |
|
); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* @param string|null $method |
|
169
|
|
|
* @param string[] $arguments |
|
170
|
|
|
* @param bool $semicolon |
|
171
|
|
|
* @param bool $newLine |
|
172
|
|
|
* |
|
173
|
|
|
* @return string |
|
174
|
|
|
*/ |
|
175
|
668 |
|
public function renderCall($method, array $arguments = [], $semicolon = false, $newLine = false) |
|
176
|
|
|
{ |
|
177
|
668 |
|
return $this->renderCode( |
|
178
|
668 |
|
$method.$this->renderArguments($arguments), |
|
179
|
334 |
|
$semicolon, |
|
180
|
|
|
$newLine |
|
181
|
334 |
|
); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @param string|null $code |
|
186
|
|
|
* @param string[] $arguments |
|
187
|
|
|
* @param string|null $name |
|
188
|
|
|
* @param bool $semicolon |
|
189
|
|
|
* @param bool $newLine |
|
190
|
|
|
* |
|
191
|
|
|
* @return string |
|
192
|
|
|
*/ |
|
193
|
400 |
|
public function renderClosure( |
|
194
|
|
|
$code = null, |
|
195
|
|
|
array $arguments = [], |
|
196
|
|
|
$name = null, |
|
197
|
|
|
$semicolon = false, |
|
198
|
|
|
$newLine = false |
|
199
|
|
|
) { |
|
200
|
400 |
|
$separator = $this->renderSeparator(); |
|
201
|
|
|
|
|
202
|
400 |
|
if ($name !== null) { |
|
203
|
336 |
|
$name = ' '.$name; |
|
204
|
168 |
|
} |
|
205
|
|
|
|
|
206
|
400 |
|
return $this->renderCode($this->renderLines([ |
|
207
|
400 |
|
'function'.$name.$separator.$this->renderArguments($arguments).$separator.'{', |
|
208
|
400 |
|
$this->renderIndentation($code), |
|
209
|
400 |
|
'}', |
|
210
|
400 |
|
], !empty($code), $newLine && !$semicolon), $semicolon, $newLine && $semicolon); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* @param VariableAwareInterface $object |
|
215
|
|
|
* @param string $declaration |
|
216
|
|
|
* @param bool $semicolon |
|
217
|
|
|
* @param bool $newLine |
|
218
|
|
|
* |
|
219
|
|
|
* @return string |
|
220
|
|
|
*/ |
|
221
|
468 |
|
public function renderObjectAssignment( |
|
222
|
|
|
VariableAwareInterface $object, |
|
223
|
|
|
$declaration, |
|
224
|
|
|
$semicolon = false, |
|
225
|
|
|
$newLine = false |
|
226
|
|
|
) { |
|
227
|
468 |
|
return $this->renderAssignment($object->getVariable(), $declaration, $semicolon, $newLine); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* @param VariableAwareInterface $root |
|
232
|
|
|
* @param string $declaration |
|
233
|
|
|
* @param string|null $propertyPath |
|
234
|
|
|
* @param VariableAwareInterface|null $object |
|
235
|
|
|
* @param bool $semicolon |
|
236
|
|
|
* @param bool $newLine |
|
237
|
|
|
* |
|
238
|
|
|
* @return string |
|
239
|
|
|
*/ |
|
240
|
312 |
|
public function renderContainerAssignment( |
|
241
|
|
|
VariableAwareInterface $root, |
|
242
|
|
|
$declaration, |
|
243
|
|
|
$propertyPath = null, |
|
244
|
|
|
VariableAwareInterface $object = null, |
|
245
|
|
|
$semicolon = true, |
|
246
|
|
|
$newLine = true |
|
247
|
|
|
) { |
|
248
|
312 |
|
return $this->renderAssignment( |
|
249
|
312 |
|
$this->renderContainerVariable($root, $propertyPath, $object), |
|
250
|
156 |
|
$declaration, |
|
251
|
156 |
|
$semicolon, |
|
252
|
|
|
$newLine |
|
253
|
156 |
|
); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* @param VariableAwareInterface $root |
|
258
|
|
|
* @param string|null $propertyPath |
|
259
|
|
|
* @param VariableAwareInterface|null $object |
|
260
|
|
|
* |
|
261
|
|
|
* @return string |
|
262
|
|
|
*/ |
|
263
|
328 |
|
public function renderContainerVariable( |
|
264
|
|
|
VariableAwareInterface $root, |
|
265
|
|
|
$propertyPath = null, |
|
266
|
|
|
VariableAwareInterface $object = null |
|
267
|
|
|
) { |
|
268
|
328 |
|
$variable = $root->getVariable().'_container'; |
|
269
|
|
|
|
|
270
|
328 |
|
if ($propertyPath !== null) { |
|
271
|
288 |
|
$variable = $this->renderProperty($variable, $propertyPath); |
|
272
|
144 |
|
} |
|
273
|
|
|
|
|
274
|
328 |
|
if ($object !== null) { |
|
275
|
264 |
|
$variable = $this->renderProperty($variable, $object->getVariable()); |
|
276
|
132 |
|
} |
|
277
|
|
|
|
|
278
|
328 |
|
return $variable; |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
/** |
|
282
|
|
|
* @param string $variable |
|
283
|
|
|
* @param string $declaration |
|
284
|
|
|
* @param bool $semicolon |
|
285
|
|
|
* @param bool $newLine |
|
286
|
|
|
* |
|
287
|
|
|
* @return string |
|
288
|
|
|
*/ |
|
289
|
592 |
|
public function renderAssignment($variable, $declaration, $semicolon = false, $newLine = false) |
|
290
|
|
|
{ |
|
291
|
592 |
|
$separator = $this->renderSeparator(); |
|
292
|
|
|
|
|
293
|
592 |
|
return $this->renderCode($variable.$separator.'='.$separator.$declaration, $semicolon, $newLine); |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
/** |
|
297
|
|
|
* @param string $statement |
|
298
|
|
|
* @param string $code |
|
299
|
|
|
* @param string|null $condition |
|
300
|
|
|
* @param string|null $next |
|
301
|
|
|
* @param bool $newLine |
|
302
|
|
|
* |
|
303
|
|
|
* @return string |
|
304
|
|
|
*/ |
|
305
|
320 |
|
public function renderStatement($statement, $code, $condition = null, $next = null, $newLine = true) |
|
306
|
|
|
{ |
|
307
|
320 |
|
$separator = $this->renderSeparator(); |
|
308
|
320 |
|
$statement .= $separator; |
|
309
|
|
|
|
|
310
|
320 |
|
if (!empty($condition)) { |
|
311
|
312 |
|
$statement .= $this->renderArguments([$condition]).$separator; |
|
312
|
156 |
|
} |
|
313
|
|
|
|
|
314
|
320 |
|
if (!empty($next)) { |
|
315
|
292 |
|
$next = $separator.$next; |
|
316
|
146 |
|
} |
|
317
|
|
|
|
|
318
|
320 |
|
return $this->renderLines([ |
|
319
|
320 |
|
$statement.'{', |
|
320
|
320 |
|
$this->renderIndentation($code), |
|
321
|
320 |
|
'}'.$next, |
|
322
|
320 |
|
], true, $newLine); |
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
|
|
/** |
|
326
|
|
|
* @param string $code |
|
327
|
|
|
* @param bool $semicolon |
|
328
|
|
|
* @param bool $newLine |
|
329
|
|
|
* |
|
330
|
|
|
* @return string |
|
331
|
|
|
*/ |
|
332
|
928 |
|
public function renderCode($code, $semicolon = true, $newLine = true) |
|
333
|
|
|
{ |
|
334
|
928 |
|
if ($semicolon) { |
|
335
|
516 |
|
$code .= ';'; |
|
336
|
258 |
|
} |
|
337
|
|
|
|
|
338
|
928 |
|
return $this->renderLine($code, $newLine); |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
|
|
/** |
|
342
|
|
|
* @param string|null $code |
|
343
|
|
|
* |
|
344
|
|
|
* @return string |
|
345
|
|
|
*/ |
|
346
|
584 |
|
public function renderIndentation($code = null) |
|
347
|
|
|
{ |
|
348
|
584 |
|
if ($this->debug && !empty($code)) { |
|
349
|
84 |
|
$indentation = str_repeat(' ', $this->indentationStep); |
|
350
|
84 |
|
$code = $indentation.str_replace("\n", "\n".$indentation, $code); |
|
351
|
42 |
|
} |
|
352
|
|
|
|
|
353
|
584 |
|
return (string) $code; |
|
354
|
|
|
} |
|
355
|
|
|
|
|
356
|
|
|
/** |
|
357
|
|
|
* @param string[] $codes |
|
358
|
|
|
* @param bool $newLine |
|
359
|
|
|
* @param bool $eolLine |
|
360
|
|
|
* |
|
361
|
|
|
* @return string |
|
362
|
|
|
*/ |
|
363
|
608 |
|
public function renderLines(array $codes, $newLine = true, $eolLine = true) |
|
364
|
|
|
{ |
|
365
|
608 |
|
$result = ''; |
|
366
|
608 |
|
$count = count($codes); |
|
367
|
|
|
|
|
368
|
608 |
|
for ($index = 0; $index < $count; ++$index) { |
|
369
|
600 |
|
$result .= $this->renderLine($codes[$index], $newLine && $index !== $count - 1); |
|
370
|
300 |
|
} |
|
371
|
|
|
|
|
372
|
608 |
|
return $this->renderLine($result, $eolLine); |
|
373
|
|
|
} |
|
374
|
|
|
|
|
375
|
|
|
/** |
|
376
|
|
|
* @param string|null $code |
|
377
|
|
|
* @param bool $newLine |
|
378
|
|
|
* |
|
379
|
|
|
* @return string |
|
380
|
|
|
*/ |
|
381
|
1128 |
|
public function renderLine($code = null, $newLine = true) |
|
382
|
|
|
{ |
|
383
|
1128 |
|
if ($newLine && !empty($code) && $this->debug) { |
|
384
|
184 |
|
$code .= "\n"; |
|
385
|
92 |
|
} |
|
386
|
|
|
|
|
387
|
1128 |
|
return (string) $code; |
|
388
|
|
|
} |
|
389
|
|
|
|
|
390
|
|
|
/** |
|
391
|
|
|
* @param string $argument |
|
392
|
|
|
* |
|
393
|
|
|
* @return string |
|
394
|
|
|
*/ |
|
395
|
544 |
|
public function renderEscape($argument) |
|
396
|
|
|
{ |
|
397
|
544 |
|
return json_encode($argument, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
|
398
|
|
|
} |
|
399
|
|
|
|
|
400
|
|
|
/** |
|
401
|
|
|
* @return string |
|
402
|
|
|
*/ |
|
403
|
976 |
|
public function renderSeparator() |
|
404
|
|
|
{ |
|
405
|
976 |
|
return $this->debug ? ' ' : ''; |
|
406
|
|
|
} |
|
407
|
|
|
|
|
408
|
|
|
/** |
|
409
|
|
|
* @param string[] $arguments |
|
410
|
|
|
* |
|
411
|
|
|
* @return string |
|
412
|
|
|
*/ |
|
413
|
740 |
|
private function renderArguments(array $arguments) |
|
414
|
|
|
{ |
|
415
|
740 |
|
return '('.implode(','.$this->renderSeparator(), $arguments).')'; |
|
416
|
|
|
} |
|
417
|
|
|
} |
|
418
|
|
|
|