1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MewesK\TwigSpreadsheetBundle\Wrapper; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Exception; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class XlsSheetWrapper |
10
|
|
|
* |
11
|
|
|
* @package MewesK\TwigSpreadsheetBundle\Wrapper |
12
|
|
|
*/ |
13
|
|
|
class XlsSheetWrapper extends AbstractWrapper |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var int |
17
|
|
|
*/ |
18
|
|
|
public static $COLUMN_DEFAULT = 0; |
19
|
|
|
/** |
20
|
|
|
* @var int |
21
|
|
|
*/ |
22
|
|
|
public static $ROW_DEFAULT = 1; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $context; |
28
|
|
|
/** |
29
|
|
|
* @var \Twig_Environment |
30
|
|
|
*/ |
31
|
|
|
protected $environment; |
32
|
|
|
/** |
33
|
|
|
* @var XlsDocumentWrapper |
34
|
|
|
*/ |
35
|
|
|
protected $documentWrapper; |
36
|
|
|
/** |
37
|
|
|
* @var null|int |
38
|
|
|
*/ |
39
|
|
|
protected $row; |
40
|
|
|
/** |
41
|
|
|
* @var null|int |
42
|
|
|
*/ |
43
|
|
|
protected $column; |
44
|
|
|
/** |
45
|
|
|
* @var Worksheet |
46
|
|
|
*/ |
47
|
|
|
protected $object; |
48
|
|
|
/** |
49
|
|
|
* @var array |
50
|
|
|
*/ |
51
|
|
|
protected $attributes; |
52
|
|
|
/** |
53
|
|
|
* @var array |
54
|
|
|
*/ |
55
|
|
|
protected $mappings; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* XlsSheetWrapper constructor. |
59
|
|
|
* |
60
|
|
|
* @param array $context |
61
|
|
|
* @param \Twig_Environment $environment |
62
|
|
|
* @param XlsDocumentWrapper $documentWrapper |
63
|
|
|
*/ |
64
|
|
View Code Duplication |
public function __construct(array $context, \Twig_Environment $environment, XlsDocumentWrapper $documentWrapper) |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
$this->context = $context; |
67
|
|
|
$this->environment = $environment; |
68
|
|
|
$this->documentWrapper = $documentWrapper; |
69
|
|
|
|
70
|
|
|
$this->row = null; |
71
|
|
|
$this->column = null; |
72
|
|
|
|
73
|
|
|
$this->object = null; |
74
|
|
|
$this->attributes = []; |
75
|
|
|
$this->mappings = []; |
76
|
|
|
|
77
|
|
|
$this->initializeMappings(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
protected function initializeMappings() |
81
|
|
|
{ |
82
|
|
|
$this->mappings['autoFilter'] = function ($value) { |
83
|
|
|
$this->object->setAutoFilter($value); |
84
|
|
|
}; |
85
|
|
|
$this->mappings['columnDimension']['__multi'] = true; |
86
|
|
|
$this->mappings['columnDimension']['__object'] = function ($key = 'default') { |
87
|
|
|
return $key === 'default' ? $this->object->getDefaultColumnDimension() : $this->object->getColumnDimension($key); |
88
|
|
|
}; |
89
|
|
|
$this->mappings['columnDimension']['autoSize'] = function ($key, $value) { |
90
|
|
|
$this->mappings['columnDimension']['__object']($key)->setAutoSize($value); |
91
|
|
|
}; |
92
|
|
|
$this->mappings['columnDimension']['collapsed'] = function ($key, $value) { |
93
|
|
|
$this->mappings['columnDimension']['__object']($key)->setCollapsed($value); |
94
|
|
|
}; |
95
|
|
|
$this->mappings['columnDimension']['columnIndex'] = function ($key, $value) { |
96
|
|
|
$this->mappings['columnDimension']['__object']($key)->setColumnIndex($value); |
97
|
|
|
}; |
98
|
|
|
$this->mappings['columnDimension']['outlineLevel'] = function ($key, $value) { |
99
|
|
|
$this->mappings['columnDimension']['__object']($key)->setOutlineLevel($value); |
100
|
|
|
}; |
101
|
|
|
$this->mappings['columnDimension']['visible'] = function ($key, $value) { |
102
|
|
|
$this->mappings['columnDimension']['__object']($key)->setVisible($value); |
103
|
|
|
}; |
104
|
|
|
$this->mappings['columnDimension']['width'] = function ($key, $value) { |
105
|
|
|
$this->mappings['columnDimension']['__object']($key)->setWidth($value); |
106
|
|
|
}; |
107
|
|
|
$this->mappings['columnDimension']['xfIndex'] = function ($key, $value) { |
108
|
|
|
$this->mappings['columnDimension']['__object']($key)->setXfIndex($value); |
109
|
|
|
}; |
110
|
|
|
$this->mappings['pageMargins']['top'] = function ($value) { |
111
|
|
|
$this->object->getPageMargins()->setTop($value); |
112
|
|
|
}; |
113
|
|
|
$this->mappings['pageMargins']['bottom'] = function ($value) { |
114
|
|
|
$this->object->getPageMargins()->setBottom($value); |
115
|
|
|
}; |
116
|
|
|
$this->mappings['pageMargins']['left'] = function ($value) { |
117
|
|
|
$this->object->getPageMargins()->setLeft($value); |
118
|
|
|
}; |
119
|
|
|
$this->mappings['pageMargins']['right'] = function ($value) { |
120
|
|
|
$this->object->getPageMargins()->setRight($value); |
121
|
|
|
}; |
122
|
|
|
$this->mappings['pageMargins']['header'] = function ($value) { |
123
|
|
|
$this->object->getPageMargins()->setHeader($value); |
124
|
|
|
}; |
125
|
|
|
$this->mappings['pageMargins']['footer'] = function ($value) { |
126
|
|
|
$this->object->getPageMargins()->setFooter($value); |
127
|
|
|
}; |
128
|
|
|
$this->mappings['pageSetup']['fitToHeight'] = function ($value) { |
129
|
|
|
$this->object->getPageSetup()->setFitToHeight($value); |
130
|
|
|
}; |
131
|
|
|
$this->mappings['pageSetup']['fitToPage'] = function ($value) { |
132
|
|
|
$this->object->getPageSetup()->setFitToPage($value); |
133
|
|
|
}; |
134
|
|
|
$this->mappings['pageSetup']['fitToWidth'] = function ($value) { |
135
|
|
|
$this->object->getPageSetup()->setFitToWidth($value); |
136
|
|
|
}; |
137
|
|
|
$this->mappings['pageSetup']['horizontalCentered'] = function ($value) { |
138
|
|
|
$this->object->getPageSetup()->setHorizontalCentered($value); |
139
|
|
|
}; |
140
|
|
|
$this->mappings['pageSetup']['orientation'] = function ($value) { |
141
|
|
|
$this->object->getPageSetup()->setOrientation($value); |
142
|
|
|
}; |
143
|
|
|
$this->mappings['pageSetup']['paperSize'] = function ($value) { |
144
|
|
|
$this->object->getPageSetup()->setPaperSize($value); |
145
|
|
|
}; |
146
|
|
|
$this->mappings['pageSetup']['printArea'] = function ($value) { |
147
|
|
|
$this->object->getPageSetup()->setPrintArea($value); |
148
|
|
|
}; |
149
|
|
|
$this->mappings['pageSetup']['scale'] = function ($value) { |
150
|
|
|
$this->object->getPageSetup()->setScale($value); |
151
|
|
|
}; |
152
|
|
|
$this->mappings['pageSetup']['verticalCentered'] = function ($value) { |
153
|
|
|
$this->object->getPageSetup()->setVerticalCentered($value); |
154
|
|
|
}; |
155
|
|
|
$this->mappings['printGridlines'] = function ($value) { |
156
|
|
|
$this->object->setPrintGridlines($value); |
157
|
|
|
}; |
158
|
|
|
$this->mappings['protection']['autoFilter'] = function ($value) { |
159
|
|
|
$this->object->getProtection()->setAutoFilter($value); |
160
|
|
|
}; |
161
|
|
|
$this->mappings['protection']['deleteColumns'] = function ($value) { |
162
|
|
|
$this->object->getProtection()->setDeleteColumns($value); |
163
|
|
|
}; |
164
|
|
|
$this->mappings['protection']['deleteRows'] = function ($value) { |
165
|
|
|
$this->object->getProtection()->setDeleteRows($value); |
166
|
|
|
}; |
167
|
|
|
$this->mappings['protection']['formatCells'] = function ($value) { |
168
|
|
|
$this->object->getProtection()->setFormatCells($value); |
169
|
|
|
}; |
170
|
|
|
$this->mappings['protection']['formatColumns'] = function ($value) { |
171
|
|
|
$this->object->getProtection()->setFormatColumns($value); |
172
|
|
|
}; |
173
|
|
|
$this->mappings['protection']['formatRows'] = function ($value) { |
174
|
|
|
$this->object->getProtection()->setFormatRows($value); |
175
|
|
|
}; |
176
|
|
|
$this->mappings['protection']['insertColumns'] = function ($value) { |
177
|
|
|
$this->object->getProtection()->setInsertColumns($value); |
178
|
|
|
}; |
179
|
|
|
$this->mappings['protection']['insertHyperlinks'] = function ($value) { |
180
|
|
|
$this->object->getProtection()->setInsertHyperlinks($value); |
181
|
|
|
}; |
182
|
|
|
$this->mappings['protection']['insertRows'] = function ($value) { |
183
|
|
|
$this->object->getProtection()->setInsertRows($value); |
184
|
|
|
}; |
185
|
|
|
$this->mappings['protection']['objects'] = function ($value) { |
186
|
|
|
$this->object->getProtection()->setObjects($value); |
187
|
|
|
}; |
188
|
|
|
$this->mappings['protection']['password'] = function ($value) { |
189
|
|
|
$this->object->getProtection()->setPassword($value); |
190
|
|
|
}; |
191
|
|
|
$this->mappings['protection']['pivotTables'] = function ($value) { |
192
|
|
|
$this->object->getProtection()->setPivotTables($value); |
193
|
|
|
}; |
194
|
|
|
$this->mappings['protection']['scenarios'] = function ($value) { |
195
|
|
|
$this->object->getProtection()->setScenarios($value); |
196
|
|
|
}; |
197
|
|
|
$this->mappings['protection']['selectLockedCells'] = function ($value) { |
198
|
|
|
$this->object->getProtection()->setSelectLockedCells($value); |
199
|
|
|
}; |
200
|
|
|
$this->mappings['protection']['selectUnlockedCells'] = function ($value) { |
201
|
|
|
$this->object->getProtection()->setSelectUnlockedCells($value); |
202
|
|
|
}; |
203
|
|
|
$this->mappings['protection']['sheet'] = function ($value) { |
204
|
|
|
$this->object->getProtection()->setSheet($value); |
205
|
|
|
}; |
206
|
|
|
$this->mappings['protection']['sort'] = function ($value) { |
207
|
|
|
$this->object->getProtection()->setSort($value); |
208
|
|
|
}; |
209
|
|
|
$this->mappings['rightToLeft'] = function ($value) { |
210
|
|
|
$this->object->setRightToLeft($value); |
211
|
|
|
}; |
212
|
|
|
$this->mappings['rowDimension']['__multi'] = true; |
213
|
|
|
$this->mappings['rowDimension']['__object'] = function ($key) { |
214
|
|
|
return $key === 'default' ? $this->object->getDefaultRowDimension() : $this->object->getRowDimension($key); |
215
|
|
|
}; |
216
|
|
|
$this->mappings['rowDimension']['collapsed'] = function ($key, $value) { |
217
|
|
|
$this->mappings['rowDimension']['__object']($key)->setCollapsed($value); |
218
|
|
|
}; |
219
|
|
|
$this->mappings['rowDimension']['outlineLevel'] = function ($key, $value) { |
220
|
|
|
$this->mappings['rowDimension']['__object']($key)->setOutlineLevel($value); |
221
|
|
|
}; |
222
|
|
|
$this->mappings['rowDimension']['rowHeight'] = function ($key, $value) { |
223
|
|
|
$this->mappings['rowDimension']['__object']($key)->setRowHeight($value); |
224
|
|
|
}; |
225
|
|
|
$this->mappings['rowDimension']['rowIndex'] = function ($key, $value) { |
226
|
|
|
$this->mappings['rowDimension']['__object']($key)->setRowIndex($value); |
227
|
|
|
}; |
228
|
|
|
$this->mappings['rowDimension']['visible'] = function ($key, $value) { |
229
|
|
|
$this->mappings['rowDimension']['__object']($key)->setVisible($value); |
230
|
|
|
}; |
231
|
|
|
$this->mappings['rowDimension']['xfIndex'] = function ($key, $value) { |
232
|
|
|
$this->mappings['rowDimension']['__object']($key)->setXfIndex($value); |
233
|
|
|
}; |
234
|
|
|
$this->mappings['rowDimension']['zeroHeight'] = function ($key, $value) { |
235
|
|
|
$this->mappings['rowDimension']['__object']($key)->setZeroHeight($value); |
236
|
|
|
}; |
237
|
|
|
$this->mappings['sheetState'] = function ($value) { |
238
|
|
|
$this->object->setSheetState($value); |
239
|
|
|
}; |
240
|
|
|
$this->mappings['showGridlines'] = function ($value) { |
241
|
|
|
$this->object->setShowGridlines($value); |
242
|
|
|
}; |
243
|
|
|
$this->mappings['tabColor'] = function ($value) { |
244
|
|
|
$this->object->getTabColor()->setRGB($value); |
245
|
|
|
}; |
246
|
|
|
$this->mappings['zoomScale'] = function ($value) { |
247
|
|
|
$this->object->getSheetView()->setZoomScale($value); |
248
|
|
|
}; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @param int|string|null $index |
253
|
|
|
* @param array $properties |
254
|
|
|
* @throws \PhpOffice\PhpSpreadsheet\Exception |
255
|
|
|
*/ |
256
|
|
|
public function start($index, array $properties = []) |
257
|
|
|
{ |
258
|
|
|
if (is_int($index) && $index <$this->documentWrapper->getObject()->getSheetCount()) { |
259
|
|
|
$this->object = $this->documentWrapper->getObject()->setActiveSheetIndex($index); |
260
|
|
|
} elseif (is_string($index)) { |
261
|
|
|
if (!$this->documentWrapper->getObject()->sheetNameExists($index)) { |
262
|
|
|
// create new sheet with a name |
263
|
|
|
$this->documentWrapper->getObject()->createSheet()->setTitle($index); |
264
|
|
|
} |
265
|
|
|
$this->object = $this->documentWrapper->getObject()->setActiveSheetIndexByName($index); |
266
|
|
|
} else { |
267
|
|
|
// create new sheet without a name |
268
|
|
|
$this->documentWrapper->getObject()->createSheet(); |
269
|
|
|
$this->object = $this->documentWrapper->getObject()->setActiveSheetIndex(0); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
$this->attributes['index'] = $index; |
273
|
|
|
$this->attributes['properties'] = $properties; |
274
|
|
|
|
275
|
|
|
$this->setProperties($properties, $this->mappings); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* @throws \PhpOffice\PhpSpreadsheet\Exception |
280
|
|
|
*/ |
281
|
|
|
public function end() |
282
|
|
|
{ |
283
|
|
|
// auto-size columns |
284
|
|
|
if ( |
285
|
|
|
isset($this->attributes['properties']['columnDimension']) && |
286
|
|
|
is_array($this->attributes['properties']['columnDimension']) |
287
|
|
|
) { |
288
|
|
|
/** |
289
|
|
|
* @var array $columnDimension |
290
|
|
|
*/ |
291
|
|
|
$columnDimension = $this->attributes['properties']['columnDimension']; |
292
|
|
|
foreach ($columnDimension as $key => $value) { |
293
|
|
|
if(isset($value['autoSize'])) { |
294
|
|
|
if ('default' === $key) { |
295
|
|
|
try { |
296
|
|
|
$cellIterator = $this->object->getRowIterator()->current()->getCellIterator(); |
297
|
|
|
$cellIterator->setIterateOnlyExistingCells(true); |
298
|
|
|
|
299
|
|
|
foreach ($cellIterator as $cell) { |
300
|
|
|
$this->object->getColumnDimension($cell->getColumn())->setAutoSize($value['autoSize']); |
301
|
|
|
} |
302
|
|
|
} catch (Exception $e) { |
303
|
|
|
// ignore exceptions thrown when no cells are defined |
304
|
|
|
} |
305
|
|
|
} else { |
306
|
|
|
$this->object->getColumnDimension($key)->setAutoSize($value['autoSize']); |
307
|
|
|
} |
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
$this->object = null; |
313
|
|
|
$this->attributes = []; |
314
|
|
|
$this->row = null; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
// |
318
|
|
|
// Helpers |
319
|
|
|
// |
320
|
|
|
|
321
|
|
|
public function increaseRow() |
322
|
|
|
{ |
323
|
|
|
$this->row = $this->row === null ? self::$ROW_DEFAULT : $this->row + 1; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
public function increaseColumn() |
327
|
|
|
{ |
328
|
|
|
$this->column = $this->column === null ? self::$COLUMN_DEFAULT : $this->column + 1; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
// |
332
|
|
|
// Getters/Setters |
333
|
|
|
// |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* @return int|null |
337
|
|
|
*/ |
338
|
|
|
public function getRow() |
339
|
|
|
{ |
340
|
|
|
return $this->row; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* @param int|null $row |
345
|
|
|
*/ |
346
|
|
|
public function setRow($row) |
347
|
|
|
{ |
348
|
|
|
$this->row = $row; |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* @return int|null |
353
|
|
|
*/ |
354
|
|
|
public function getColumn() |
355
|
|
|
{ |
356
|
|
|
return $this->column; |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* @param int|null $column |
361
|
|
|
*/ |
362
|
|
|
public function setColumn($column) |
363
|
|
|
{ |
364
|
|
|
$this->column = $column; |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* @return Worksheet |
369
|
|
|
*/ |
370
|
|
|
public function getObject(): Worksheet |
371
|
|
|
{ |
372
|
|
|
return $this->object; |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* @param Worksheet $object |
377
|
|
|
*/ |
378
|
|
|
public function setObject(Worksheet $object) |
379
|
|
|
{ |
380
|
|
|
$this->object = $object; |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
/** |
384
|
|
|
* @return array |
385
|
|
|
*/ |
386
|
|
|
public function getAttributes(): array |
387
|
|
|
{ |
388
|
|
|
return $this->attributes; |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
/** |
392
|
|
|
* @param array $attributes |
393
|
|
|
*/ |
394
|
|
|
public function setAttributes(array $attributes) |
395
|
|
|
{ |
396
|
|
|
$this->attributes = $attributes; |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
/** |
400
|
|
|
* @return array |
401
|
|
|
*/ |
402
|
|
|
public function getMappings(): array |
403
|
|
|
{ |
404
|
|
|
return $this->mappings; |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
/** |
408
|
|
|
* @param array $mappings |
409
|
|
|
*/ |
410
|
|
|
public function setMappings(array $mappings) |
411
|
|
|
{ |
412
|
|
|
$this->mappings = $mappings; |
413
|
|
|
} |
414
|
|
|
} |
415
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.