Passed
Push — master ( 115e39...72a36a )
by Mark
10:46
created

Styles::readFontStyle()   F

Complexity

Conditions 15
Paths 384

Size

Total Lines 33
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 15

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 15
eloc 22
c 1
b 0
f 0
nc 384
nop 2
dl 0
loc 33
ccs 23
cts 23
cp 1
crap 15
rs 2.7833

How to fix   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\Reader\Xlsx;
4
5
use PhpOffice\PhpSpreadsheet\Style\Alignment;
6
use PhpOffice\PhpSpreadsheet\Style\Border;
7
use PhpOffice\PhpSpreadsheet\Style\Borders;
8
use PhpOffice\PhpSpreadsheet\Style\Color;
9
use PhpOffice\PhpSpreadsheet\Style\Fill;
10
use PhpOffice\PhpSpreadsheet\Style\Font;
11
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
12
use PhpOffice\PhpSpreadsheet\Style\Protection;
13
use PhpOffice\PhpSpreadsheet\Style\Style;
14
use SimpleXMLElement;
15
16
class Styles extends BaseParserClass
17
{
18
    /**
19
     * Theme instance.
20
     *
21
     * @var Theme
22
     */
23
    private static $theme = null;
24
25
    private $styles = [];
26
27
    private $cellStyles = [];
28
29
    private $styleXml;
30
31 132
    public function __construct(SimpleXMLElement $styleXml)
32
    {
33 132
        $this->styleXml = $styleXml;
34 132
    }
35
36 132
    public function setStyleBaseData(?Theme $theme = null, $styles = [], $cellStyles = []): void
37
    {
38 132
        self::$theme = $theme;
39 132
        $this->styles = $styles;
40 132
        $this->cellStyles = $cellStyles;
41 132
    }
42
43 132
    public static function readFontStyle(Font $fontStyle, SimpleXMLElement $fontStyleXml): void
44
    {
45 132
        if (isset($fontStyleXml->name, $fontStyleXml->name['val'])) {
46 132
            $fontStyle->setName((string) $fontStyleXml->name['val']);
47
        }
48 132
        if (isset($fontStyleXml->sz, $fontStyleXml->sz['val'])) {
49 132
            $fontStyle->setSize((float) $fontStyleXml->sz['val']);
50
        }
51 132
        if (isset($fontStyleXml->b)) {
52 76
            $fontStyle->setBold(!isset($fontStyleXml->b['val']) || self::boolean((string) $fontStyleXml->b['val']));
53
        }
54 132
        if (isset($fontStyleXml->i)) {
55 70
            $fontStyle->setItalic(!isset($fontStyleXml->i['val']) || self::boolean((string) $fontStyleXml->i['val']));
56
        }
57 132
        if (isset($fontStyleXml->strike)) {
58 66
            $fontStyle->setStrikethrough(
59 66
                !isset($fontStyleXml->strike['val']) || self::boolean((string) $fontStyleXml->strike['val'])
60
            );
61
        }
62 132
        $fontStyle->getColor()->setARGB(self::readColor($fontStyleXml->color));
63
64 132
        if (isset($fontStyleXml->u) && !isset($fontStyleXml->u['val'])) {
65 1
            $fontStyle->setUnderline(Font::UNDERLINE_SINGLE);
66 132
        } elseif (isset($fontStyleXml->u, $fontStyleXml->u['val'])) {
67 65
            $fontStyle->setUnderline((string) $fontStyleXml->u['val']);
68
        }
69
70 132
        if (isset($fontStyleXml->vertAlign, $fontStyleXml->vertAlign['val'])) {
71 1
            $verticalAlign = strtolower((string) $fontStyleXml->vertAlign['val']);
72 1
            if ($verticalAlign === 'superscript') {
73 1
                $fontStyle->setSuperscript(true);
74 1
            } elseif ($verticalAlign === 'subscript') {
75 1
                $fontStyle->setSubscript(true);
76
            }
77
        }
78 132
    }
79
80 12
    private static function readNumberFormat(NumberFormat $numfmtStyle, SimpleXMLElement $numfmtStyleXml): void
81
    {
82 12
        if ($numfmtStyleXml->count() === 0) {
83 11
            return;
84
        }
85 2
        $numfmt = $numfmtStyleXml->attributes();
86 2
        if ($numfmt->count() > 0 && isset($numfmt['formatCode'])) {
87 2
            $numfmtStyle->setFormatCode((string) $numfmt['formatCode']);
88
        }
89 2
    }
90
91 132
    public static function readFillStyle(Fill $fillStyle, SimpleXMLElement $fillStyleXml): void
92
    {
93 132
        if ($fillStyleXml->gradientFill) {
94
            /** @var SimpleXMLElement $gradientFill */
95 2
            $gradientFill = $fillStyleXml->gradientFill[0];
96 2
            if (!empty($gradientFill['type'])) {
97 2
                $fillStyle->setFillType((string) $gradientFill['type']);
98
            }
99 2
            $fillStyle->setRotation((float) ($gradientFill['degree']));
100 2
            $gradientFill->registerXPathNamespace('sml', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main');
101 2
            $fillStyle->getStartColor()->setARGB(
102 2
                self::readColor(self::getArrayItem($gradientFill->xpath('sml:stop[@position=0]'))->color)
103
            );
104 2
            $fillStyle->getEndColor()->setARGB(
105 2
                self::readColor(self::getArrayItem($gradientFill->xpath('sml:stop[@position=1]'))->color)
106
            );
107 132
        } elseif ($fillStyleXml->patternFill) {
108 132
            $defaultFillStyle = Fill::FILL_NONE;
109 132
            if ($fillStyleXml->patternFill->fgColor) {
110 9
                $fillStyle->getStartColor()->setARGB(self::readColor($fillStyleXml->patternFill->fgColor, true));
111 9
                $defaultFillStyle = Fill::FILL_SOLID;
112
            }
113 132
            if ($fillStyleXml->patternFill->bgColor) {
114 18
                $fillStyle->getEndColor()->setARGB(self::readColor($fillStyleXml->patternFill->bgColor, true));
115 18
                $defaultFillStyle = Fill::FILL_SOLID;
116
            }
117
118 132
            $patternType = (string) $fillStyleXml->patternFill['patternType'] != ''
119 132
                ? (string) $fillStyleXml->patternFill['patternType']
120 132
                : $defaultFillStyle;
121
122 132
            $fillStyle->setFillType($patternType);
123
        }
124 132
    }
125
126 132
    public static function readBorderStyle(Borders $borderStyle, SimpleXMLElement $borderStyleXml): void
127
    {
128 132
        $diagonalUp = self::boolean((string) $borderStyleXml['diagonalUp']);
129 132
        $diagonalDown = self::boolean((string) $borderStyleXml['diagonalDown']);
130 132
        if (!$diagonalUp && !$diagonalDown) {
131 132
            $borderStyle->setDiagonalDirection(Borders::DIAGONAL_NONE);
132 1
        } elseif ($diagonalUp && !$diagonalDown) {
133 1
            $borderStyle->setDiagonalDirection(Borders::DIAGONAL_UP);
134 1
        } elseif (!$diagonalUp && $diagonalDown) {
135 1
            $borderStyle->setDiagonalDirection(Borders::DIAGONAL_DOWN);
136
        } else {
137
            $borderStyle->setDiagonalDirection(Borders::DIAGONAL_BOTH);
138
        }
139
140 132
        self::readBorder($borderStyle->getLeft(), $borderStyleXml->left);
141 132
        self::readBorder($borderStyle->getRight(), $borderStyleXml->right);
142 132
        self::readBorder($borderStyle->getTop(), $borderStyleXml->top);
143 132
        self::readBorder($borderStyle->getBottom(), $borderStyleXml->bottom);
144 132
        self::readBorder($borderStyle->getDiagonal(), $borderStyleXml->diagonal);
145 132
    }
146
147 132
    private static function readBorder(Border $border, SimpleXMLElement $borderXml): void
148
    {
149 132
        if (isset($borderXml['style'])) {
150 8
            $border->setBorderStyle((string) $borderXml['style']);
151
        }
152 132
        if (isset($borderXml->color)) {
153 8
            $border->getColor()->setARGB(self::readColor($borderXml->color));
154
        }
155 132
    }
156
157 132
    public static function readAlignmentStyle(Alignment $alignment, SimpleXMLElement $alignmentXml): void
158
    {
159 132
        $alignment->setHorizontal((string) $alignmentXml['horizontal']);
160 132
        $alignment->setVertical((string) $alignmentXml['vertical']);
161
162 132
        $textRotation = 0;
163 132
        if ((int) $alignmentXml['textRotation'] <= 90) {
164 132
            $textRotation = (int) $alignmentXml['textRotation'];
165
        } elseif ((int) $alignmentXml['textRotation'] > 90) {
166
            $textRotation = 90 - (int) $alignmentXml['textRotation'];
167
        }
168
169 132
        $alignment->setTextRotation((int) $textRotation);
170 132
        $alignment->setWrapText(self::boolean((string) $alignmentXml['wrapText']));
171 132
        $alignment->setShrinkToFit(self::boolean((string) $alignmentXml['shrinkToFit']));
172 132
        $alignment->setIndent(
173 132
            (int) ((string) $alignmentXml['indent']) > 0 ? (int) ((string) $alignmentXml['indent']) : 0
174
        );
175 132
        $alignment->setReadOrder(
176 132
            (int) ((string) $alignmentXml['readingOrder']) > 0 ? (int) ((string) $alignmentXml['readingOrder']) : 0
177
        );
178 132
    }
179
180 132
    private function readStyle(Style $docStyle, $style): void
181
    {
182 132
        if ($style->numFmt instanceof SimpleXMLElement) {
183 12
            self::readNumberFormat($docStyle->getNumberFormat(), $style->numFmt);
184
        } else {
185 132
            $docStyle->getNumberFormat()->setFormatCode($style->numFmt);
186
        }
187
188 132
        if (isset($style->font)) {
189 132
            self::readFontStyle($docStyle->getFont(), $style->font);
190
        }
191
192 132
        if (isset($style->fill)) {
193 132
            self::readFillStyle($docStyle->getFill(), $style->fill);
194
        }
195
196 132
        if (isset($style->border)) {
197 132
            self::readBorderStyle($docStyle->getBorders(), $style->border);
198
        }
199
200 132
        if (isset($style->alignment->alignment)) {
201
            self::readAlignmentStyle($docStyle->getAlignment(), $style->alignment);
202
        }
203
204
        // protection
205 132
        if (isset($style->protection)) {
206 132
            self::readProtectionLocked($docStyle, $style);
207 132
            self::readProtectionHidden($docStyle, $style);
208
        }
209
210
        // top-level style settings
211 132
        if (isset($style->quotePrefix)) {
212 132
            $docStyle->setQuotePrefix(true);
213
        }
214 132
    }
215
216 132
    public static function readProtectionLocked(Style $docStyle, $style): void
217
    {
218 132
        if (isset($style->protection['locked'])) {
219
            if (self::boolean((string) $style->protection['locked'])) {
220
                $docStyle->getProtection()->setLocked(Protection::PROTECTION_PROTECTED);
221
            } else {
222
                $docStyle->getProtection()->setLocked(Protection::PROTECTION_UNPROTECTED);
223
            }
224
        }
225 132
    }
226
227 132
    public static function readProtectionHidden(Style $docStyle, $style): void
228
    {
229 132
        if (isset($style->protection['hidden'])) {
230
            if (self::boolean((string) $style->protection['hidden'])) {
231
                $docStyle->getProtection()->setHidden(Protection::PROTECTION_PROTECTED);
232
            } else {
233
                $docStyle->getProtection()->setHidden(Protection::PROTECTION_UNPROTECTED);
234
            }
235
        }
236 132
    }
237
238 132
    public static function readColor($color, $background = false)
239
    {
240 132
        if (isset($color['rgb'])) {
241 108
            return (string) $color['rgb'];
242 49
        } elseif (isset($color['indexed'])) {
243 11
            return Color::indexedColor($color['indexed'] - 7, $background)->getARGB();
244 46
        } elseif (isset($color['theme'])) {
245 37
            if (self::$theme !== null) {
246 37
                $returnColour = self::$theme->getColourByIndex((int) $color['theme']);
247 37
                if (isset($color['tint'])) {
248 3
                    $tintAdjust = (float) $color['tint'];
249 3
                    $returnColour = Color::changeBrightness($returnColour, $tintAdjust);
250
                }
251
252 37
                return 'FF' . $returnColour;
253
            }
254
        }
255
256 17
        return ($background) ? 'FFFFFFFF' : 'FF000000';
257
    }
258
259 132
    public function dxfs($readDataOnly = false)
260
    {
261 132
        $dxfs = [];
262 132
        if (!$readDataOnly && $this->styleXml) {
263
            //    Conditional Styles
264 132
            if ($this->styleXml->dxfs) {
265 131
                foreach ($this->styleXml->dxfs->dxf as $dxf) {
266 12
                    $style = new Style(false, true);
267 12
                    $this->readStyle($style, $dxf);
268 12
                    $dxfs[] = $style;
269
                }
270
            }
271
            //    Cell Styles
272 132
            if ($this->styleXml->cellStyles) {
273 132
                foreach ($this->styleXml->cellStyles->cellStyle as $cellStyle) {
274 132
                    if ((int) ($cellStyle['builtinId']) == 0) {
275 132
                        if (isset($this->cellStyles[(int) ($cellStyle['xfId'])])) {
276
                            // Set default style
277 132
                            $style = new Style();
278 132
                            $this->readStyle($style, $this->cellStyles[(int) ($cellStyle['xfId'])]);
279
280
                            // normal style, currently not using it for anything
281
                        }
282
                    }
283
                }
284
            }
285
        }
286
287 132
        return $dxfs;
288
    }
289
290 132
    public function styles()
291
    {
292 132
        return $this->styles;
293
    }
294
295 2
    private static function getArrayItem($array, $key = 0)
296
    {
297 2
        return $array[$key] ?? null;
298
    }
299
}
300