|
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
|
|
|
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
|
|
|
EOD; |
|
27
|
|
|
|
|
28
|
|
|
$content .= $this->getFontsSectionContent(); |
|
29
|
|
|
$content .= $this->getFillsSectionContent(); |
|
30
|
|
|
$content .= $this->getBordersSectionContent(); |
|
31
|
|
|
$content .= $this->getCellStyleXfsSectionContent(); |
|
32
|
|
|
$content .= $this->getCellXfsSectionContent(); |
|
33
|
|
|
$content .= $this->getCellStylesSectionContent(); |
|
34
|
|
|
|
|
35
|
|
|
$content .= <<<EOD |
|
36
|
|
|
</styleSheet> |
|
37
|
|
|
EOD; |
|
38
|
|
|
|
|
39
|
|
|
return $content; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Returns the content of the "<fonts>" section. |
|
44
|
|
|
* |
|
45
|
|
|
* @return string |
|
46
|
|
|
*/ |
|
47
|
|
|
protected function getFontsSectionContent() |
|
48
|
|
|
{ |
|
49
|
|
|
$content = '<fonts count="' . count($this->styleIdToStyleMappingTable) . '">'; |
|
50
|
|
|
|
|
51
|
|
|
/** @var \Box\Spout\Writer\Style\Style $style */ |
|
52
|
|
|
foreach ($this->getRegisteredStyles() as $style) { |
|
53
|
|
|
$content .= '<font>'; |
|
54
|
|
|
|
|
55
|
|
|
$content .= '<sz val="' . $style->getFontSize() . '"/>'; |
|
56
|
|
|
$content .= '<color rgb="' . Color::toARGB($style->getFontColor()) . '"/>'; |
|
57
|
|
|
$content .= '<name val="' . $style->getFontName() . '"/>'; |
|
58
|
|
|
|
|
59
|
|
|
if ($style->isFontBold()) { |
|
60
|
|
|
$content .= '<b/>'; |
|
61
|
|
|
} |
|
62
|
|
|
if ($style->isFontItalic()) { |
|
63
|
|
|
$content .= '<i/>'; |
|
64
|
|
|
} |
|
65
|
|
|
if ($style->isFontUnderline()) { |
|
66
|
|
|
$content .= '<u/>'; |
|
67
|
|
|
} |
|
68
|
|
|
if ($style->isFontStrikethrough()) { |
|
69
|
|
|
$content .= '<strike/>'; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$content .= '</font>'; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$content .= '</fonts>'; |
|
76
|
|
|
|
|
77
|
|
|
return $content; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Returns the content of the "<fills>" section. |
|
82
|
|
|
* |
|
83
|
|
|
* @return string |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function getFillsSectionContent() |
|
86
|
|
|
{ |
|
87
|
|
|
return <<<EOD |
|
88
|
|
|
<fills count="1"> |
|
89
|
|
|
<fill> |
|
90
|
|
|
<patternFill patternType="none"/> |
|
91
|
|
|
</fill> |
|
92
|
|
|
</fills> |
|
93
|
|
|
EOD; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Returns the content of the "<borders>" section. |
|
98
|
|
|
* |
|
99
|
|
|
* @return string |
|
100
|
|
|
*/ |
|
101
|
|
|
protected function getBordersSectionContent() |
|
102
|
|
|
{ |
|
103
|
|
|
$registeredStyles = $this->getRegisteredStyles(); |
|
104
|
|
|
$registeredStylesCount = count($registeredStyles); |
|
105
|
|
|
|
|
106
|
|
|
$content = '<borders count="' . $registeredStylesCount . '">'; |
|
107
|
|
|
|
|
108
|
|
|
/** @var \Box\Spout\Writer\Style\Style $style */ |
|
109
|
|
|
foreach ($registeredStyles as $style) { |
|
110
|
|
|
$border = $style->getBorder(); |
|
111
|
|
|
if ($border) { |
|
112
|
|
|
$content .= '<border>'; |
|
113
|
|
|
foreach ($border->getParts() as $part) { |
|
114
|
|
|
/** @var $part \Box\Spout\Writer\Style\BorderPart */ |
|
115
|
|
|
$content .= BorderHelper::serializeBorderPart($part); |
|
116
|
|
|
} |
|
117
|
|
|
$content .= '</border>'; |
|
118
|
|
|
} else { |
|
119
|
|
|
$content .= '<border><left/><right/><top/><bottom/><diagonal/></border>'; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$content .= '</borders>'; |
|
124
|
|
|
|
|
125
|
|
|
return $content; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Returns the content of the "<cellStyleXfs>" section. |
|
130
|
|
|
* |
|
131
|
|
|
* @return string |
|
132
|
|
|
*/ |
|
133
|
|
|
protected function getCellStyleXfsSectionContent() |
|
134
|
|
|
{ |
|
135
|
|
|
return <<<EOD |
|
136
|
|
|
<cellStyleXfs count="1"> |
|
137
|
|
|
<xf borderId="0" fillId="0" fontId="0" numFmtId="0"/> |
|
138
|
|
|
</cellStyleXfs> |
|
139
|
|
|
EOD; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Returns the content of the "<cellXfs>" section. |
|
144
|
|
|
* |
|
145
|
|
|
* @return string |
|
146
|
|
|
*/ |
|
147
|
|
|
protected function getCellXfsSectionContent() |
|
148
|
|
|
{ |
|
149
|
|
|
$registeredStyles = $this->getRegisteredStyles(); |
|
150
|
|
|
|
|
151
|
|
|
$content = '<cellXfs count="' . count($registeredStyles) . '">'; |
|
152
|
|
|
|
|
153
|
|
|
foreach ($registeredStyles as $style) { |
|
154
|
|
|
$content .= '<xf numFmtId="0" fontId="' . $style->getId() . '" fillId="0" borderId="' . $style->getId() . '" xfId="0"'; |
|
155
|
|
|
|
|
156
|
|
|
if ($style->shouldApplyFont()) { |
|
157
|
|
|
$content .= ' applyFont="1"'; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
if ($style->shouldApplyBorder()) { |
|
161
|
|
|
$content .= ' applyBorder="1"'; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
if ($style->shouldWrapText()) { |
|
165
|
|
|
$content .= ' applyAlignment="1">'; |
|
166
|
|
|
$content .= '<alignment wrapText="1"/>'; |
|
167
|
|
|
$content .= '</xf>'; |
|
168
|
|
|
} else { |
|
169
|
|
|
$content .= '/>'; |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
$content .= '</cellXfs>'; |
|
174
|
|
|
|
|
175
|
|
|
return $content; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Returns the content of the "<cellStyles>" section. |
|
180
|
|
|
* |
|
181
|
|
|
* @return string |
|
182
|
|
|
*/ |
|
183
|
|
|
protected function getCellStylesSectionContent() |
|
184
|
|
|
{ |
|
185
|
|
|
return <<<EOD |
|
186
|
|
|
<cellStyles count="1"> |
|
187
|
|
|
<cellStyle builtinId="0" name="Normal" xfId="0"/> |
|
188
|
|
|
</cellStyles> |
|
189
|
|
|
EOD; |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|