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