From e7f0d6165121685d88cd4fa7fe4abdb82c9984fa Mon Sep 17 00:00:00 2001 From: Adrien Loison Date: Wed, 19 Oct 2016 13:27:18 -0700 Subject: [PATCH] Refactor Style::mergeWith() and StyleHelper::getStyleSectionContent() --- src/Spout/Writer/ODS/Helper/StyleHelper.php | 181 ++++++++++++++------ src/Spout/Writer/Style/Style.php | 91 ++++++---- 2 files changed, 191 insertions(+), 81 deletions(-) diff --git a/src/Spout/Writer/ODS/Helper/StyleHelper.php b/src/Spout/Writer/ODS/Helper/StyleHelper.php index 9a0eeee..f5ad3bc 100644 --- a/src/Spout/Writer/ODS/Helper/StyleHelper.php +++ b/src/Spout/Writer/ODS/Helper/StyleHelper.php @@ -214,64 +214,143 @@ EOD; */ protected function getStyleSectionContent($style) { - $defaultStyle = $this->getDefaultStyle(); $styleIndex = $style->getId() + 1; // 1-based $content = ''; - if ($style->shouldApplyFont()) { - $content .= 'getFontColor(); - if ($fontColor !== $defaultStyle->getFontColor()) { - $content .= ' fo:color="#' . $fontColor . '"'; - } - - $fontName = $style->getFontName(); - if ($fontName !== $defaultStyle->getFontName()) { - $content .= ' style:font-name="' . $fontName . '" style:font-name-asian="' . $fontName . '" style:font-name-complex="' . $fontName . '"'; - } - - $fontSize = $style->getFontSize(); - if ($fontSize !== $defaultStyle->getFontSize()) { - $content .= ' fo:font-size="' . $fontSize . 'pt" style:font-size-asian="' . $fontSize . 'pt" style:font-size-complex="' . $fontSize . 'pt"'; - } - - if ($style->isFontBold()) { - $content .= ' fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"'; - } - if ($style->isFontItalic()) { - $content .= ' fo:font-style="italic" style:font-style-asian="italic" style:font-style-complex="italic"'; - } - if ($style->isFontUnderline()) { - $content .= ' style:text-underline-style="solid" style:text-underline-type="single"'; - } - if ($style->isFontStrikethrough()) { - $content .= ' style:text-line-through-style="solid"'; - } - - $content .= '/>'; - } - - if ($style->shouldWrapText()) { - $content .= ''; - } - - if ($style->shouldApplyBorder()) { - $borderProperty = ''; - $borders = array_map(function (BorderPart $borderPart) { - return BorderHelper::serializeBorderPart($borderPart); - }, $style->getBorder()->getParts()); - $content .= sprintf($borderProperty, implode(' ', $borders)); - } - - if ($style->shouldApplyBackgroundColor()) { - $content .= sprintf(' - ', $style->getBackgroundColor()); - } + $content .= $this->getTextPropertiesSectionContent($style); + $content .= $this->getTableCellPropertiesSectionContent($style); $content .= ''; return $content; } + + /** + * Returns the contents of the "" section, inside "" section + * + * @param \Box\Spout\Writer\Style\Style $style + * @return string + */ + private function getTextPropertiesSectionContent($style) + { + $content = ''; + + if ($style->shouldApplyFont()) { + $content .= $this->getFontSectionContent($style); + } + + return $content; + } + + /** + * Returns the contents of the "" section, inside "" section + * + * @param \Box\Spout\Writer\Style\Style $style + * @return string + */ + private function getFontSectionContent($style) + { + $defaultStyle = $this->getDefaultStyle(); + + $content = 'getFontColor(); + if ($fontColor !== $defaultStyle->getFontColor()) { + $content .= ' fo:color="#' . $fontColor . '"'; + } + + $fontName = $style->getFontName(); + if ($fontName !== $defaultStyle->getFontName()) { + $content .= ' style:font-name="' . $fontName . '" style:font-name-asian="' . $fontName . '" style:font-name-complex="' . $fontName . '"'; + } + + $fontSize = $style->getFontSize(); + if ($fontSize !== $defaultStyle->getFontSize()) { + $content .= ' fo:font-size="' . $fontSize . 'pt" style:font-size-asian="' . $fontSize . 'pt" style:font-size-complex="' . $fontSize . 'pt"'; + } + + if ($style->isFontBold()) { + $content .= ' fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"'; + } + if ($style->isFontItalic()) { + $content .= ' fo:font-style="italic" style:font-style-asian="italic" style:font-style-complex="italic"'; + } + if ($style->isFontUnderline()) { + $content .= ' style:text-underline-style="solid" style:text-underline-type="single"'; + } + if ($style->isFontStrikethrough()) { + $content .= ' style:text-line-through-style="solid"'; + } + + $content .= '/>'; + + return $content; + } + + /** + * Returns the contents of the "" section, inside "" section + * + * @param \Box\Spout\Writer\Style\Style $style + * @return string + */ + private function getTableCellPropertiesSectionContent($style) + { + $content = ''; + + if ($style->shouldWrapText()) { + $content .= $this->getWrapTextXMLContent(); + } + + if ($style->shouldApplyBorder()) { + $content .= $this->getBorderXMLContent($style); + } + + if ($style->shouldApplyBackgroundColor()) { + $content .= $this->getBackgroundColorXMLContent($style); + } + + return $content; + } + + /** + * Returns the contents of the wrap text definition for the "" section + * + * @return string + */ + private function getWrapTextXMLContent() + { + return ''; + } + + /** + * Returns the contents of the borders definition for the "" section + * + * @param \Box\Spout\Writer\Style\Style $style + * @return string + */ + private function getBorderXMLContent($style) + { + $borderProperty = ''; + + $borders = array_map(function (BorderPart $borderPart) { + return BorderHelper::serializeBorderPart($borderPart); + }, $style->getBorder()->getParts()); + + return sprintf($borderProperty, implode(' ', $borders)); + } + + /** + * Returns the contents of the background color definition for the "" section + * + * @param \Box\Spout\Writer\Style\Style $style + * @return string + */ + private function getBackgroundColorXMLContent($style) + { + return sprintf( + '', + $style->getBackgroundColor() + ); + } } diff --git a/src/Spout/Writer/Style/Style.php b/src/Spout/Writer/Style/Style.php index 69d5e81..b408ad3 100644 --- a/src/Spout/Writer/Style/Style.php +++ b/src/Spout/Writer/Style/Style.php @@ -106,6 +106,7 @@ class Style /** * @param Border $border + * @return Style */ public function setBorder(Border $border) { @@ -359,37 +360,67 @@ class Style { $mergedStyle = clone $this; - if (!$this->hasSetFontBold && $baseStyle->isFontBold()) { - $mergedStyle->setFontBold(); - } - if (!$this->hasSetFontItalic && $baseStyle->isFontItalic()) { - $mergedStyle->setFontItalic(); - } - if (!$this->hasSetFontUnderline && $baseStyle->isFontUnderline()) { - $mergedStyle->setFontUnderline(); - } - if (!$this->hasSetFontStrikethrough && $baseStyle->isFontStrikethrough()) { - $mergedStyle->setFontStrikethrough(); - } - if (!$this->hasSetFontSize && $baseStyle->getFontSize() !== self::DEFAULT_FONT_SIZE) { - $mergedStyle->setFontSize($baseStyle->getFontSize()); - } - if (!$this->hasSetFontColor && $baseStyle->getFontColor() !== self::DEFAULT_FONT_COLOR) { - $mergedStyle->setFontColor($baseStyle->getFontColor()); - } - if (!$this->hasSetFontName && $baseStyle->getFontName() !== self::DEFAULT_FONT_NAME) { - $mergedStyle->setFontName($baseStyle->getFontName()); - } - if (!$this->hasSetWrapText && $baseStyle->shouldWrapText()) { - $mergedStyle->setShouldWrapText(); - } - if (!$this->getBorder() && $baseStyle->shouldApplyBorder()) { - $mergedStyle->setBorder($baseStyle->getBorder()); - } - if (!$this->hasSetBackgroundColor && $baseStyle->shouldApplyBackgroundColor()) { - $mergedStyle->setBackgroundColor($baseStyle->getBackgroundColor()); - } + $this->mergeFontStyles($mergedStyle, $baseStyle); + $this->mergeOtherFontProperties($mergedStyle, $baseStyle); + $this->mergeCellProperties($mergedStyle, $baseStyle); return $mergedStyle; } + + /** + * @param Style $styleToUpdate (passed as reference) + * @param Style $baseStyle + * @return void + */ + private function mergeFontStyles($styleToUpdate, $baseStyle) + { + if (!$this->hasSetFontBold && $baseStyle->isFontBold()) { + $styleToUpdate->setFontBold(); + } + if (!$this->hasSetFontItalic && $baseStyle->isFontItalic()) { + $styleToUpdate->setFontItalic(); + } + if (!$this->hasSetFontUnderline && $baseStyle->isFontUnderline()) { + $styleToUpdate->setFontUnderline(); + } + if (!$this->hasSetFontStrikethrough && $baseStyle->isFontStrikethrough()) { + $styleToUpdate->setFontStrikethrough(); + } + } + + /** + * @param Style $styleToUpdate Style to update (passed as reference) + * @param Style $baseStyle + * @return void + */ + private function mergeOtherFontProperties($styleToUpdate, $baseStyle) + { + if (!$this->hasSetFontSize && $baseStyle->getFontSize() !== self::DEFAULT_FONT_SIZE) { + $styleToUpdate->setFontSize($baseStyle->getFontSize()); + } + if (!$this->hasSetFontColor && $baseStyle->getFontColor() !== self::DEFAULT_FONT_COLOR) { + $styleToUpdate->setFontColor($baseStyle->getFontColor()); + } + if (!$this->hasSetFontName && $baseStyle->getFontName() !== self::DEFAULT_FONT_NAME) { + $styleToUpdate->setFontName($baseStyle->getFontName()); + } + } + + /** + * @param Style $styleToUpdate Style to update (passed as reference) + * @param Style $baseStyle + * @return void + */ + private function mergeCellProperties($styleToUpdate, $baseStyle) + { + if (!$this->hasSetWrapText && $baseStyle->shouldWrapText()) { + $styleToUpdate->setShouldWrapText(); + } + if (!$this->getBorder() && $baseStyle->shouldApplyBorder()) { + $styleToUpdate->setBorder($baseStyle->getBorder()); + } + if (!$this->hasSetBackgroundColor && $baseStyle->shouldApplyBackgroundColor()) { + $styleToUpdate->setBackgroundColor($baseStyle->getBackgroundColor()); + } + } }