1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Box\Spout\Writer\XLSX\Manager\Style; |
4
|
|
|
|
5
|
|
|
use Box\Spout\Common\Entity\Style\Color; |
6
|
|
|
use Box\Spout\Common\Entity\Style\Style; |
7
|
|
|
use Box\Spout\Writer\XLSX\Helper\BorderHelper; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class StyleManager |
11
|
|
|
* Manages styles to be applied to a cell |
12
|
|
|
*/ |
13
|
|
|
class StyleManager extends \Box\Spout\Writer\Common\Manager\Style\StyleManager |
14
|
|
|
{ |
15
|
|
|
/** @var StyleRegistry */ |
16
|
|
|
protected $styleRegistry; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* For empty cells, we can specify a style or not. If no style are specified, |
20
|
|
|
* then the software default will be applied. But sometimes, it may be useful |
21
|
|
|
* to override this default style, for instance if the cell should have a |
22
|
|
|
* background color different than the default one or some borders |
23
|
|
|
* (fonts property don't really matter here). |
24
|
|
|
* |
25
|
|
|
* @param int $styleId |
26
|
|
|
* @return bool Whether the cell should define a custom style |
27
|
|
|
*/ |
28
|
11 |
|
public function shouldApplyStyleOnEmptyCell($styleId) |
29
|
|
|
{ |
30
|
11 |
|
$associatedFillId = $this->styleRegistry->getFillIdForStyleId($styleId); |
31
|
11 |
|
$hasStyleCustomFill = ($associatedFillId !== null && $associatedFillId !== 0); |
32
|
|
|
|
33
|
11 |
|
$associatedBorderId = $this->styleRegistry->getBorderIdForStyleId($styleId); |
34
|
11 |
|
$hasStyleCustomBorders = ($associatedBorderId !== null && $associatedBorderId !== 0); |
35
|
|
|
|
36
|
11 |
|
$associatedFormatId = $this->styleRegistry->getFormatIdForStyleId($styleId); |
37
|
11 |
|
$hasStyleCustomFormats = ($associatedFormatId !== null && $associatedFormatId !== 0); |
38
|
|
|
|
39
|
11 |
|
return ($hasStyleCustomFill || $hasStyleCustomBorders || $hasStyleCustomFormats); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Returns the content of the "styles.xml" file, given a list of styles. |
44
|
|
|
* |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
36 |
|
public function getStylesXMLFileContent() |
48
|
|
|
{ |
49
|
|
|
$content = <<<'EOD' |
50
|
36 |
|
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
51
|
|
|
<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"> |
52
|
|
|
EOD; |
53
|
|
|
|
54
|
36 |
|
$content .= $this->getFormatsSectionContent(); |
55
|
36 |
|
$content .= $this->getFontsSectionContent(); |
56
|
36 |
|
$content .= $this->getFillsSectionContent(); |
57
|
36 |
|
$content .= $this->getBordersSectionContent(); |
58
|
36 |
|
$content .= $this->getCellStyleXfsSectionContent(); |
59
|
36 |
|
$content .= $this->getCellXfsSectionContent(); |
60
|
36 |
|
$content .= $this->getCellStylesSectionContent(); |
61
|
|
|
|
62
|
|
|
$content .= <<<'EOD' |
63
|
36 |
|
</styleSheet> |
64
|
|
|
EOD; |
65
|
|
|
|
66
|
36 |
|
return $content; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Returns the content of the "<numFmts>" section. |
71
|
|
|
* |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
36 |
|
protected function getFormatsSectionContent() |
75
|
|
|
{ |
76
|
36 |
|
$tags = []; |
77
|
36 |
|
$registeredFormats = $this->styleRegistry->getRegisteredFormats(); |
78
|
36 |
|
foreach ($registeredFormats as $styleId) { |
79
|
1 |
|
$numFmtId = $this->styleRegistry->getFormatIdForStyleId($styleId); |
80
|
|
|
|
81
|
|
|
//Built-in formats do not need to be declared, skip them |
82
|
1 |
|
if ($numFmtId < 164) { |
83
|
1 |
|
continue; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** @var Style $style */ |
87
|
1 |
|
$style = $this->styleRegistry->getStyleFromStyleId($styleId); |
88
|
1 |
|
$format = $style->getFormat(); |
89
|
1 |
|
$tags[] = '<numFmt numFmtId="' . $numFmtId . '" formatCode="' . $format . '"/>'; |
90
|
|
|
} |
91
|
36 |
|
$content = '<numFmts count="' . count($tags) . '">'; |
92
|
36 |
|
$content .= implode('', $tags); |
93
|
36 |
|
$content .= '</numFmts>'; |
94
|
|
|
|
95
|
36 |
|
return $content; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Returns the content of the "<fonts>" section. |
100
|
|
|
* |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
36 |
|
protected function getFontsSectionContent() |
104
|
|
|
{ |
105
|
36 |
|
$registeredStyles = $this->styleRegistry->getRegisteredStyles(); |
106
|
|
|
|
107
|
36 |
|
$content = '<fonts count="' . count($registeredStyles) . '">'; |
108
|
|
|
|
109
|
|
|
/** @var Style $style */ |
110
|
36 |
|
foreach ($registeredStyles as $style) { |
111
|
36 |
|
$content .= '<font>'; |
112
|
|
|
|
113
|
36 |
|
$content .= '<sz val="' . $style->getFontSize() . '"/>'; |
114
|
36 |
|
$content .= '<color rgb="' . Color::toARGB($style->getFontColor()) . '"/>'; |
115
|
36 |
|
$content .= '<name val="' . $style->getFontName() . '"/>'; |
116
|
|
|
|
117
|
36 |
|
if ($style->isFontBold()) { |
118
|
8 |
|
$content .= '<b/>'; |
119
|
|
|
} |
120
|
36 |
|
if ($style->isFontItalic()) { |
121
|
1 |
|
$content .= '<i/>'; |
122
|
|
|
} |
123
|
36 |
|
if ($style->isFontUnderline()) { |
124
|
2 |
|
$content .= '<u/>'; |
125
|
|
|
} |
126
|
36 |
|
if ($style->isFontStrikethrough()) { |
127
|
1 |
|
$content .= '<strike/>'; |
128
|
|
|
} |
129
|
|
|
|
130
|
36 |
|
$content .= '</font>'; |
131
|
|
|
} |
132
|
|
|
|
133
|
36 |
|
$content .= '</fonts>'; |
134
|
|
|
|
135
|
36 |
|
return $content; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Returns the content of the "<fills>" section. |
140
|
|
|
* |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
36 |
|
protected function getFillsSectionContent() |
144
|
|
|
{ |
145
|
36 |
|
$registeredFills = $this->styleRegistry->getRegisteredFills(); |
146
|
|
|
|
147
|
|
|
// Excel reserves two default fills |
148
|
36 |
|
$fillsCount = count($registeredFills) + 2; |
149
|
36 |
|
$content = sprintf('<fills count="%d">', $fillsCount); |
150
|
|
|
|
151
|
36 |
|
$content .= '<fill><patternFill patternType="none"/></fill>'; |
152
|
36 |
|
$content .= '<fill><patternFill patternType="gray125"/></fill>'; |
153
|
|
|
|
154
|
|
|
// The other fills are actually registered by setting a background color |
155
|
36 |
|
foreach ($registeredFills as $styleId) { |
156
|
|
|
/** @var Style $style */ |
157
|
3 |
|
$style = $this->styleRegistry->getStyleFromStyleId($styleId); |
158
|
|
|
|
159
|
3 |
|
$backgroundColor = $style->getBackgroundColor(); |
160
|
3 |
|
$content .= sprintf( |
161
|
3 |
|
'<fill><patternFill patternType="solid"><fgColor rgb="%s"/></patternFill></fill>', |
162
|
3 |
|
$backgroundColor |
163
|
|
|
); |
164
|
|
|
} |
165
|
|
|
|
166
|
36 |
|
$content .= '</fills>'; |
167
|
|
|
|
168
|
36 |
|
return $content; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Returns the content of the "<borders>" section. |
173
|
|
|
* |
174
|
|
|
* @return string |
175
|
|
|
*/ |
176
|
36 |
|
protected function getBordersSectionContent() |
177
|
|
|
{ |
178
|
36 |
|
$registeredBorders = $this->styleRegistry->getRegisteredBorders(); |
179
|
|
|
|
180
|
|
|
// There is one default border with index 0 |
181
|
36 |
|
$borderCount = count($registeredBorders) + 1; |
182
|
|
|
|
183
|
36 |
|
$content = '<borders count="' . $borderCount . '">'; |
184
|
|
|
|
185
|
|
|
// Default border starting at index 0 |
186
|
36 |
|
$content .= '<border><left/><right/><top/><bottom/></border>'; |
187
|
|
|
|
188
|
36 |
|
foreach ($registeredBorders as $styleId) { |
189
|
|
|
/** @var \Box\Spout\Common\Entity\Style\Style $style */ |
190
|
4 |
|
$style = $this->styleRegistry->getStyleFromStyleId($styleId); |
191
|
4 |
|
$border = $style->getBorder(); |
192
|
4 |
|
$content .= '<border>'; |
193
|
|
|
|
194
|
|
|
// @link https://github.com/box/spout/issues/271 |
195
|
4 |
|
$sortOrder = ['left', 'right', 'top', 'bottom']; |
196
|
|
|
|
197
|
4 |
|
foreach ($sortOrder as $partName) { |
198
|
4 |
|
if ($border->hasPart($partName)) { |
199
|
|
|
/** @var $part \Box\Spout\Common\Entity\Style\BorderPart */ |
200
|
4 |
|
$part = $border->getPart($partName); |
201
|
4 |
|
$content .= BorderHelper::serializeBorderPart($part); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
205
|
4 |
|
$content .= '</border>'; |
206
|
|
|
} |
207
|
|
|
|
208
|
36 |
|
$content .= '</borders>'; |
209
|
|
|
|
210
|
36 |
|
return $content; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Returns the content of the "<cellStyleXfs>" section. |
215
|
|
|
* |
216
|
|
|
* @return string |
217
|
|
|
*/ |
218
|
36 |
|
protected function getCellStyleXfsSectionContent() |
219
|
|
|
{ |
220
|
|
|
return <<<'EOD' |
221
|
36 |
|
<cellStyleXfs count="1"> |
222
|
|
|
<xf borderId="0" fillId="0" fontId="0" numFmtId="0"/> |
223
|
|
|
</cellStyleXfs> |
224
|
|
|
EOD; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Returns the content of the "<cellXfs>" section. |
229
|
|
|
* |
230
|
|
|
* @return string |
231
|
|
|
*/ |
232
|
36 |
|
protected function getCellXfsSectionContent() |
233
|
|
|
{ |
234
|
36 |
|
$registeredStyles = $this->styleRegistry->getRegisteredStyles(); |
235
|
|
|
|
236
|
36 |
|
$content = '<cellXfs count="' . count($registeredStyles) . '">'; |
237
|
|
|
|
238
|
36 |
|
foreach ($registeredStyles as $style) { |
239
|
36 |
|
$styleId = $style->getId(); |
240
|
36 |
|
$fillId = $this->getFillIdForStyleId($styleId); |
241
|
36 |
|
$borderId = $this->getBorderIdForStyleId($styleId); |
242
|
36 |
|
$numFmtId = $this->getFormatIdForStyleId($styleId); |
243
|
|
|
|
244
|
36 |
|
$content .= '<xf numFmtId="' . $numFmtId . '" fontId="' . $styleId . '" fillId="' . $fillId . '" borderId="' . $borderId . '" xfId="0"'; |
245
|
|
|
|
246
|
36 |
|
if ($style->shouldApplyFont()) { |
247
|
36 |
|
$content .= ' applyFont="1"'; |
248
|
|
|
} |
249
|
|
|
|
250
|
36 |
|
$content .= sprintf(' applyBorder="%d"', $style->shouldApplyBorder() ? 1 : 0); |
251
|
|
|
|
252
|
36 |
|
if ($style->shouldWrapText()) { |
253
|
2 |
|
$content .= ' applyAlignment="1">'; |
254
|
2 |
|
$content .= '<alignment wrapText="1"/>'; |
255
|
2 |
|
$content .= '</xf>'; |
256
|
|
|
} else { |
257
|
36 |
|
$content .= '/>'; |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
|
261
|
36 |
|
$content .= '</cellXfs>'; |
262
|
|
|
|
263
|
36 |
|
return $content; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Returns the fill ID associated to the given style ID. |
268
|
|
|
* For the default style, we don't a fill. |
269
|
|
|
* |
270
|
|
|
* @param int $styleId |
271
|
|
|
* @return int |
272
|
|
|
*/ |
273
|
36 |
|
private function getFillIdForStyleId($styleId) |
274
|
|
|
{ |
275
|
|
|
// For the default style (ID = 0), we don't want to override the fill. |
276
|
|
|
// Otherwise all cells of the spreadsheet will have a background color. |
277
|
36 |
|
$isDefaultStyle = ($styleId === 0); |
278
|
|
|
|
279
|
36 |
|
return $isDefaultStyle ? 0 : ($this->styleRegistry->getFillIdForStyleId($styleId) ?: 0); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Returns the fill ID associated to the given style ID. |
284
|
|
|
* For the default style, we don't a border. |
285
|
|
|
* |
286
|
|
|
* @param int $styleId |
287
|
|
|
* @return int |
288
|
|
|
*/ |
289
|
36 |
|
private function getBorderIdForStyleId($styleId) |
290
|
|
|
{ |
291
|
|
|
// For the default style (ID = 0), we don't want to override the border. |
292
|
|
|
// Otherwise all cells of the spreadsheet will have a border. |
293
|
36 |
|
$isDefaultStyle = ($styleId === 0); |
294
|
|
|
|
295
|
36 |
|
return $isDefaultStyle ? 0 : ($this->styleRegistry->getBorderIdForStyleId($styleId) ?: 0); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Returns the format ID associated to the given style ID. |
300
|
|
|
* For the default style use general format. |
301
|
|
|
* |
302
|
|
|
* @param int $styleId |
303
|
|
|
* @return int |
304
|
|
|
*/ |
305
|
36 |
|
private function getFormatIdForStyleId($styleId) |
306
|
|
|
{ |
307
|
|
|
// For the default style (ID = 0), we don't want to override the format. |
308
|
|
|
// Otherwise all cells of the spreadsheet will have a format. |
309
|
36 |
|
$isDefaultStyle = ($styleId === 0); |
310
|
|
|
|
311
|
36 |
|
return $isDefaultStyle ? 0 : ($this->styleRegistry->getFormatIdForStyleId($styleId) ?: 0); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Returns the content of the "<cellStyles>" section. |
316
|
|
|
* |
317
|
|
|
* @return string |
318
|
|
|
*/ |
319
|
36 |
|
protected function getCellStylesSectionContent() |
320
|
|
|
{ |
321
|
|
|
return <<<'EOD' |
322
|
36 |
|
<cellStyles count="1"> |
323
|
|
|
<cellStyle builtinId="0" name="Normal" xfId="0"/> |
324
|
|
|
</cellStyles> |
325
|
|
|
EOD; |
326
|
|
|
} |
327
|
|
|
} |
328
|
|
|
|