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