styleRegistry->getFillIdForStyleId($styleId); $hasStyleCustomFill = ($associatedFillId !== null && $associatedFillId !== 0); $associatedBorderId = $this->styleRegistry->getBorderIdForStyleId($styleId); $hasStyleCustomBorders = ($associatedBorderId !== null && $associatedBorderId !== 0); $associatedFormatId = $this->styleRegistry->getFormatIdForStyleId($styleId); $hasStyleCustomFormats = ($associatedFormatId !== null && $associatedFormatId !== 0); return ($hasStyleCustomFill || $hasStyleCustomBorders || $hasStyleCustomFormats); } /** * Returns the content of the "styles.xml" file, given a list of styles. * * @return string */ public function getStylesXMLFileContent() { $content = <<<'EOD' EOD; $content .= $this->getFormatsSectionContent(); $content .= $this->getFontsSectionContent(); $content .= $this->getFillsSectionContent(); $content .= $this->getBordersSectionContent(); $content .= $this->getCellStyleXfsSectionContent(); $content .= $this->getCellXfsSectionContent(); $content .= $this->getCellStylesSectionContent(); $content .= <<<'EOD' EOD; return $content; } /** * Returns the content of the "" section. * * @return string */ protected function getFormatsSectionContent() { $tags = []; $registeredFormats = $this->styleRegistry->getRegisteredFormats(); foreach ($registeredFormats as $styleId) { $numFmtId = $this->styleRegistry->getFormatIdForStyleId($styleId); //Built-in formats do not need to be declared, skip them if ($numFmtId < 164) { continue; } /** @var Style $style */ $style = $this->styleRegistry->getStyleFromStyleId($styleId); $format = $style->getFormat(); $tags[] = ''; } $content = ''; $content .= implode('', $tags); $content .= ''; return $content; } /** * Returns the content of the "" section. * * @return string */ protected function getFontsSectionContent() { $registeredStyles = $this->styleRegistry->getRegisteredStyles(); $content = ''; /** @var Style $style */ foreach ($registeredStyles as $style) { $content .= ''; $content .= ''; $content .= ''; $content .= ''; if ($style->isFontBold()) { $content .= ''; } if ($style->isFontItalic()) { $content .= ''; } if ($style->isFontUnderline()) { $content .= ''; } if ($style->isFontStrikethrough()) { $content .= ''; } $content .= ''; } $content .= ''; return $content; } /** * Returns the content of the "" section. * * @return string */ protected function getFillsSectionContent() { $registeredFills = $this->styleRegistry->getRegisteredFills(); // Excel reserves two default fills $fillsCount = count($registeredFills) + 2; $content = sprintf('', $fillsCount); $content .= ''; $content .= ''; // The other fills are actually registered by setting a background color foreach ($registeredFills as $styleId) { /** @var Style $style */ $style = $this->styleRegistry->getStyleFromStyleId($styleId); $backgroundColor = $style->getBackgroundColor(); $content .= sprintf( '', $backgroundColor ); } $content .= ''; return $content; } /** * Returns the content of the "" section. * * @return string */ protected function getBordersSectionContent() { $registeredBorders = $this->styleRegistry->getRegisteredBorders(); // There is one default border with index 0 $borderCount = count($registeredBorders) + 1; $content = ''; // Default border starting at index 0 $content .= ''; foreach ($registeredBorders as $styleId) { /** @var \Box\Spout\Common\Entity\Style\Style $style */ $style = $this->styleRegistry->getStyleFromStyleId($styleId); $border = $style->getBorder(); $content .= ''; // @link https://github.com/box/spout/issues/271 $sortOrder = ['left', 'right', 'top', 'bottom']; foreach ($sortOrder as $partName) { if ($border->hasPart($partName)) { /** @var $part \Box\Spout\Common\Entity\Style\BorderPart */ $part = $border->getPart($partName); $content .= BorderHelper::serializeBorderPart($part); } } $content .= ''; } $content .= ''; return $content; } /** * Returns the content of the "" section. * * @return string */ protected function getCellStyleXfsSectionContent() { return <<<'EOD' EOD; } /** * Returns the content of the "" section. * * @return string */ protected function getCellXfsSectionContent() { $registeredStyles = $this->styleRegistry->getRegisteredStyles(); $content = ''; foreach ($registeredStyles as $style) { $styleId = $style->getId(); $fillId = $this->getFillIdForStyleId($styleId); $borderId = $this->getBorderIdForStyleId($styleId); $numFmtId = $this->getFormatIdForStyleId($styleId); $content .= 'shouldApplyFont()) { $content .= ' applyFont="1"'; } $content .= sprintf(' applyBorder="%d"', $style->shouldApplyBorder() ? 1 : 0); if ($style->shouldWrapText()) { $content .= ' applyAlignment="1">'; $content .= ''; $content .= ''; } else { $content .= '/>'; } } $content .= ''; return $content; } /** * Returns the fill ID associated to the given style ID. * For the default style, we don't a fill. * * @param int $styleId * @return int */ private function getFillIdForStyleId($styleId) { // For the default style (ID = 0), we don't want to override the fill. // Otherwise all cells of the spreadsheet will have a background color. $isDefaultStyle = ($styleId === 0); return $isDefaultStyle ? 0 : ($this->styleRegistry->getFillIdForStyleId($styleId) ?: 0); } /** * Returns the fill ID associated to the given style ID. * For the default style, we don't a border. * * @param int $styleId * @return int */ private function getBorderIdForStyleId($styleId) { // For the default style (ID = 0), we don't want to override the border. // Otherwise all cells of the spreadsheet will have a border. $isDefaultStyle = ($styleId === 0); return $isDefaultStyle ? 0 : ($this->styleRegistry->getBorderIdForStyleId($styleId) ?: 0); } /** * Returns the format ID associated to the given style ID. * For the default style use general format. * * @param int $styleId * @return int */ private function getFormatIdForStyleId($styleId) { // For the default style (ID = 0), we don't want to override the format. // Otherwise all cells of the spreadsheet will have a format. $isDefaultStyle = ($styleId === 0); return $isDefaultStyle ? 0 : ($this->styleRegistry->getFormatIdForStyleId($styleId) ?: 0); } /** * Returns the content of the "" section. * * @return string */ protected function getCellStylesSectionContent() { return <<<'EOD' EOD; } }