Completed
Pull Request — master (#209)
by
unknown
11:22
created

StyleHelper::getStylesXMLFileContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 21
ccs 10
cts 10
cp 1
rs 9.3142
cc 1
eloc 15
nc 1
nop 0
crap 1
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 63
    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 63
EOD;
27
28 63
        $content .= $this->getNumberFormatSectionContent();
29 63
        $content .= $this->getFontsSectionContent();
30 63
        $content .= $this->getFillsSectionContent();
31 63
        $content .= $this->getBordersSectionContent();
32 63
        $content .= $this->getCellStyleXfsSectionContent();
33 63
        $content .= $this->getCellXfsSectionContent();
34
        $content .= $this->getCellStylesSectionContent();
35
36
        $content .= <<<EOD
37 63
</styleSheet>
38
EOD;
39 63
40
        return $content;
41
    }
42
43
    protected function getNumberFormatSectionContent() {
44
        $formats = array();
45
        $numberFormatCount = 0;
46
        // This is the limit excel holds for the default number formats
47 63
        $baseNumberFormatId = 163;
48
49 63
        foreach($this->getRegisteredStyles() as $style) {
50
            /* If this evals to false we should skip it since it isnt used */
51
            if ($style->shouldApplyNumberFormat()) {
52 63
                $numberFormatCount++;
53 63
                $style->setNumberFormatId($baseNumberFormatId + $numberFormatCount);
54
                $formats[] = '<numFmt numFmtId="'.$style->getNumberFormatId().'" formatCode="'.$style->getNumberFormat().'"/>';
55 63
            }
56 63
        }
57 63
58
        if ($numberFormatCount == 0){
59 63
            return '';
60 9
        }
61 9
62 63
        $content = '<numFmts count="'.$numberFormatCount.'">';
63 3
        $content .= implode('', $formats);
64 3
        $content .= '</numFmts>';
65 63
        return $content;
66 3
    }
67 3
68 63
    /**
69 3
     * Returns the content of the "<fonts>" section.
70 3
     *
71
     * @return string
72 63
     */
73 63
    protected function getFontsSectionContent()
74
    {
75 63
        $content = '<fonts count="' . count($this->styleIdToStyleMappingTable) . '">';
76
77 63
        /** @var \Box\Spout\Writer\Style\Style $style */
78
        foreach ($this->getRegisteredStyles() as $style) {
79
            $content .= '<font>';
80
81
            $content .= '<sz val="' . $style->getFontSize() . '"/>';
82
            $content .= '<color rgb="' . Color::toARGB($style->getFontColor()) . '"/>';
83
            $content .= '<name val="' . $style->getFontName() . '"/>';
84
85 63
            if ($style->isFontBold()) {
86
                $content .= '<b/>';
87
            }
88
            if ($style->isFontItalic()) {
89
                $content .= '<i/>';
90
            }
91
            if ($style->isFontUnderline()) {
92
                $content .= '<u/>';
93 63
            }
94
            if ($style->isFontStrikethrough()) {
95
                $content .= '<strike/>';
96
            }
97
98
            $content .= '</font>';
99
        }
100
101 63
        $content .= '</fonts>';
102
103
        return $content;
104
    }
105
106
    /**
107
     * Returns the content of the "<fills>" section.
108
     *
109
     * @return string
110
     */
111
    protected function getFillsSectionContent()
112
    {
113 63
        return <<<EOD
114
<fills count="1">
115
    <fill>
116
        <patternFill patternType="none"/>
117
    </fill>
118
</fills>
119
EOD;
120
    }
121 63
122
    /**
123
     * Returns the content of the "<borders>" section.
124
     *
125
     * @return string
126
     */
127 63
    protected function getBordersSectionContent()
128
    {
129
        return <<<EOD
130
<borders count="1">
131
    <border>
132
        <left/>
133
        <right/>
134
        <top/>
135 63
        <bottom/>
136
        <diagonal/>
137 63
    </border>
138
</borders>
139 63
EOD;
140
    }
141 63
142 63
    /**
143
     * Returns the content of the "<cellStyleXfs>" section.
144 63
     *
145 63
     * @return string
146 63
     */
147
    protected function getCellStyleXfsSectionContent()
148 63
    {
149 6
        return <<<EOD
150 6
<cellStyleXfs count="1">
151 6
    <xf borderId="0" fillId="0" fontId="0" numFmtId="0"/>
152 6
</cellStyleXfs>
153 63
EOD;
154
    }
155 63
156
    /**
157 63
     * Returns the content of the "<cellXfs>" section.
158
     *
159 63
     * @return string
160
     */
161
    protected function getCellXfsSectionContent()
162
    {
163
        $registeredStyles = $this->getRegisteredStyles();
164
165
        $content = '<cellXfs count="' . count($registeredStyles) . '">';
166
167 63
        foreach ($registeredStyles as $style) {
168
            $content .= '<xf numFmtId="'.$style->getNumberFormatId().'" fontId="' . $style->getId() . '" fillId="0" borderId="0" xfId="0"';
169
170
            if ($style->shouldApplyNumberFormat()) {
171
                $content .= ' applyNumberFormat="1"';
172
            }
173 63
174
            if ($style->shouldApplyFont()) {
175
                $content .= ' applyFont="1"';
176
            }
177
178
            if ($style->shouldWrapText()) {
179
                $content .= ' applyAlignment="1">';
180
                $content .= '<alignment wrapText="1"/>';
181
                $content .= '</xf>';
182
            } else {
183
                $content .= '/>';
184
            }
185
        }
186
187
        $content .= '</cellXfs>';
188
189
        return $content;
190
    }
191
192
    /**
193
     * Returns the content of the "<cellStyles>" section.
194
     *
195
     * @return string
196
     */
197
    protected function getCellStylesSectionContent()
198
    {
199
        return <<<EOD
200
<cellStyles count="1">
201
    <cellStyle builtinId="0" name="Normal" xfId="0"/>
202
</cellStyles>
203
EOD;
204
    }
205
}
206