Failed Conditions
Pull Request — master (#200)
by Hura
03:45 queued 01:12
created

StyleHelper::getFontsSectionContent()   B

Complexity

Conditions 6
Paths 17

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 6

Importance

Changes 4
Bugs 0 Features 2
Metric Value
c 4
b 0
f 2
dl 0
loc 32
rs 8.439
ccs 23
cts 23
cp 1
cc 6
eloc 18
nc 17
nop 0
crap 6
1
<?php
2
3
namespace Box\Spout\Writer\XLSX\Helper;
4
5
use Box\Spout\Writer\Common\Helper\AbstractStyleHelper;
6
use Box\Spout\Writer\Style\Color;
7
8
/**
9
 * Class StyleHelper
10
 * This class provides helper functions to manage styles
11
 *
12
 * @package Box\Spout\Writer\XLSX\Helper
13
 */
14
class StyleHelper extends AbstractStyleHelper
15
{
16
    /**
17
     * Returns the content of the "styles.xml" file, given a list of styles.
18
     *
19
     * @return string
20
     */
21 66
    public function getStylesXMLFileContent()
22
    {
23
        $content = <<<EOD
24
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
25
<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
26 66
EOD;
27
28 66
        $content .= $this->getFontsSectionContent();
29 66
        $content .= $this->getFillsSectionContent();
30 66
        $content .= $this->getBordersSectionContent();
31 66
        $content .= $this->getCellStyleXfsSectionContent();
32 66
        $content .= $this->getCellXfsSectionContent();
33 66
        $content .= $this->getCellStylesSectionContent();
34
35
        $content .= <<<EOD
36
</styleSheet>
37 66
EOD;
38
39 66
        return $content;
40
    }
41
42
    /**
43
     * Returns the content of the "<fonts>" section.
44
     *
45
     * @return string
46
     */
47 66
    protected function getFontsSectionContent()
48
    {
49 66
        $content = '<fonts count="' . count($this->styleIdToStyleMappingTable) . '">';
50
51
        /** @var \Box\Spout\Writer\Style\Style $style */
52 66
        foreach ($this->getRegisteredStyles() as $style) {
53 66
            $content .= '<font>';
54
55 66
            $content .= '<sz val="' . $style->getFontSize() . '"/>';
56 66
            $content .= '<color rgb="' . Color::toARGB($style->getFontColor()) . '"/>';
57 66
            $content .= '<name val="' . $style->getFontName() . '"/>';
58
59 66
            if ($style->isFontBold()) {
60 9
                $content .= '<b/>';
61 9
            }
62 66
            if ($style->isFontItalic()) {
63 3
                $content .= '<i/>';
64 3
            }
65 66
            if ($style->isFontUnderline()) {
66 3
                $content .= '<u/>';
67 3
            }
68 66
            if ($style->isFontStrikethrough()) {
69 3
                $content .= '<strike/>';
70 3
            }
71
72 66
            $content .= '</font>';
73 66
        }
74
75 66
        $content .= '</fonts>';
76
77 66
        return $content;
78
    }
79
80
    /**
81
     * Returns the content of the "<fills>" section.
82
     *
83
     * @return string
84
     */
85 66
    protected function getFillsSectionContent()
86
    {
87
        return <<<EOD
88
<fills count="1">
89
    <fill>
90
        <patternFill patternType="none"/>
91
    </fill>
92
</fills>
93 66
EOD;
94
    }
95
96
    /**
97
     * Returns the content of the "<borders>" section.
98
     *
99
     * @return string
100
     */
101 66
    protected function getBordersSectionContent()
102
    {
103 66
        $content = '<borders count="' . count($this->styleIdToStyleMappingTable) . '">';
104
105
        /** @var \Box\Spout\Writer\Style\Style $style */
106 66
        foreach ($this->getRegisteredStyles() as $style) {
107 66
            $border = $style->getBorder();
108 66
            if ($border) {
109
                $content .= '<border>';
110
                foreach ($border->getParts() as $part) {
111
                    /** @var $part \Box\Spout\Writer\Style\BorderPart */
112
                    $content .= BorderHelper::serializeBorderPart($part);
113
                }
114
                $content .= '</border>';
115
            } else {
116 66
                $content .= '<border><left/><right/><top/><bottom/><diagonal/></border>';
117
            }
118 66
        }
119
120 66
        $content .= '</borders>';
121
122 66
        return $content;
123
    }
124
125
    /**
126
     * Returns the content of the "<cellStyleXfs>" section.
127
     *
128
     * @return string
129
     */
130 66
    protected function getCellStyleXfsSectionContent()
131
    {
132
        return <<<EOD
133
<cellStyleXfs count="1">
134
    <xf borderId="0" fillId="0" fontId="0" numFmtId="0"/>
135
</cellStyleXfs>
136 66
EOD;
137
    }
138
139
    /**
140
     * Returns the content of the "<cellXfs>" section.
141
     *
142
     * @return string
143
     */
144 66
    protected function getCellXfsSectionContent()
145
    {
146 66
        $registeredStyles = $this->getRegisteredStyles();
147
148 66
        $content = '<cellXfs count="' . count($registeredStyles) . '">';
149
150 66
        foreach ($registeredStyles as $style) {
151 66
            $content .= '<xf numFmtId="0" fontId="' . $style->getId() . '" fillId="0" borderId="' . $style->getId() . '" xfId="0"';
152
153 66
            if ($style->shouldApplyFont()) {
154 66
                $content .= ' applyFont="1"';
155 66
            }
156
157 66
            if ($style->shouldApplyBorder()) {
158
                $content .= ' applyBorder="1"';
159
            }
160
161 66
            if ($style->shouldWrapText()) {
162 6
                $content .= ' applyAlignment="1">';
163 6
                $content .= '<alignment wrapText="1"/>';
164 6
                $content .= '</xf>';
165 6
            } else {
166 66
                $content .= '/>';
167
            }
168 66
        }
169
170 66
        $content .= '</cellXfs>';
171
172 66
        return $content;
173
    }
174
175
    /**
176
     * Returns the content of the "<cellStyles>" section.
177
     *
178
     * @return string
179
     */
180 66
    protected function getCellStylesSectionContent()
181
    {
182
        return <<<EOD
183
<cellStyles count="1">
184
    <cellStyle builtinId="0" name="Normal" xfId="0"/>
185
</cellStyles>
186 66
EOD;
187
    }
188
}
189