Completed
Push — master ( cc4cee...40bddf )
by Mewes
09:51
created

XlsSheetWrapper::end()   D

Complexity

Conditions 9
Paths 2

Size

Total Lines 41
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

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