Completed
Push — master ( 25fb08...38f401 )
by Mewes
02:25
created

XlsSheetWrapper::increaseRow()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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