Failed Conditions
Push — master ( 454d94...b2070f )
by Adrien
27:46
created

Styles::styles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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