Test Failed
Push — develop ( 90366f...812a46 )
by Adrien
28:16
created

Style::writeFont()   F

Complexity

Conditions 15
Paths 512

Size

Total Lines 71
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 38
CRAP Score 15

Importance

Changes 0
Metric Value
cc 15
eloc 37
nc 512
nop 2
dl 0
loc 71
ccs 38
cts 38
cp 1
crap 15
rs 3.472
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Writer\Xlsx;
4
5
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
6
use PhpOffice\PhpSpreadsheet\Shared\XMLWriter;
7
use PhpOffice\PhpSpreadsheet\Spreadsheet;
8
use PhpOffice\PhpSpreadsheet\Style\Border;
9
use PhpOffice\PhpSpreadsheet\Style\Borders;
10
use PhpOffice\PhpSpreadsheet\Style\Fill;
11
use PhpOffice\PhpSpreadsheet\Style\Font;
12
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
13
use PhpOffice\PhpSpreadsheet\Style\Protection;
14
15
class Style extends WriterPart
16
{
17
    /**
18
     * Write styles to XML format.
19
     *
20
     * @param Spreadsheet $spreadsheet
21
     *
22
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
23
     *
24
     * @return string XML Output
25
     */
26 56
    public function writeStyles(Spreadsheet $spreadsheet)
27
    {
28
        // Create XML writer
29 56
        $objWriter = null;
30 56 View Code Duplication
        if ($this->getParentWriter()->getUseDiskCaching()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
31
            $objWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
32
        } else {
33 56
            $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
34
        }
35
36
        // XML header
37 56
        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
38
39
        // styleSheet
40 56
        $objWriter->startElement('styleSheet');
41 56
        $objWriter->writeAttribute('xml:space', 'preserve');
42 56
        $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main');
43
44
        // numFmts
45 56
        $objWriter->startElement('numFmts');
46 56
        $objWriter->writeAttribute('count', $this->getParentWriter()->getNumFmtHashTable()->count());
47
48
        // numFmt
49 56
        for ($i = 0; $i < $this->getParentWriter()->getNumFmtHashTable()->count(); ++$i) {
50 15
            $this->writeNumFmt($objWriter, $this->getParentWriter()->getNumFmtHashTable()->getByIndex($i), $i);
51
        }
52
53 56
        $objWriter->endElement();
54
55
        // fonts
56 56
        $objWriter->startElement('fonts');
57 56
        $objWriter->writeAttribute('count', $this->getParentWriter()->getFontHashTable()->count());
58
59
        // font
60 56
        for ($i = 0; $i < $this->getParentWriter()->getFontHashTable()->count(); ++$i) {
61 56
            $this->writeFont($objWriter, $this->getParentWriter()->getFontHashTable()->getByIndex($i));
62
        }
63
64 56
        $objWriter->endElement();
65
66
        // fills
67 56
        $objWriter->startElement('fills');
68 56
        $objWriter->writeAttribute('count', $this->getParentWriter()->getFillHashTable()->count());
69
70
        // fill
71 56
        for ($i = 0; $i < $this->getParentWriter()->getFillHashTable()->count(); ++$i) {
72 56
            $this->writeFill($objWriter, $this->getParentWriter()->getFillHashTable()->getByIndex($i));
73
        }
74
75 56
        $objWriter->endElement();
76
77
        // borders
78 56
        $objWriter->startElement('borders');
79 56
        $objWriter->writeAttribute('count', $this->getParentWriter()->getBordersHashTable()->count());
80
81
        // border
82 56
        for ($i = 0; $i < $this->getParentWriter()->getBordersHashTable()->count(); ++$i) {
83 56
            $this->writeBorder($objWriter, $this->getParentWriter()->getBordersHashTable()->getByIndex($i));
84
        }
85
86 56
        $objWriter->endElement();
87
88
        // cellStyleXfs
89 56
        $objWriter->startElement('cellStyleXfs');
90 56
        $objWriter->writeAttribute('count', 1);
91
92
        // xf
93 56
        $objWriter->startElement('xf');
94 56
        $objWriter->writeAttribute('numFmtId', 0);
95 56
        $objWriter->writeAttribute('fontId', 0);
96 56
        $objWriter->writeAttribute('fillId', 0);
97 56
        $objWriter->writeAttribute('borderId', 0);
98 56
        $objWriter->endElement();
99
100 56
        $objWriter->endElement();
101
102
        // cellXfs
103 56
        $objWriter->startElement('cellXfs');
104 56
        $objWriter->writeAttribute('count', count($spreadsheet->getCellXfCollection()));
105
106
        // xf
107 56
        foreach ($spreadsheet->getCellXfCollection() as $cellXf) {
108 56
            $this->writeCellStyleXf($objWriter, $cellXf, $spreadsheet);
109
        }
110
111 56
        $objWriter->endElement();
112
113
        // cellStyles
114 56
        $objWriter->startElement('cellStyles');
115 56
        $objWriter->writeAttribute('count', 1);
116
117
        // cellStyle
118 56
        $objWriter->startElement('cellStyle');
119 56
        $objWriter->writeAttribute('name', 'Normal');
120 56
        $objWriter->writeAttribute('xfId', 0);
121 56
        $objWriter->writeAttribute('builtinId', 0);
122 56
        $objWriter->endElement();
123
124 56
        $objWriter->endElement();
125
126
        // dxfs
127 56
        $objWriter->startElement('dxfs');
128 56
        $objWriter->writeAttribute('count', $this->getParentWriter()->getStylesConditionalHashTable()->count());
129
130
        // dxf
131 56
        for ($i = 0; $i < $this->getParentWriter()->getStylesConditionalHashTable()->count(); ++$i) {
132 2
            $this->writeCellStyleDxf($objWriter, $this->getParentWriter()->getStylesConditionalHashTable()->getByIndex($i)->getStyle());
0 ignored issues
show
Bug introduced by
The method getStyle() does not exist on PhpOffice\PhpSpreadsheet\IComparable. It seems like you code against a sub-type of PhpOffice\PhpSpreadsheet\IComparable such as PhpOffice\PhpSpreadsheet\Worksheet\Worksheet or PhpOffice\PhpSpreadsheet\Style\Conditional. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

132
            $this->writeCellStyleDxf($objWriter, $this->getParentWriter()->getStylesConditionalHashTable()->getByIndex($i)->/** @scrutinizer ignore-call */ getStyle());
Loading history...
133
        }
134
135 56
        $objWriter->endElement();
136
137
        // tableStyles
138 56
        $objWriter->startElement('tableStyles');
139 56
        $objWriter->writeAttribute('defaultTableStyle', 'TableStyleMedium9');
140 56
        $objWriter->writeAttribute('defaultPivotStyle', 'PivotTableStyle1');
141 56
        $objWriter->endElement();
142
143 56
        $objWriter->endElement();
144
145
        // Return
146 56
        return $objWriter->getData();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $objWriter->getData() could also return false which is incompatible with the documented return type string. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
147
    }
148
149
    /**
150
     * Write Fill.
151
     *
152
     * @param XMLWriter $objWriter XML Writer
153
     * @param Fill $pFill Fill style
154
     *
155
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
156
     */
157 56
    private function writeFill(XMLWriter $objWriter, Fill $pFill)
158
    {
159
        // Check if this is a pattern type or gradient type
160 56
        if ($pFill->getFillType() === Fill::FILL_GRADIENT_LINEAR ||
161 56
            $pFill->getFillType() === Fill::FILL_GRADIENT_PATH) {
162
            // Gradient fill
163 5
            $this->writeGradientFill($objWriter, $pFill);
164 56
        } elseif ($pFill->getFillType() !== null) {
165
            // Pattern fill
166 56
            $this->writePatternFill($objWriter, $pFill);
167
        }
168 56
    }
169
170
    /**
171
     * Write Gradient Fill.
172
     *
173
     * @param XMLWriter $objWriter XML Writer
174
     * @param Fill $pFill Fill style
175
     *
176
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
177
     */
178 5
    private function writeGradientFill(XMLWriter $objWriter, Fill $pFill)
179
    {
180
        // fill
181 5
        $objWriter->startElement('fill');
182
183
        // gradientFill
184 5
        $objWriter->startElement('gradientFill');
185 5
        $objWriter->writeAttribute('type', $pFill->getFillType());
186 5
        $objWriter->writeAttribute('degree', $pFill->getRotation());
187
188
        // stop
189 5
        $objWriter->startElement('stop');
190 5
        $objWriter->writeAttribute('position', '0');
191
192
        // color
193 5
        $objWriter->startElement('color');
194 5
        $objWriter->writeAttribute('rgb', $pFill->getStartColor()->getARGB());
195 5
        $objWriter->endElement();
196
197 5
        $objWriter->endElement();
198
199
        // stop
200 5
        $objWriter->startElement('stop');
201 5
        $objWriter->writeAttribute('position', '1');
202
203
        // color
204 5
        $objWriter->startElement('color');
205 5
        $objWriter->writeAttribute('rgb', $pFill->getEndColor()->getARGB());
206 5
        $objWriter->endElement();
207
208 5
        $objWriter->endElement();
209
210 5
        $objWriter->endElement();
211
212 5
        $objWriter->endElement();
213 5
    }
214
215
    /**
216
     * Write Pattern Fill.
217
     *
218
     * @param XMLWriter $objWriter XML Writer
219
     * @param Fill $pFill Fill style
220
     *
221
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
222
     */
223 56
    private function writePatternFill(XMLWriter $objWriter, Fill $pFill)
224
    {
225
        // fill
226 56
        $objWriter->startElement('fill');
227
228
        // patternFill
229 56
        $objWriter->startElement('patternFill');
230 56
        $objWriter->writeAttribute('patternType', $pFill->getFillType());
231
232 56 View Code Duplication
        if ($pFill->getFillType() !== Fill::FILL_NONE) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
233
            // fgColor
234 56
            if ($pFill->getStartColor()->getARGB()) {
235 56
                $objWriter->startElement('fgColor');
236 56
                $objWriter->writeAttribute('rgb', $pFill->getStartColor()->getARGB());
237 56
                $objWriter->endElement();
238
            }
239
        }
240 56 View Code Duplication
        if ($pFill->getFillType() !== Fill::FILL_NONE) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
241
            // bgColor
242 56
            if ($pFill->getEndColor()->getARGB()) {
243 56
                $objWriter->startElement('bgColor');
244 56
                $objWriter->writeAttribute('rgb', $pFill->getEndColor()->getARGB());
245 56
                $objWriter->endElement();
246
            }
247
        }
248
249 56
        $objWriter->endElement();
250
251 56
        $objWriter->endElement();
252 56
    }
253
254
    /**
255
     * Write Font.
256
     *
257
     * @param XMLWriter $objWriter XML Writer
258
     * @param Font $pFont Font style
259
     *
260
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
261
     */
262 56
    private function writeFont(XMLWriter $objWriter, Font $pFont)
263
    {
264
        // font
265 56
        $objWriter->startElement('font');
266
        //    Weird! The order of these elements actually makes a difference when opening Xlsx
267
        //        files in Excel2003 with the compatibility pack. It's not documented behaviour,
268
        //        and makes for a real WTF!
269
270
        // Bold. We explicitly write this element also when false (like MS Office Excel 2007 does
271
        // for conditional formatting). Otherwise it will apparently not be picked up in conditional
272
        // formatting style dialog
273 56
        if ($pFont->getBold() !== null) {
274 56
            $objWriter->startElement('b');
275 56
            $objWriter->writeAttribute('val', $pFont->getBold() ? '1' : '0');
276 56
            $objWriter->endElement();
277
        }
278
279
        // Italic
280 56
        if ($pFont->getItalic() !== null) {
281 56
            $objWriter->startElement('i');
282 56
            $objWriter->writeAttribute('val', $pFont->getItalic() ? '1' : '0');
283 56
            $objWriter->endElement();
284
        }
285
286
        // Strikethrough
287 56
        if ($pFont->getStrikethrough() !== null) {
288 56
            $objWriter->startElement('strike');
289 56
            $objWriter->writeAttribute('val', $pFont->getStrikethrough() ? '1' : '0');
290 56
            $objWriter->endElement();
291
        }
292
293
        // Underline
294 56
        if ($pFont->getUnderline() !== null) {
295 56
            $objWriter->startElement('u');
296 56
            $objWriter->writeAttribute('val', $pFont->getUnderline());
297 56
            $objWriter->endElement();
298
        }
299
300
        // Superscript / subscript
301 56
        if ($pFont->getSuperscript() === true || $pFont->getSubscript() === true) {
302 1
            $objWriter->startElement('vertAlign');
303 1
            if ($pFont->getSuperscript() === true) {
304 1
                $objWriter->writeAttribute('val', 'superscript');
305 1
            } elseif ($pFont->getSubscript() === true) {
306 1
                $objWriter->writeAttribute('val', 'subscript');
307
            }
308 1
            $objWriter->endElement();
309
        }
310
311
        // Size
312 56
        if ($pFont->getSize() !== null) {
313 56
            $objWriter->startElement('sz');
314 56
            $objWriter->writeAttribute('val', StringHelper::formatNumber($pFont->getSize()));
315 56
            $objWriter->endElement();
316
        }
317
318
        // Foreground color
319 56
        if ($pFont->getColor()->getARGB() !== null) {
320 56
            $objWriter->startElement('color');
321 56
            $objWriter->writeAttribute('rgb', $pFont->getColor()->getARGB());
322 56
            $objWriter->endElement();
323
        }
324
325
        // Name
326 56
        if ($pFont->getName() !== null) {
327 56
            $objWriter->startElement('name');
328 56
            $objWriter->writeAttribute('val', $pFont->getName());
329 56
            $objWriter->endElement();
330
        }
331
332 56
        $objWriter->endElement();
333 56
    }
334
335
    /**
336
     * Write Border.
337
     *
338
     * @param XMLWriter $objWriter XML Writer
339
     * @param Borders $pBorders Borders style
340
     *
341
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
342
     */
343 56
    private function writeBorder(XMLWriter $objWriter, Borders $pBorders)
344
    {
345
        // Write border
346 56
        $objWriter->startElement('border');
347
        // Diagonal?
348 56
        switch ($pBorders->getDiagonalDirection()) {
349 56
            case Borders::DIAGONAL_UP:
350 1
                $objWriter->writeAttribute('diagonalUp', 'true');
351 1
                $objWriter->writeAttribute('diagonalDown', 'false');
352
353 1
                break;
354 56
            case Borders::DIAGONAL_DOWN:
355 1
                $objWriter->writeAttribute('diagonalUp', 'false');
356 1
                $objWriter->writeAttribute('diagonalDown', 'true');
357
358 1
                break;
359 56
            case Borders::DIAGONAL_BOTH:
360 1
                $objWriter->writeAttribute('diagonalUp', 'true');
361 1
                $objWriter->writeAttribute('diagonalDown', 'true');
362
363 1
                break;
364
        }
365
366
        // BorderPr
367 56
        $this->writeBorderPr($objWriter, 'left', $pBorders->getLeft());
368 56
        $this->writeBorderPr($objWriter, 'right', $pBorders->getRight());
369 56
        $this->writeBorderPr($objWriter, 'top', $pBorders->getTop());
370 56
        $this->writeBorderPr($objWriter, 'bottom', $pBorders->getBottom());
371 56
        $this->writeBorderPr($objWriter, 'diagonal', $pBorders->getDiagonal());
372 56
        $objWriter->endElement();
373 56
    }
374
375
    /**
376
     * Write Cell Style Xf.
377
     *
378
     * @param XMLWriter $objWriter XML Writer
379
     * @param \PhpOffice\PhpSpreadsheet\Style\Style $pStyle Style
380
     * @param Spreadsheet $spreadsheet Workbook
381
     *
382
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
383
     */
384 56
    private function writeCellStyleXf(XMLWriter $objWriter, \PhpOffice\PhpSpreadsheet\Style\Style $pStyle, Spreadsheet $spreadsheet)
385
    {
386
        // xf
387 56
        $objWriter->startElement('xf');
388 56
        $objWriter->writeAttribute('xfId', 0);
389 56
        $objWriter->writeAttribute('fontId', (int) $this->getParentWriter()->getFontHashTable()->getIndexForHashCode($pStyle->getFont()->getHashCode()));
390 56
        if ($pStyle->getQuotePrefix()) {
391 1
            $objWriter->writeAttribute('quotePrefix', 1);
392
        }
393
394 56
        if ($pStyle->getNumberFormat()->getBuiltInFormatCode() === false) {
395 15
            $objWriter->writeAttribute('numFmtId', (int) ($this->getParentWriter()->getNumFmtHashTable()->getIndexForHashCode($pStyle->getNumberFormat()->getHashCode()) + 164));
396
        } else {
397 56
            $objWriter->writeAttribute('numFmtId', (int) $pStyle->getNumberFormat()->getBuiltInFormatCode());
398
        }
399
400 56
        $objWriter->writeAttribute('fillId', (int) $this->getParentWriter()->getFillHashTable()->getIndexForHashCode($pStyle->getFill()->getHashCode()));
401 56
        $objWriter->writeAttribute('borderId', (int) $this->getParentWriter()->getBordersHashTable()->getIndexForHashCode($pStyle->getBorders()->getHashCode()));
402
403
        // Apply styles?
404 56
        $objWriter->writeAttribute('applyFont', ($spreadsheet->getDefaultStyle()->getFont()->getHashCode() != $pStyle->getFont()->getHashCode()) ? '1' : '0');
405 56
        $objWriter->writeAttribute('applyNumberFormat', ($spreadsheet->getDefaultStyle()->getNumberFormat()->getHashCode() != $pStyle->getNumberFormat()->getHashCode()) ? '1' : '0');
406 56
        $objWriter->writeAttribute('applyFill', ($spreadsheet->getDefaultStyle()->getFill()->getHashCode() != $pStyle->getFill()->getHashCode()) ? '1' : '0');
407 56
        $objWriter->writeAttribute('applyBorder', ($spreadsheet->getDefaultStyle()->getBorders()->getHashCode() != $pStyle->getBorders()->getHashCode()) ? '1' : '0');
408 56
        $objWriter->writeAttribute('applyAlignment', ($spreadsheet->getDefaultStyle()->getAlignment()->getHashCode() != $pStyle->getAlignment()->getHashCode()) ? '1' : '0');
409 56 View Code Duplication
        if ($pStyle->getProtection()->getLocked() != Protection::PROTECTION_INHERIT || $pStyle->getProtection()->getHidden() != Protection::PROTECTION_INHERIT) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
410 9
            $objWriter->writeAttribute('applyProtection', 'true');
411
        }
412
413
        // alignment
414 56
        $objWriter->startElement('alignment');
415 56
        $objWriter->writeAttribute('horizontal', $pStyle->getAlignment()->getHorizontal());
416 56
        $objWriter->writeAttribute('vertical', $pStyle->getAlignment()->getVertical());
417
418 56
        $textRotation = 0;
419 56 View Code Duplication
        if ($pStyle->getAlignment()->getTextRotation() >= 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
420 56
            $textRotation = $pStyle->getAlignment()->getTextRotation();
421
        } elseif ($pStyle->getAlignment()->getTextRotation() < 0) {
422
            $textRotation = 90 - $pStyle->getAlignment()->getTextRotation();
423
        }
424 56
        $objWriter->writeAttribute('textRotation', $textRotation);
425
426 56
        $objWriter->writeAttribute('wrapText', ($pStyle->getAlignment()->getWrapText() ? 'true' : 'false'));
427 56
        $objWriter->writeAttribute('shrinkToFit', ($pStyle->getAlignment()->getShrinkToFit() ? 'true' : 'false'));
428
429 56
        if ($pStyle->getAlignment()->getIndent() > 0) {
430
            $objWriter->writeAttribute('indent', $pStyle->getAlignment()->getIndent());
431
        }
432 56
        if ($pStyle->getAlignment()->getReadOrder() > 0) {
433
            $objWriter->writeAttribute('readingOrder', $pStyle->getAlignment()->getReadOrder());
434
        }
435 56
        $objWriter->endElement();
436
437
        // protection
438 56
        if ($pStyle->getProtection()->getLocked() != Protection::PROTECTION_INHERIT || $pStyle->getProtection()->getHidden() != Protection::PROTECTION_INHERIT) {
439 9
            $objWriter->startElement('protection');
440 9 View Code Duplication
            if ($pStyle->getProtection()->getLocked() != Protection::PROTECTION_INHERIT) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
441 8
                $objWriter->writeAttribute('locked', ($pStyle->getProtection()->getLocked() == Protection::PROTECTION_PROTECTED ? 'true' : 'false'));
442
            }
443 9 View Code Duplication
            if ($pStyle->getProtection()->getHidden() != Protection::PROTECTION_INHERIT) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
444 3
                $objWriter->writeAttribute('hidden', ($pStyle->getProtection()->getHidden() == Protection::PROTECTION_PROTECTED ? 'true' : 'false'));
445
            }
446 9
            $objWriter->endElement();
447
        }
448
449 56
        $objWriter->endElement();
450 56
    }
451
452
    /**
453
     * Write Cell Style Dxf.
454
     *
455
     * @param XMLWriter $objWriter XML Writer
456
     * @param \PhpOffice\PhpSpreadsheet\Style\Style $pStyle Style
457
     *
458
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
459
     */
460 2
    private function writeCellStyleDxf(XMLWriter $objWriter, \PhpOffice\PhpSpreadsheet\Style\Style $pStyle)
461
    {
462
        // dxf
463 2
        $objWriter->startElement('dxf');
464
465
        // font
466 2
        $this->writeFont($objWriter, $pStyle->getFont());
467
468
        // numFmt
469 2
        $this->writeNumFmt($objWriter, $pStyle->getNumberFormat());
470
471
        // fill
472 2
        $this->writeFill($objWriter, $pStyle->getFill());
473
474
        // alignment
475 2
        $objWriter->startElement('alignment');
476 2
        if ($pStyle->getAlignment()->getHorizontal() !== null) {
477
            $objWriter->writeAttribute('horizontal', $pStyle->getAlignment()->getHorizontal());
478
        }
479 2
        if ($pStyle->getAlignment()->getVertical() !== null) {
480
            $objWriter->writeAttribute('vertical', $pStyle->getAlignment()->getVertical());
481
        }
482
483 2
        if ($pStyle->getAlignment()->getTextRotation() !== null) {
484
            $textRotation = 0;
485 View Code Duplication
            if ($pStyle->getAlignment()->getTextRotation() >= 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
486
                $textRotation = $pStyle->getAlignment()->getTextRotation();
487
            } elseif ($pStyle->getAlignment()->getTextRotation() < 0) {
488
                $textRotation = 90 - $pStyle->getAlignment()->getTextRotation();
489
            }
490
            $objWriter->writeAttribute('textRotation', $textRotation);
491
        }
492 2
        $objWriter->endElement();
493
494
        // border
495 2
        $this->writeBorder($objWriter, $pStyle->getBorders());
496
497
        // protection
498 2
        if (($pStyle->getProtection()->getLocked() !== null) || ($pStyle->getProtection()->getHidden() !== null)) {
499
            if ($pStyle->getProtection()->getLocked() !== Protection::PROTECTION_INHERIT ||
500
                $pStyle->getProtection()->getHidden() !== Protection::PROTECTION_INHERIT) {
501
                $objWriter->startElement('protection');
502 View Code Duplication
                if (($pStyle->getProtection()->getLocked() !== null) &&
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
503
                    ($pStyle->getProtection()->getLocked() !== Protection::PROTECTION_INHERIT)) {
504
                    $objWriter->writeAttribute('locked', ($pStyle->getProtection()->getLocked() == Protection::PROTECTION_PROTECTED ? 'true' : 'false'));
505
                }
506 View Code Duplication
                if (($pStyle->getProtection()->getHidden() !== null) &&
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
507
                    ($pStyle->getProtection()->getHidden() !== Protection::PROTECTION_INHERIT)) {
508
                    $objWriter->writeAttribute('hidden', ($pStyle->getProtection()->getHidden() == Protection::PROTECTION_PROTECTED ? 'true' : 'false'));
509
                }
510
                $objWriter->endElement();
511
            }
512
        }
513
514 2
        $objWriter->endElement();
515 2
    }
516
517
    /**
518
     * Write BorderPr.
519
     *
520
     * @param XMLWriter $objWriter XML Writer
521
     * @param string $pName Element name
522
     * @param Border $pBorder Border style
523
     *
524
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
525
     */
526 56
    private function writeBorderPr(XMLWriter $objWriter, $pName, Border $pBorder)
527
    {
528
        // Write BorderPr
529 56
        if ($pBorder->getBorderStyle() != Border::BORDER_NONE) {
530 12
            $objWriter->startElement($pName);
531 12
            $objWriter->writeAttribute('style', $pBorder->getBorderStyle());
532
533
            // color
534 12
            $objWriter->startElement('color');
535 12
            $objWriter->writeAttribute('rgb', $pBorder->getColor()->getARGB());
536 12
            $objWriter->endElement();
537
538 12
            $objWriter->endElement();
539
        }
540 56
    }
541
542
    /**
543
     * Write NumberFormat.
544
     *
545
     * @param XMLWriter $objWriter XML Writer
546
     * @param NumberFormat $pNumberFormat Number Format
547
     * @param int $pId Number Format identifier
548
     *
549
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
550
     */
551 17
    private function writeNumFmt(XMLWriter $objWriter, NumberFormat $pNumberFormat, $pId = 0)
552
    {
553
        // Translate formatcode
554 17
        $formatCode = $pNumberFormat->getFormatCode();
555
556
        // numFmt
557 17
        if ($formatCode !== null) {
558 16
            $objWriter->startElement('numFmt');
559 16
            $objWriter->writeAttribute('numFmtId', ($pId + 164));
560 16
            $objWriter->writeAttribute('formatCode', $formatCode);
561 16
            $objWriter->endElement();
562
        }
563 17
    }
564
565
    /**
566
     * Get an array of all styles.
567
     *
568
     * @param Spreadsheet $spreadsheet
569
     *
570
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
571
     *
572
     * @return \PhpOffice\PhpSpreadsheet\Style\Style[] All styles in PhpSpreadsheet
573
     */
574 56
    public function allStyles(Spreadsheet $spreadsheet)
575
    {
576 56
        return $spreadsheet->getCellXfCollection();
577
    }
578
579
    /**
580
     * Get an array of all conditional styles.
581
     *
582
     * @param Spreadsheet $spreadsheet
583
     *
584
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
585
     *
586
     * @return Conditional[] All conditional styles in PhpSpreadsheet
587
     */
588 56
    public function allConditionalStyles(Spreadsheet $spreadsheet)
589
    {
590
        // Get an array of all styles
591 56
        $aStyles = [];
592
593 56
        $sheetCount = $spreadsheet->getSheetCount();
594 56
        for ($i = 0; $i < $sheetCount; ++$i) {
595 56
            foreach ($spreadsheet->getSheet($i)->getConditionalStylesCollection() as $conditionalStyles) {
596 2
                foreach ($conditionalStyles as $conditionalStyle) {
597 2
                    $aStyles[] = $conditionalStyle;
598
                }
599
            }
600
        }
601
602 56
        return $aStyles;
603
    }
604
605
    /**
606
     * Get an array of all fills.
607
     *
608
     * @param Spreadsheet $spreadsheet
609
     *
610
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
611
     *
612
     * @return Fill[] All fills in PhpSpreadsheet
613
     */
614 56
    public function allFills(Spreadsheet $spreadsheet)
615
    {
616
        // Get an array of unique fills
617 56
        $aFills = [];
618
619
        // Two first fills are predefined
620 56
        $fill0 = new Fill();
621 56
        $fill0->setFillType(Fill::FILL_NONE);
622 56
        $aFills[] = $fill0;
623
624 56
        $fill1 = new Fill();
625 56
        $fill1->setFillType(Fill::FILL_PATTERN_GRAY125);
626 56
        $aFills[] = $fill1;
627
        // The remaining fills
628 56
        $aStyles = $this->allStyles($spreadsheet);
629
        /** @var \PhpOffice\PhpSpreadsheet\Style\Style $style */
630 56
        foreach ($aStyles as $style) {
631 56
            if (!isset($aFills[$style->getFill()->getHashCode()])) {
632 56
                $aFills[$style->getFill()->getHashCode()] = $style->getFill();
633
            }
634
        }
635
636 56
        return $aFills;
637
    }
638
639
    /**
640
     * Get an array of all fonts.
641
     *
642
     * @param Spreadsheet $spreadsheet
643
     *
644
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
645
     *
646
     * @return Font[] All fonts in PhpSpreadsheet
647
     */
648 56 View Code Duplication
    public function allFonts(Spreadsheet $spreadsheet)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
649
    {
650
        // Get an array of unique fonts
651 56
        $aFonts = [];
652 56
        $aStyles = $this->allStyles($spreadsheet);
653
654
        /** @var \PhpOffice\PhpSpreadsheet\Style\Style $style */
655 56
        foreach ($aStyles as $style) {
656 56
            if (!isset($aFonts[$style->getFont()->getHashCode()])) {
657 56
                $aFonts[$style->getFont()->getHashCode()] = $style->getFont();
658
            }
659
        }
660
661 56
        return $aFonts;
662
    }
663
664
    /**
665
     * Get an array of all borders.
666
     *
667
     * @param Spreadsheet $spreadsheet
668
     *
669
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
670
     *
671
     * @return Borders[] All borders in PhpSpreadsheet
672
     */
673 56 View Code Duplication
    public function allBorders(Spreadsheet $spreadsheet)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
674
    {
675
        // Get an array of unique borders
676 56
        $aBorders = [];
677 56
        $aStyles = $this->allStyles($spreadsheet);
678
679
        /** @var \PhpOffice\PhpSpreadsheet\Style\Style $style */
680 56
        foreach ($aStyles as $style) {
681 56
            if (!isset($aBorders[$style->getBorders()->getHashCode()])) {
682 56
                $aBorders[$style->getBorders()->getHashCode()] = $style->getBorders();
683
            }
684
        }
685
686 56
        return $aBorders;
687
    }
688
689
    /**
690
     * Get an array of all number formats.
691
     *
692
     * @param Spreadsheet $spreadsheet
693
     *
694
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
695
     *
696
     * @return NumberFormat[] All number formats in PhpSpreadsheet
697
     */
698 56
    public function allNumberFormats(Spreadsheet $spreadsheet)
699
    {
700
        // Get an array of unique number formats
701 56
        $aNumFmts = [];
702 56
        $aStyles = $this->allStyles($spreadsheet);
703
704
        /** @var \PhpOffice\PhpSpreadsheet\Style\Style $style */
705 56
        foreach ($aStyles as $style) {
706 56
            if ($style->getNumberFormat()->getBuiltInFormatCode() === false && !isset($aNumFmts[$style->getNumberFormat()->getHashCode()])) {
707 56
                $aNumFmts[$style->getNumberFormat()->getHashCode()] = $style->getNumberFormat();
708
            }
709
        }
710
711 56
        return $aNumFmts;
712
    }
713
}
714