1 | <?php |
||
15 | class StyleManager extends \Box\Spout\Writer\Common\Manager\Style\StyleManager |
||
16 | { |
||
17 | /** @var StyleRegistry */ |
||
18 | protected $styleRegistry; |
||
19 | |||
20 | /** |
||
21 | * For empty cells, we can specify a style or not. If no style are specified, |
||
22 | * then the software default will be applied. But sometimes, it may be useful |
||
23 | * to override this default style, for instance if the cell should have a |
||
24 | * background color different than the default one or some borders |
||
25 | * (fonts property don't really matter here). |
||
26 | * |
||
27 | * @param int $styleId |
||
28 | * @return bool Whether the cell should define a custom style |
||
29 | */ |
||
30 | 9 | public function shouldApplyStyleOnEmptyCell($styleId) |
|
40 | |||
41 | |||
42 | /** |
||
43 | * Returns the content of the "styles.xml" file, given a list of styles. |
||
44 | * |
||
45 | * @return string |
||
46 | */ |
||
47 | 4 | public function getStylesXMLFileContent() |
|
67 | |||
68 | /** |
||
69 | * Returns the content of the "<fonts>" section. |
||
70 | * |
||
71 | * @return string |
||
72 | */ |
||
73 | 4 | protected function getFontsSectionContent() |
|
74 | { |
||
75 | 4 | $registeredStyles = $this->styleRegistry->getRegisteredStyles(); |
|
76 | |||
77 | 4 | $content = '<fonts count="' . count($registeredStyles) . '">'; |
|
78 | |||
79 | /** @var Style $style */ |
||
80 | 4 | foreach ($registeredStyles as $style) { |
|
81 | 4 | $content .= '<font>'; |
|
82 | |||
83 | 4 | $content .= '<sz val="' . $style->getFontSize() . '"/>'; |
|
84 | 4 | $content .= '<color rgb="' . Color::toARGB($style->getFontColor()) . '"/>'; |
|
85 | 4 | $content .= '<name val="' . $style->getFontName() . '"/>'; |
|
86 | |||
87 | 4 | if ($style->isFontBold()) { |
|
88 | $content .= '<b/>'; |
||
89 | } |
||
90 | 4 | if ($style->isFontItalic()) { |
|
91 | $content .= '<i/>'; |
||
92 | } |
||
93 | 4 | if ($style->isFontUnderline()) { |
|
94 | $content .= '<u/>'; |
||
95 | } |
||
96 | 4 | if ($style->isFontStrikethrough()) { |
|
97 | $content .= '<strike/>'; |
||
98 | } |
||
99 | |||
100 | 4 | $content .= '</font>'; |
|
101 | } |
||
102 | |||
103 | 4 | $content .= '</fonts>'; |
|
104 | |||
105 | 4 | return $content; |
|
106 | } |
||
107 | |||
108 | /** |
||
109 | * Returns the content of the "<fills>" section. |
||
110 | * |
||
111 | * @return string |
||
112 | */ |
||
113 | 4 | protected function getFillsSectionContent() |
|
114 | { |
||
115 | 4 | $registeredFills = $this->styleRegistry->getRegisteredFills(); |
|
116 | |||
117 | // Excel reserves two default fills |
||
118 | 4 | $fillsCount = count($registeredFills) + 2; |
|
119 | 4 | $content = sprintf('<fills count="%d">', $fillsCount); |
|
120 | |||
121 | 4 | $content .= '<fill><patternFill patternType="none"/></fill>'; |
|
122 | 4 | $content .= '<fill><patternFill patternType="gray125"/></fill>'; |
|
123 | |||
124 | // The other fills are actually registered by setting a background color |
||
125 | 4 | foreach ($registeredFills as $styleId) { |
|
126 | /** @var Style $style */ |
||
127 | $style = $this->styleRegistry->getStyleFromStyleId($styleId); |
||
128 | |||
129 | $backgroundColor = $style->getBackgroundColor(); |
||
130 | $content .= sprintf( |
||
131 | '<fill><patternFill patternType="solid"><fgColor rgb="%s"/></patternFill></fill>', |
||
132 | $backgroundColor |
||
133 | ); |
||
134 | } |
||
135 | |||
136 | 4 | $content .= '</fills>'; |
|
137 | |||
138 | 4 | return $content; |
|
139 | } |
||
140 | |||
141 | /** |
||
142 | * Returns the content of the "<borders>" section. |
||
143 | * |
||
144 | * @return string |
||
145 | */ |
||
146 | 4 | protected function getBordersSectionContent() |
|
147 | { |
||
148 | 4 | $registeredBorders = $this->styleRegistry->getRegisteredBorders(); |
|
149 | |||
150 | // There is one default border with index 0 |
||
151 | 4 | $borderCount = count($registeredBorders) + 1; |
|
152 | |||
153 | 4 | $content = '<borders count="' . $borderCount . '">'; |
|
154 | |||
155 | // Default border starting at index 0 |
||
156 | 4 | $content .= '<border><left/><right/><top/><bottom/></border>'; |
|
157 | |||
158 | 4 | foreach ($registeredBorders as $styleId) { |
|
159 | /** @var \Box\Spout\Writer\Common\Entity\Style\Style $style */ |
||
160 | $style = $this->styleRegistry->getStyleFromStyleId($styleId); |
||
161 | $border = $style->getBorder(); |
||
162 | $content .= '<border>'; |
||
163 | |||
164 | // @link https://github.com/box/spout/issues/271 |
||
165 | $sortOrder = ['left', 'right', 'top', 'bottom']; |
||
166 | |||
167 | foreach ($sortOrder as $partName) { |
||
168 | if ($border->hasPart($partName)) { |
||
169 | /** @var $part \Box\Spout\Writer\Common\Entity\Style\BorderPart */ |
||
170 | $part = $border->getPart($partName); |
||
171 | $content .= BorderHelper::serializeBorderPart($part); |
||
172 | } |
||
173 | } |
||
174 | |||
175 | $content .= '</border>'; |
||
176 | } |
||
177 | |||
178 | 4 | $content .= '</borders>'; |
|
179 | |||
180 | 4 | return $content; |
|
181 | } |
||
182 | |||
183 | /** |
||
184 | * Returns the content of the "<cellStyleXfs>" section. |
||
185 | * |
||
186 | * @return string |
||
187 | */ |
||
188 | 4 | protected function getCellStyleXfsSectionContent() |
|
196 | |||
197 | /** |
||
198 | * Returns the content of the "<cellXfs>" section. |
||
199 | * |
||
200 | * @return string |
||
201 | */ |
||
202 | 4 | protected function getCellXfsSectionContent() |
|
203 | { |
||
204 | 4 | $registeredStyles = $this->styleRegistry->getRegisteredStyles(); |
|
205 | |||
206 | 4 | $content = '<cellXfs count="' . count($registeredStyles) . '">'; |
|
207 | |||
208 | 4 | foreach ($registeredStyles as $style) { |
|
209 | 4 | $styleId = $style->getId(); |
|
210 | 4 | $fillId = $this->styleRegistry->getFillIdForStyleId($styleId); |
|
211 | 4 | $borderId = $this->styleRegistry->getBorderIdForStyleId($styleId); |
|
212 | |||
213 | 4 | $content .= '<xf numFmtId="0" fontId="' . $styleId . '" fillId="' . $fillId . '" borderId="' . $borderId . '" xfId="0"'; |
|
214 | |||
215 | 4 | if ($style->shouldApplyFont()) { |
|
216 | 4 | $content .= ' applyFont="1"'; |
|
217 | } |
||
218 | |||
219 | 4 | $content .= sprintf(' applyBorder="%d"', $style->shouldApplyBorder() ? 1 : 0); |
|
220 | |||
221 | 4 | if ($style->shouldWrapText()) { |
|
222 | $content .= ' applyAlignment="1">'; |
||
223 | $content .= '<alignment wrapText="1"/>'; |
||
224 | $content .= '</xf>'; |
||
225 | } else { |
||
226 | 4 | $content .= '/>'; |
|
227 | } |
||
228 | } |
||
229 | |||
230 | 4 | $content .= '</cellXfs>'; |
|
231 | |||
232 | 4 | return $content; |
|
233 | } |
||
234 | |||
235 | /** |
||
236 | * Returns the content of the "<cellStyles>" section. |
||
237 | * |
||
238 | * @return string |
||
239 | */ |
||
240 | 4 | protected function getCellStylesSectionContent() |
|
248 | } |
||
249 |