From 238756ab6ed11c014e2fe9304214dfed0286dcfb Mon Sep 17 00:00:00 2001 From: Adrien Loison Date: Tue, 30 May 2017 00:56:50 +0200 Subject: [PATCH] Move Style classes into Common folder (#432) --- README.md | 6 +- src/Spout/Writer/CSV/Writer.php | 2 +- .../Creator}/Style/BorderBuilder.php | 8 +- .../Creator}/Style/StyleBuilder.php | 7 +- .../{ => Common/Entity}/Style/Border.php | 17 +- .../{ => Common/Entity}/Style/BorderPart.php | 4 +- .../{ => Common/Entity}/Style/Color.php | 4 +- .../{ => Common/Entity}/Style/Style.php | 203 +++++++----------- .../Common/Helper/StyleHelperAbstract.php | 37 ++-- .../Common/Helper/StyleHelperInterface.php | 8 +- .../Writer/Common/Manager/StyleManager.php | 118 ++++++++++ .../Manager/WorkbookManagerAbstract.php | 2 +- .../Manager/WorkbookManagerInterface.php | 2 +- .../Manager/WorksheetManagerInterface.php | 2 +- .../Exception/Border/InvalidNameException.php | 2 +- .../Border/InvalidStyleException.php | 2 +- .../Border/InvalidWidthException.php | 2 +- .../Writer/ODS/Creator/InternalFactory.php | 13 +- src/Spout/Writer/ODS/Helper/BorderHelper.php | 4 +- src/Spout/Writer/ODS/Helper/StyleHelper.php | 18 +- .../Writer/ODS/Manager/OptionsManager.php | 2 +- .../Writer/ODS/Manager/WorksheetManager.php | 2 +- src/Spout/Writer/WriterAbstract.php | 36 ++-- src/Spout/Writer/WriterFactory.php | 12 +- src/Spout/Writer/WriterInterface.php | 6 +- .../Writer/WriterMultiSheetsAbstract.php | 8 +- .../Writer/XLSX/Creator/InternalFactory.php | 13 +- src/Spout/Writer/XLSX/Helper/BorderHelper.php | 4 +- src/Spout/Writer/XLSX/Helper/StyleHelper.php | 18 +- .../Writer/XLSX/Manager/OptionsManager.php | 2 +- .../Writer/XLSX/Manager/WorksheetManager.php | 4 +- .../{ => Common/Entity}/Style/BorderTest.php | 18 +- .../{ => Common/Entity}/Style/ColorTest.php | 4 +- .../Manager/StyleManagerTest.php} | 53 +++-- .../Writer/ODS/Helper/StyleHelperTest.php | 26 +-- .../Spout/Writer/ODS/WriterWithStyleTest.php | 22 +- .../Writer/XLSX/Helper/StyleHelperTest.php | 45 ++-- .../Spout/Writer/XLSX/WriterWithStyleTest.php | 25 +-- 38 files changed, 463 insertions(+), 298 deletions(-) rename src/Spout/Writer/{ => Common/Creator}/Style/BorderBuilder.php (90%) rename src/Spout/Writer/{ => Common/Creator}/Style/StyleBuilder.php (93%) rename src/Spout/Writer/{ => Common/Entity}/Style/Border.php (80%) rename src/Spout/Writer/{ => Common/Entity}/Style/BorderPart.php (97%) rename src/Spout/Writer/{ => Common/Entity}/Style/Color.php (96%) rename src/Spout/Writer/{ => Common/Entity}/Style/Style.php (58%) create mode 100644 src/Spout/Writer/Common/Manager/StyleManager.php rename tests/Spout/Writer/{ => Common/Entity}/Style/BorderTest.php (84%) rename tests/Spout/Writer/{ => Common/Entity}/Style/ColorTest.php (95%) rename tests/Spout/Writer/{Style/StyleTest.php => Common/Manager/StyleManagerTest.php} (67%) diff --git a/README.md b/README.md index 531b985..9650cc4 100644 --- a/README.md +++ b/README.md @@ -137,7 +137,7 @@ It is possible to apply some formatting options to a row. Spout supports fonts, ```php use Box\Spout\Common\Type; use Box\Spout\Writer\WriterFactory; -use Box\Spout\Writer\Style\StyleBuilder; +use Box\Spout\Writer\Common\Creator\Style\StyleBuilder; use Box\Spout\Writer\Style\Color; $style = (new StyleBuilder()) @@ -163,9 +163,9 @@ Adding borders to a row requires a ```Border``` object. ```php use Box\Spout\Common\Type; use Box\Spout\Writer\Style\Border; -use Box\Spout\Writer\Style\BorderBuilder; +use Box\Spout\Writer\Common\Creator\Style\BorderBuilder; use Box\Spout\Writer\Style\Color; -use Box\Spout\Writer\Style\StyleBuilder; +use Box\Spout\Writer\Common\Creator\Style\StyleBuilder; use Box\Spout\Writer\WriterFactory; $border = (new BorderBuilder()) diff --git a/src/Spout/Writer/CSV/Writer.php b/src/Spout/Writer/CSV/Writer.php index 9740fd9..8bf3c0f 100644 --- a/src/Spout/Writer/CSV/Writer.php +++ b/src/Spout/Writer/CSV/Writer.php @@ -81,7 +81,7 @@ class Writer extends WriterAbstract * * @param array $dataRow Array containing data to be written. * Example $dataRow = ['data1', 1234, null, '', 'data5']; - * @param \Box\Spout\Writer\Style\Style $style Ignored here since CSV does not support styling. + * @param \Box\Spout\Writer\Common\Entity\Style\Style $style Ignored here since CSV does not support styling. * @return void * @throws \Box\Spout\Common\Exception\IOException If unable to write data */ diff --git a/src/Spout/Writer/Style/BorderBuilder.php b/src/Spout/Writer/Common/Creator/Style/BorderBuilder.php similarity index 90% rename from src/Spout/Writer/Style/BorderBuilder.php rename to src/Spout/Writer/Common/Creator/Style/BorderBuilder.php index c0b8aea..81609e0 100644 --- a/src/Spout/Writer/Style/BorderBuilder.php +++ b/src/Spout/Writer/Common/Creator/Style/BorderBuilder.php @@ -1,9 +1,15 @@ hasSetFontBold; + } + /** * @return bool */ @@ -157,6 +165,14 @@ class Style return $this; } + /** + * @return bool + */ + public function hasSetFontItalic() + { + return $this->hasSetFontItalic; + } + /** * @return bool */ @@ -176,6 +192,14 @@ class Style return $this; } + /** + * @return bool + */ + public function hasSetFontUnderline() + { + return $this->hasSetFontUnderline; + } + /** * @return bool */ @@ -195,6 +219,14 @@ class Style return $this; } + /** + * @return bool + */ + public function hasSetFontStrikethrough() + { + return $this->hasSetFontStrikethrough; + } + /** * @return int */ @@ -215,6 +247,14 @@ class Style return $this; } + /** + * @return bool + */ + public function hasSetFontSize() + { + return $this->hasSetFontSize; + } + /** * @return string */ @@ -237,6 +277,14 @@ class Style return $this; } + /** + * @return bool + */ + public function hasSetFontColor() + { + return $this->hasSetFontColor; + } + /** * @return string */ @@ -257,6 +305,14 @@ class Style return $this; } + /** + * @return bool + */ + public function hasSetFontName() + { + return $this->hasSetFontName; + } + /** * @return bool */ @@ -320,103 +376,4 @@ class Style { return $this->hasSetBackgroundColor; } - - /** - * Serializes the style for future comparison with other styles. - * The ID is excluded from the comparison, as we only care about - * actual style properties. - * - * @return string The serialized style - */ - public function serialize() - { - // In order to be able to properly compare style, set static ID value - $currentId = $this->id; - $this->setId(0); - - $serializedStyle = serialize($this); - - $this->setId($currentId); - - return $serializedStyle; - } - - /** - * Merges the current style with the given style, using the given style as a base. This means that: - * - if current style and base style both have property A set, use current style property's value - * - if current style has property A set but base style does not, use current style property's value - * - if base style has property A set but current style does not, use base style property's value - * - * @NOTE: This function returns a new style. - * - * @param Style $baseStyle - * @return Style New style corresponding to the merge of the 2 styles - */ - public function mergeWith($baseStyle) - { - $mergedStyle = clone $this; - - $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()); - } - } } diff --git a/src/Spout/Writer/Common/Helper/StyleHelperAbstract.php b/src/Spout/Writer/Common/Helper/StyleHelperAbstract.php index 269b560..c6c9216 100644 --- a/src/Spout/Writer/Common/Helper/StyleHelperAbstract.php +++ b/src/Spout/Writer/Common/Helper/StyleHelperAbstract.php @@ -2,6 +2,9 @@ namespace Box\Spout\Writer\Common\Helper; +use Box\Spout\Writer\Common\Entity\Style\Style; +use Box\Spout\Writer\Common\Manager\StyleManager; + /** * Class StyleHelperAbstract * This class provides helper functions to manage styles @@ -10,6 +13,9 @@ namespace Box\Spout\Writer\Common\Helper; */ abstract class StyleHelperAbstract implements StyleHelperInterface { + /** @var StyleManager Style manager */ + private $styleManager; + /** @var array [SERIALIZED_STYLE] => [STYLE_ID] mapping table, keeping track of the registered styles */ protected $serializedStyleToStyleIdMappingTable = []; @@ -17,10 +23,13 @@ abstract class StyleHelperAbstract implements StyleHelperInterface protected $styleIdToStyleMappingTable = []; /** - * @param \Box\Spout\Writer\Style\Style $defaultStyle + * @param Style $defaultStyle + * @param StyleManager $styleManager */ - public function __construct($defaultStyle) + public function __construct(Style $defaultStyle, StyleManager $styleManager) { + $this->styleManager = $styleManager; + // This ensures that the default style is the first one to be registered $this->registerStyle($defaultStyle); } @@ -29,12 +38,12 @@ abstract class StyleHelperAbstract implements StyleHelperInterface * Registers the given style as a used style. * Duplicate styles won't be registered more than once. * - * @param \Box\Spout\Writer\Style\Style $style The style to be registered - * @return \Box\Spout\Writer\Style\Style The registered style, updated with an internal ID. + * @param Style $style The style to be registered + * @return Style The registered style, updated with an internal ID. */ public function registerStyle($style) { - $serializedStyle = $style->serialize(); + $serializedStyle = $this->styleManager->serialize($style); if (!$this->hasStyleAlreadyBeenRegistered($style)) { $nextStyleId = count($this->serializedStyleToStyleIdMappingTable); @@ -50,12 +59,12 @@ abstract class StyleHelperAbstract implements StyleHelperInterface /** * Returns whether the given style has already been registered. * - * @param \Box\Spout\Writer\Style\Style $style + * @param Style $style * @return bool */ protected function hasStyleAlreadyBeenRegistered($style) { - $serializedStyle = $style->serialize(); + $serializedStyle = $this->styleManager->serialize($style); // Using isset here because it is way faster than array_key_exists... return isset($this->serializedStyleToStyleIdMappingTable[$serializedStyle]); @@ -65,7 +74,7 @@ abstract class StyleHelperAbstract implements StyleHelperInterface * Returns the registered style associated to the given serialization. * * @param string $serializedStyle The serialized style from which the actual style should be fetched from - * @return \Box\Spout\Writer\Style\Style + * @return Style */ protected function getStyleFromSerializedStyle($serializedStyle) { @@ -74,7 +83,7 @@ abstract class StyleHelperAbstract implements StyleHelperInterface } /** - * @return \Box\Spout\Writer\Style\Style[] List of registered styles + * @return Style[] List of registered styles */ protected function getRegisteredStyles() { @@ -84,7 +93,7 @@ abstract class StyleHelperAbstract implements StyleHelperInterface /** * Returns the default style * - * @return \Box\Spout\Writer\Style\Style Default style + * @return Style Default style */ protected function getDefaultStyle() { @@ -96,9 +105,9 @@ abstract class StyleHelperAbstract implements StyleHelperInterface * Apply additional styles if the given row needs it. * Typically, set "wrap text" if a cell contains a new line. * - * @param \Box\Spout\Writer\Style\Style $style The original style + * @param Style $style The original style * @param array $dataRow The row the style will be applied to - * @return \Box\Spout\Writer\Style\Style The updated style + * @return Style The updated style */ public function applyExtraStylesIfNeeded($style, $dataRow) { @@ -115,9 +124,9 @@ abstract class StyleHelperAbstract implements StyleHelperInterface * A workaround would be to encode "\n" as "_x000D_" but it does not work * on the Windows version of Excel... * - * @param \Box\Spout\Writer\Style\Style $style The original style + * @param Style $style The original style * @param array $dataRow The row the style will be applied to - * @return \Box\Spout\Writer\Style\Style The eventually updated style + * @return Style The eventually updated style */ protected function applyWrapTextIfCellContainsNewLine($style, $dataRow) { diff --git a/src/Spout/Writer/Common/Helper/StyleHelperInterface.php b/src/Spout/Writer/Common/Helper/StyleHelperInterface.php index a73d1cb..9066178 100644 --- a/src/Spout/Writer/Common/Helper/StyleHelperInterface.php +++ b/src/Spout/Writer/Common/Helper/StyleHelperInterface.php @@ -13,8 +13,8 @@ interface StyleHelperInterface * Registers the given style as a used style. * Duplicate styles won't be registered more than once. * - * @param \Box\Spout\Writer\Style\Style $style The style to be registered - * @return \Box\Spout\Writer\Style\Style The registered style, updated with an internal ID. + * @param \Box\Spout\Writer\Common\Entity\Style\Style $style The style to be registered + * @return \Box\Spout\Writer\Common\Entity\Style\Style The registered style, updated with an internal ID. */ public function registerStyle($style); @@ -22,9 +22,9 @@ interface StyleHelperInterface * Apply additional styles if the given row needs it. * Typically, set "wrap text" if a cell contains a new line. * - * @param \Box\Spout\Writer\Style\Style $style The original style + * @param \Box\Spout\Writer\Common\Entity\Style\Style $style The original style * @param array $dataRow The row the style will be applied to - * @return \Box\Spout\Writer\Style\Style The updated style + * @return \Box\Spout\Writer\Common\Entity\Style\Style The updated style */ public function applyExtraStylesIfNeeded($style, $dataRow); } diff --git a/src/Spout/Writer/Common/Manager/StyleManager.php b/src/Spout/Writer/Common/Manager/StyleManager.php new file mode 100644 index 0000000..25a0d1d --- /dev/null +++ b/src/Spout/Writer/Common/Manager/StyleManager.php @@ -0,0 +1,118 @@ +getId(); + $style->setId(0); + + $serializedStyle = serialize($style); + + $style->setId($currentId); + + return $serializedStyle; + } + + /** + * Merges the current style with the given style, using the given style as a base. This means that: + * - if current style and base style both have property A set, use current style property's value + * - if current style has property A set but base style does not, use current style property's value + * - if base style has property A set but current style does not, use base style property's value + * + * @NOTE: This function returns a new style. + * + * @param Style $style + * @param Style $baseStyle + * @return Style New style corresponding to the merge of the 2 styles + */ + public function merge(Style $style, Style $baseStyle) + { + $mergedStyle = clone $style; + + $this->mergeFontStyles($mergedStyle, $style, $baseStyle); + $this->mergeOtherFontProperties($mergedStyle, $style, $baseStyle); + $this->mergeCellProperties($mergedStyle, $style, $baseStyle); + + return $mergedStyle; + } + + /** + * @param Style $styleToUpdate (passed as reference) + * @param Style $style + * @param Style $baseStyle + * @return void + */ + private function mergeFontStyles(Style $styleToUpdate, Style $style, Style $baseStyle) + { + if (!$style->hasSetFontBold() && $baseStyle->isFontBold()) { + $styleToUpdate->setFontBold(); + } + if (!$style->hasSetFontItalic() && $baseStyle->isFontItalic()) { + $styleToUpdate->setFontItalic(); + } + if (!$style->hasSetFontUnderline() && $baseStyle->isFontUnderline()) { + $styleToUpdate->setFontUnderline(); + } + if (!$style->hasSetFontStrikethrough() && $baseStyle->isFontStrikethrough()) { + $styleToUpdate->setFontStrikethrough(); + } + } + + /** + * @param Style $styleToUpdate Style to update (passed as reference) + * @param Style $style + * @param Style $baseStyle + * @return void + */ + private function mergeOtherFontProperties(Style $styleToUpdate, Style $style, Style $baseStyle) + { + if (!$style->hasSetFontSize() && $baseStyle->getFontSize() !== Style::DEFAULT_FONT_SIZE) { + $styleToUpdate->setFontSize($baseStyle->getFontSize()); + } + if (!$style->hasSetFontColor() && $baseStyle->getFontColor() !== Style::DEFAULT_FONT_COLOR) { + $styleToUpdate->setFontColor($baseStyle->getFontColor()); + } + if (!$style->hasSetFontName() && $baseStyle->getFontName() !== Style::DEFAULT_FONT_NAME) { + $styleToUpdate->setFontName($baseStyle->getFontName()); + } + } + + /** + * @param Style $styleToUpdate Style to update (passed as reference) + * @param Style $style + * @param Style $baseStyle + * @return void + */ + private function mergeCellProperties(Style $styleToUpdate, Style $style, Style $baseStyle) + { + if (!$style->hasSetWrapText() && $baseStyle->shouldWrapText()) { + $styleToUpdate->setShouldWrapText(); + } + if (!$style->getBorder() && $baseStyle->shouldApplyBorder()) { + $styleToUpdate->setBorder($baseStyle->getBorder()); + } + if (!$style->shouldApplyBackgroundColor() && $baseStyle->shouldApplyBackgroundColor()) { + $styleToUpdate->setBackgroundColor($baseStyle->getBackgroundColor()); + } + } +} diff --git a/src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php b/src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php index 050f673..96d4ff9 100644 --- a/src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php +++ b/src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php @@ -12,7 +12,7 @@ use Box\Spout\Writer\Common\Entity\Worksheet; use Box\Spout\Writer\Exception\SheetNotFoundException; use Box\Spout\Writer\Exception\WriterException; use Box\Spout\Writer\Common\Creator\EntityFactory; -use Box\Spout\Writer\Style\Style; +use Box\Spout\Writer\Common\Entity\Style\Style; /** * Class WorkbookManagerAbstract diff --git a/src/Spout/Writer/Common/Manager/WorkbookManagerInterface.php b/src/Spout/Writer/Common/Manager/WorkbookManagerInterface.php index e410004..fcd8854 100644 --- a/src/Spout/Writer/Common/Manager/WorkbookManagerInterface.php +++ b/src/Spout/Writer/Common/Manager/WorkbookManagerInterface.php @@ -8,7 +8,7 @@ use Box\Spout\Writer\Common\Entity\Workbook; use Box\Spout\Writer\Common\Entity\Worksheet; use Box\Spout\Writer\Exception\SheetNotFoundException; use Box\Spout\Writer\Exception\WriterException; -use Box\Spout\Writer\Style\Style; +use Box\Spout\Writer\Common\Entity\Style\Style; /** * Interface WorkbookManagerInterface diff --git a/src/Spout/Writer/Common/Manager/WorksheetManagerInterface.php b/src/Spout/Writer/Common/Manager/WorksheetManagerInterface.php index 4904c84..8f41559 100644 --- a/src/Spout/Writer/Common/Manager/WorksheetManagerInterface.php +++ b/src/Spout/Writer/Common/Manager/WorksheetManagerInterface.php @@ -3,7 +3,7 @@ namespace Box\Spout\Writer\Common\Manager; use Box\Spout\Writer\Common\Entity\Worksheet; -use Box\Spout\Writer\Style\Style; +use Box\Spout\Writer\Common\Entity\Style\Style; /** * Interface WorksheetManagerInterface diff --git a/src/Spout/Writer/Exception/Border/InvalidNameException.php b/src/Spout/Writer/Exception/Border/InvalidNameException.php index 13ac06c..329323a 100644 --- a/src/Spout/Writer/Exception/Border/InvalidNameException.php +++ b/src/Spout/Writer/Exception/Border/InvalidNameException.php @@ -3,7 +3,7 @@ namespace Box\Spout\Writer\Exception\Border; use Box\Spout\Writer\Exception\WriterException; -use Box\Spout\Writer\Style\BorderPart; +use Box\Spout\Writer\Common\Entity\Style\BorderPart; class InvalidNameException extends WriterException { diff --git a/src/Spout/Writer/Exception/Border/InvalidStyleException.php b/src/Spout/Writer/Exception/Border/InvalidStyleException.php index c684339..f3261f0 100644 --- a/src/Spout/Writer/Exception/Border/InvalidStyleException.php +++ b/src/Spout/Writer/Exception/Border/InvalidStyleException.php @@ -3,7 +3,7 @@ namespace Box\Spout\Writer\Exception\Border; use Box\Spout\Writer\Exception\WriterException; -use Box\Spout\Writer\Style\BorderPart; +use Box\Spout\Writer\Common\Entity\Style\BorderPart; class InvalidStyleException extends WriterException { diff --git a/src/Spout/Writer/Exception/Border/InvalidWidthException.php b/src/Spout/Writer/Exception/Border/InvalidWidthException.php index 32f092b..0f24faf 100644 --- a/src/Spout/Writer/Exception/Border/InvalidWidthException.php +++ b/src/Spout/Writer/Exception/Border/InvalidWidthException.php @@ -3,7 +3,7 @@ namespace Box\Spout\Writer\Exception\Border; use Box\Spout\Writer\Exception\WriterException; -use Box\Spout\Writer\Style\BorderPart; +use Box\Spout\Writer\Common\Entity\Style\BorderPart; class InvalidWidthException extends WriterException { diff --git a/src/Spout/Writer/ODS/Creator/InternalFactory.php b/src/Spout/Writer/ODS/Creator/InternalFactory.php index a2adb05..8677639 100644 --- a/src/Spout/Writer/ODS/Creator/InternalFactory.php +++ b/src/Spout/Writer/ODS/Creator/InternalFactory.php @@ -7,6 +7,7 @@ use Box\Spout\Writer\Common\Manager\OptionsManagerInterface; use Box\Spout\Writer\Common\Entity\Options; use Box\Spout\Writer\Common\Creator\EntityFactory; use Box\Spout\Writer\Common\Creator\InternalFactoryInterface; +use Box\Spout\Writer\Common\Manager\StyleManager; use Box\Spout\Writer\ODS\Helper\FileSystemHelper; use Box\Spout\Writer\ODS\Helper\StyleHelper; use Box\Spout\Writer\ODS\Manager\WorkbookManager; @@ -80,7 +81,17 @@ class InternalFactory implements InternalFactoryInterface private function createStyleHelper(OptionsManagerInterface $optionsManager) { $defaultRowStyle = $optionsManager->getOption(Options::DEFAULT_ROW_STYLE); - return new StyleHelper($defaultRowStyle); + $styleManager = $this->createStyleManager(); + + return new StyleHelper($defaultRowStyle, $styleManager); + } + + /** + * @return StyleManager + */ + private function createStyleManager() + { + return new StyleManager(); } /** diff --git a/src/Spout/Writer/ODS/Helper/BorderHelper.php b/src/Spout/Writer/ODS/Helper/BorderHelper.php index f3c3fed..a619ffd 100644 --- a/src/Spout/Writer/ODS/Helper/BorderHelper.php +++ b/src/Spout/Writer/ODS/Helper/BorderHelper.php @@ -2,8 +2,8 @@ namespace Box\Spout\Writer\ODS\Helper; -use Box\Spout\Writer\Style\BorderPart; -use Box\Spout\Writer\Style\Border; +use Box\Spout\Writer\Common\Entity\Style\BorderPart; +use Box\Spout\Writer\Common\Entity\Style\Border; /** * Class BorderHelper diff --git a/src/Spout/Writer/ODS/Helper/StyleHelper.php b/src/Spout/Writer/ODS/Helper/StyleHelper.php index e8a4110..29e15b4 100644 --- a/src/Spout/Writer/ODS/Helper/StyleHelper.php +++ b/src/Spout/Writer/ODS/Helper/StyleHelper.php @@ -3,7 +3,7 @@ namespace Box\Spout\Writer\ODS\Helper; use Box\Spout\Writer\Common\Helper\StyleHelperAbstract; -use Box\Spout\Writer\Style\BorderPart; +use Box\Spout\Writer\Common\Entity\Style\BorderPart; /** * Class StyleHelper @@ -20,8 +20,8 @@ class StyleHelper extends StyleHelperAbstract * Registers the given style as a used style. * Duplicate styles won't be registered more than once. * - * @param \Box\Spout\Writer\Style\Style $style The style to be registered - * @return \Box\Spout\Writer\Style\Style The registered style, updated with an internal ID. + * @param \Box\Spout\Writer\Common\Entity\Style\Style $style The style to be registered + * @return \Box\Spout\Writer\Common\Entity\Style\Style The registered style, updated with an internal ID. */ public function registerStyle($style) { @@ -209,7 +209,7 @@ EOD; /** * Returns the contents of the "" section, inside "" section * - * @param \Box\Spout\Writer\Style\Style $style + * @param \Box\Spout\Writer\Common\Entity\Style\Style $style * @return string */ protected function getStyleSectionContent($style) @@ -229,7 +229,7 @@ EOD; /** * Returns the contents of the "" section, inside "" section * - * @param \Box\Spout\Writer\Style\Style $style + * @param \Box\Spout\Writer\Common\Entity\Style\Style $style * @return string */ private function getTextPropertiesSectionContent($style) @@ -246,7 +246,7 @@ EOD; /** * Returns the contents of the "" section, inside "" section * - * @param \Box\Spout\Writer\Style\Style $style + * @param \Box\Spout\Writer\Common\Entity\Style\Style $style * @return string */ private function getFontSectionContent($style) @@ -291,7 +291,7 @@ EOD; /** * Returns the contents of the "" section, inside "" section * - * @param \Box\Spout\Writer\Style\Style $style + * @param \Box\Spout\Writer\Common\Entity\Style\Style $style * @return string */ private function getTableCellPropertiesSectionContent($style) @@ -326,7 +326,7 @@ EOD; /** * Returns the contents of the borders definition for the "" section * - * @param \Box\Spout\Writer\Style\Style $style + * @param \Box\Spout\Writer\Common\Entity\Style\Style $style * @return string */ private function getBorderXMLContent($style) @@ -343,7 +343,7 @@ EOD; /** * Returns the contents of the background color definition for the "" section * - * @param \Box\Spout\Writer\Style\Style $style + * @param \Box\Spout\Writer\Common\Entity\Style\Style $style * @return string */ private function getBackgroundColorXMLContent($style) diff --git a/src/Spout/Writer/ODS/Manager/OptionsManager.php b/src/Spout/Writer/ODS/Manager/OptionsManager.php index 20d42d7..4a83428 100644 --- a/src/Spout/Writer/ODS/Manager/OptionsManager.php +++ b/src/Spout/Writer/ODS/Manager/OptionsManager.php @@ -4,7 +4,7 @@ namespace Box\Spout\Writer\ODS\Manager; use Box\Spout\Writer\Common\Entity\Options; use Box\Spout\Writer\Common\Manager\OptionsManagerAbstract; -use Box\Spout\Writer\Style\StyleBuilder; +use Box\Spout\Writer\Common\Creator\Style\StyleBuilder; /** * Class OptionsManager diff --git a/src/Spout/Writer/ODS/Manager/WorksheetManager.php b/src/Spout/Writer/ODS/Manager/WorksheetManager.php index 016336b..cb8894a 100644 --- a/src/Spout/Writer/ODS/Manager/WorksheetManager.php +++ b/src/Spout/Writer/ODS/Manager/WorksheetManager.php @@ -9,7 +9,7 @@ use Box\Spout\Writer\Common\Entity\Cell; use Box\Spout\Writer\Common\Entity\Worksheet; use Box\Spout\Writer\Common\Manager\WorksheetManagerInterface; use Box\Spout\Writer\ODS\Helper\StyleHelper; -use Box\Spout\Writer\Style\Style; +use Box\Spout\Writer\Common\Entity\Style\Style; /** * Class WorksheetManager diff --git a/src/Spout/Writer/WriterAbstract.php b/src/Spout/Writer/WriterAbstract.php index 1425406..06abb3e 100644 --- a/src/Spout/Writer/WriterAbstract.php +++ b/src/Spout/Writer/WriterAbstract.php @@ -6,8 +6,11 @@ use Box\Spout\Common\Exception\InvalidArgumentException; use Box\Spout\Common\Exception\IOException; use Box\Spout\Common\Exception\SpoutException; use Box\Spout\Common\Helper\FileSystemHelper; -use Box\Spout\Writer\Common\Manager\OptionsManagerInterface; +use Box\Spout\Common\Helper\GlobalFunctionsHelper; use Box\Spout\Writer\Common\Entity\Options; +use Box\Spout\Writer\Common\Entity\Style\Style; +use Box\Spout\Writer\Common\Manager\OptionsManagerInterface; +use Box\Spout\Writer\Common\Manager\StyleManager; use Box\Spout\Writer\Exception\WriterAlreadyOpenedException; use Box\Spout\Writer\Exception\WriterNotOpenedException; @@ -28,13 +31,16 @@ abstract class WriterAbstract implements WriterInterface /** @var bool Indicates whether the writer has been opened or not */ protected $isWriterOpened = false; - /** @var \Box\Spout\Common\Helper\GlobalFunctionsHelper Helper to work with global functions */ + /** @var GlobalFunctionsHelper Helper to work with global functions */ protected $globalFunctionsHelper; - /** @var \Box\Spout\Writer\Common\Manager\OptionsManagerInterface Writer options manager */ + /** @var OptionsManagerInterface Writer options manager */ protected $optionsManager; - /** @var Style\Style Style to be applied to the next written row(s) */ + /** @var StyleManager Style manager */ + protected $styleManager; + + /** @var Style Style to be applied to the next written row(s) */ protected $rowStyle; /** @var string Content-Type value for the header - to be defined by child class */ @@ -53,7 +59,7 @@ abstract class WriterAbstract implements WriterInterface * * @param array $dataRow Array containing data to be streamed. * Example $dataRow = ['data1', 1234, null, '', 'data5']; - * @param Style\Style $style Style to be applied to the written row + * @param Style $style Style to be applied to the written row * @return void */ abstract protected function addRowToWriter(array $dataRow, $style); @@ -66,11 +72,13 @@ abstract class WriterAbstract implements WriterInterface abstract protected function closeWriter(); /** - * @param \Box\Spout\Writer\Common\Manager\OptionsManagerInterface $optionsManager + * @param OptionsManagerInterface $optionsManager + * @param StyleManager $styleManager */ - public function __construct(OptionsManagerInterface $optionsManager) + public function __construct(OptionsManagerInterface $optionsManager, StyleManager $styleManager) { $this->optionsManager = $optionsManager; + $this->styleManager = $styleManager; $this->resetRowStyleToDefault(); } @@ -79,7 +87,7 @@ abstract class WriterAbstract implements WriterInterface * Overriding the default style instead of using "addRowWithStyle" improves performance by 20%. * @see https://github.com/box/spout/issues/272 * - * @param Style\Style $defaultStyle + * @param Style $defaultStyle * @return WriterAbstract */ public function setDefaultRowStyle($defaultStyle) @@ -233,7 +241,7 @@ abstract class WriterAbstract implements WriterInterface * * @api * @param array $dataRow Array of array containing data to be streamed. - * @param Style\Style $style Style to be applied to the row. + * @param Style $style Style to be applied to the row. * @return WriterAbstract * @throws \Box\Spout\Common\Exception\InvalidArgumentException If the input param is not valid * @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If this function is called before opening the writer @@ -241,7 +249,7 @@ abstract class WriterAbstract implements WriterInterface */ public function addRowWithStyle(array $dataRow, $style) { - if (!$style instanceof Style\Style) { + if (!$style instanceof Style) { throw new InvalidArgumentException('The "$style" argument must be a Style instance and cannot be NULL.'); } @@ -289,7 +297,7 @@ abstract class WriterAbstract implements WriterInterface * * @api * @param array $dataRows Array of array containing data to be streamed. - * @param Style\Style $style Style to be applied to the rows. + * @param Style $style Style to be applied to the rows. * @return WriterAbstract * @throws \Box\Spout\Common\Exception\InvalidArgumentException If the input param is not valid * @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If this function is called before opening the writer @@ -297,7 +305,7 @@ abstract class WriterAbstract implements WriterInterface */ public function addRowsWithStyle(array $dataRows, $style) { - if (!$style instanceof Style\Style) { + if (!$style instanceof Style) { throw new InvalidArgumentException('The "$style" argument must be a Style instance and cannot be NULL.'); } @@ -312,14 +320,14 @@ abstract class WriterAbstract implements WriterInterface * Sets the style to be applied to the next written rows * until it is changed or reset. * - * @param Style\Style $style + * @param Style $style * @return void */ private function setRowStyle($style) { // Merge given style with the default one to inherit custom properties $defaultRowStyle = $this->optionsManager->getOption(Options::DEFAULT_ROW_STYLE); - $this->rowStyle = $style->mergeWith($defaultRowStyle); + $this->rowStyle = $this->styleManager->merge($style, $defaultRowStyle); } /** diff --git a/src/Spout/Writer/WriterFactory.php b/src/Spout/Writer/WriterFactory.php index 7b2e9b5..3105665 100644 --- a/src/Spout/Writer/WriterFactory.php +++ b/src/Spout/Writer/WriterFactory.php @@ -6,7 +6,8 @@ use Box\Spout\Common\Exception\UnsupportedTypeException; use Box\Spout\Common\Helper\GlobalFunctionsHelper; use Box\Spout\Common\Type; use Box\Spout\Writer\Common\Creator\EntityFactory; -use Box\Spout\Writer\Style\StyleBuilder; +use Box\Spout\Writer\Common\Creator\Style\StyleBuilder; +use Box\Spout\Writer\Common\Manager\StyleManager; /** * Class WriterFactory @@ -54,8 +55,9 @@ class WriterFactory private static function getCSVWriter() { $optionsManager = new CSV\Manager\OptionsManager(); + $styleManager = new StyleManager(); - return new CSV\Writer($optionsManager); + return new CSV\Writer($optionsManager, $styleManager); } /** @@ -65,9 +67,10 @@ class WriterFactory { $styleBuilder = new StyleBuilder(); $optionsManager = new XLSX\Manager\OptionsManager($styleBuilder); + $styleManager = new StyleManager(); $generalFactory = new XLSX\Creator\InternalFactory(new EntityFactory()); - return new XLSX\Writer($optionsManager, $generalFactory); + return new XLSX\Writer($optionsManager, $styleManager, $generalFactory); } /** @@ -77,8 +80,9 @@ class WriterFactory { $styleBuilder = new StyleBuilder(); $optionsManager = new ODS\Manager\OptionsManager($styleBuilder); + $styleManager = new StyleManager(); $generalFactory = new ODS\Creator\InternalFactory(new EntityFactory()); - return new ODS\Writer($optionsManager, $generalFactory); + return new ODS\Writer($optionsManager, $styleManager, $generalFactory); } } diff --git a/src/Spout/Writer/WriterInterface.php b/src/Spout/Writer/WriterInterface.php index e2d9f8d..d9dee14 100644 --- a/src/Spout/Writer/WriterInterface.php +++ b/src/Spout/Writer/WriterInterface.php @@ -2,6 +2,8 @@ namespace Box\Spout\Writer; +use Box\Spout\Writer\Common\Entity\Style\Style; + /** * Interface WriterInterface * @@ -45,7 +47,7 @@ interface WriterInterface * @see addRow * * @param array $dataRow Array of array containing data to be streamed. - * @param Style\Style $style Style to be applied to the row. + * @param Style $style Style to be applied to the row. * @return WriterInterface * @throws \Box\Spout\Common\Exception\InvalidArgumentException If the input param is not valid * @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If this function is called before opening the writer @@ -73,7 +75,7 @@ interface WriterInterface * @see addRows * * @param array $dataRows Array of array containing data to be streamed. - * @param Style\Style $style Style to be applied to the rows. + * @param Style $style Style to be applied to the rows. * @return WriterInterface * @throws \Box\Spout\Common\Exception\InvalidArgumentException If the input param is not valid * @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If this function is called before opening the writer diff --git a/src/Spout/Writer/WriterMultiSheetsAbstract.php b/src/Spout/Writer/WriterMultiSheetsAbstract.php index 0784a47..0879580 100644 --- a/src/Spout/Writer/WriterMultiSheetsAbstract.php +++ b/src/Spout/Writer/WriterMultiSheetsAbstract.php @@ -5,6 +5,7 @@ namespace Box\Spout\Writer; use Box\Spout\Writer\Common\Manager\OptionsManagerInterface; use Box\Spout\Writer\Common\Entity\Options; use Box\Spout\Writer\Common\Entity\Worksheet; +use Box\Spout\Writer\Common\Manager\StyleManager; use Box\Spout\Writer\Exception\WriterNotOpenedException; use Box\Spout\Writer\Common\Creator\InternalFactoryInterface; use Box\Spout\Writer\Common\Manager\WorkbookManagerInterface; @@ -26,11 +27,12 @@ abstract class WriterMultiSheetsAbstract extends WriterAbstract /** * @param OptionsManagerInterface $optionsManager + * @param StyleManager $styleManager * @param InternalFactoryInterface $internalFactory */ - public function __construct(OptionsManagerInterface $optionsManager, InternalFactoryInterface $internalFactory) + public function __construct(OptionsManagerInterface $optionsManager, StyleManager $styleManager, InternalFactoryInterface $internalFactory) { - parent::__construct($optionsManager); + parent::__construct($optionsManager, $styleManager); $this->internalFactory = $internalFactory; } @@ -151,7 +153,7 @@ abstract class WriterMultiSheetsAbstract extends WriterAbstract * * @param array $dataRow Array containing data to be written. * Example $dataRow = ['data1', 1234, null, '', 'data5']; - * @param \Box\Spout\Writer\Style\Style $style Style to be applied to the row. + * @param \Box\Spout\Writer\Common\Entity\Style\Style $style Style to be applied to the row. * @return void * @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If the book is not created yet * @throws \Box\Spout\Common\Exception\IOException If unable to write data diff --git a/src/Spout/Writer/XLSX/Creator/InternalFactory.php b/src/Spout/Writer/XLSX/Creator/InternalFactory.php index 301cf33..ca15408 100644 --- a/src/Spout/Writer/XLSX/Creator/InternalFactory.php +++ b/src/Spout/Writer/XLSX/Creator/InternalFactory.php @@ -10,6 +10,7 @@ use Box\Spout\Writer\Common\Creator\EntityFactory; use Box\Spout\Writer\Common\Creator\InternalFactoryInterface; use Box\Spout\Writer\Common\Creator\WorkbookFactory; use Box\Spout\Writer\Common\Creator\WorksheetFactory; +use Box\Spout\Writer\Common\Manager\StyleManager; use Box\Spout\Writer\XLSX\Helper\FileSystemHelper; use Box\Spout\Writer\XLSX\Helper\SharedStringsHelper; use Box\Spout\Writer\XLSX\Helper\StyleHelper; @@ -102,7 +103,17 @@ class InternalFactory implements InternalFactoryInterface private function createStyleHelper(OptionsManagerInterface $optionsManager) { $defaultRowStyle = $optionsManager->getOption(Options::DEFAULT_ROW_STYLE); - return new StyleHelper($defaultRowStyle); + $styleManager = $this->createStyleManager(); + + return new StyleHelper($defaultRowStyle, $styleManager); + } + + /** + * @return StyleManager + */ + private function createStyleManager() + { + return new StyleManager(); } /** diff --git a/src/Spout/Writer/XLSX/Helper/BorderHelper.php b/src/Spout/Writer/XLSX/Helper/BorderHelper.php index ad63aea..1b0183a 100644 --- a/src/Spout/Writer/XLSX/Helper/BorderHelper.php +++ b/src/Spout/Writer/XLSX/Helper/BorderHelper.php @@ -2,8 +2,8 @@ namespace Box\Spout\Writer\XLSX\Helper; -use Box\Spout\Writer\Style\Border; -use Box\Spout\Writer\Style\BorderPart; +use Box\Spout\Writer\Common\Entity\Style\Border; +use Box\Spout\Writer\Common\Entity\Style\BorderPart; class BorderHelper { diff --git a/src/Spout/Writer/XLSX/Helper/StyleHelper.php b/src/Spout/Writer/XLSX/Helper/StyleHelper.php index 2795c87..330f8cc 100644 --- a/src/Spout/Writer/XLSX/Helper/StyleHelper.php +++ b/src/Spout/Writer/XLSX/Helper/StyleHelper.php @@ -3,8 +3,8 @@ namespace Box\Spout\Writer\XLSX\Helper; use Box\Spout\Writer\Common\Helper\StyleHelperAbstract; -use Box\Spout\Writer\Style\Color; -use Box\Spout\Writer\Style\Style; +use Box\Spout\Writer\Common\Entity\Style\Color; +use Box\Spout\Writer\Common\Entity\Style\Style; /** * Class StyleHelper @@ -45,8 +45,8 @@ class StyleHelper extends StyleHelperAbstract /** * XLSX specific operations on the registered styles * - * @param \Box\Spout\Writer\Style\Style $style - * @return \Box\Spout\Writer\Style\Style + * @param \Box\Spout\Writer\Common\Entity\Style\Style $style + * @return \Box\Spout\Writer\Common\Entity\Style\Style */ public function registerStyle($style) { @@ -59,7 +59,7 @@ class StyleHelper extends StyleHelperAbstract /** * Register a fill definition * - * @param \Box\Spout\Writer\Style\Style $style + * @param \Box\Spout\Writer\Common\Entity\Style\Style $style */ protected function registerFill($style) { @@ -92,7 +92,7 @@ class StyleHelper extends StyleHelperAbstract /** * Register a border definition * - * @param \Box\Spout\Writer\Style\Style $style + * @param \Box\Spout\Writer\Common\Entity\Style\Style $style */ protected function registerBorder($style) { @@ -174,7 +174,7 @@ EOD; { $content = ''; - /** @var \Box\Spout\Writer\Style\Style $style */ + /** @var \Box\Spout\Writer\Common\Entity\Style\Style $style */ foreach ($this->getRegisteredStyles() as $style) { $content .= ''; @@ -251,7 +251,7 @@ EOD; $content .= ''; foreach ($this->registeredBorders as $styleId) { - /** @var \Box\Spout\Writer\Style\Style $style */ + /** @var \Box\Spout\Writer\Common\Entity\Style\Style $style */ $style = $this->styleIdToStyleMappingTable[$styleId]; $border = $style->getBorder(); $content .= ''; @@ -261,7 +261,7 @@ EOD; foreach ($sortOrder as $partName) { if ($border->hasPart($partName)) { - /** @var $part \Box\Spout\Writer\Style\BorderPart */ + /** @var $part \Box\Spout\Writer\Common\Entity\Style\BorderPart */ $part = $border->getPart($partName); $content .= BorderHelper::serializeBorderPart($part); } diff --git a/src/Spout/Writer/XLSX/Manager/OptionsManager.php b/src/Spout/Writer/XLSX/Manager/OptionsManager.php index 5980d91..425f2f6 100644 --- a/src/Spout/Writer/XLSX/Manager/OptionsManager.php +++ b/src/Spout/Writer/XLSX/Manager/OptionsManager.php @@ -4,7 +4,7 @@ namespace Box\Spout\Writer\XLSX\Manager; use Box\Spout\Writer\Common\Entity\Options; use Box\Spout\Writer\Common\Manager\OptionsManagerAbstract; -use Box\Spout\Writer\Style\StyleBuilder; +use Box\Spout\Writer\Common\Creator\Style\StyleBuilder; /** * Class OptionsManager diff --git a/src/Spout/Writer/XLSX/Manager/WorksheetManager.php b/src/Spout/Writer/XLSX/Manager/WorksheetManager.php index 07f5550..ad6753c 100644 --- a/src/Spout/Writer/XLSX/Manager/WorksheetManager.php +++ b/src/Spout/Writer/XLSX/Manager/WorksheetManager.php @@ -11,7 +11,7 @@ use Box\Spout\Writer\Common\Entity\Options; use Box\Spout\Writer\Common\Entity\Cell; use Box\Spout\Writer\Common\Entity\Worksheet; use Box\Spout\Writer\Common\Manager\WorksheetManagerInterface; -use Box\Spout\Writer\Style\Style; +use Box\Spout\Writer\Common\Entity\Style\Style; use Box\Spout\Writer\XLSX\Helper\SharedStringsHelper; use Box\Spout\Writer\XLSX\Helper\StyleHelper; @@ -155,7 +155,7 @@ EOD; * @param Worksheet $worksheet The worksheet to add the row to * @param array $dataRow Array containing data to be written. Cannot be empty. * Example $dataRow = ['data1', 1234, null, '', 'data5']; - * @param \Box\Spout\Writer\Style\Style $style Style to be applied to the row. NULL means use default style. + * @param \Box\Spout\Writer\Common\Entity\Style\Style $style Style to be applied to the row. NULL means use default style. * @return void * @throws \Box\Spout\Common\Exception\IOException If the data cannot be written * @throws \Box\Spout\Common\Exception\InvalidArgumentException If a cell value's type is not supported diff --git a/tests/Spout/Writer/Style/BorderTest.php b/tests/Spout/Writer/Common/Entity/Style/BorderTest.php similarity index 84% rename from tests/Spout/Writer/Style/BorderTest.php rename to tests/Spout/Writer/Common/Entity/Style/BorderTest.php index 181d8cf..e9cd1d2 100644 --- a/tests/Spout/Writer/Style/BorderTest.php +++ b/tests/Spout/Writer/Common/Entity/Style/BorderTest.php @@ -1,7 +1,17 @@ styleManager = new StyleManager(); + } + /** * @return void */ @@ -20,7 +37,7 @@ class StyleTest extends \PHPUnit_Framework_TestCase $style2 = (new StyleBuilder())->setFontBold()->build(); $style2->setId(2); - $this->assertEquals($style1->serialize(), $style2->serialize()); + $this->assertEquals($this->styleManager->serialize($style1), $this->styleManager->serialize($style2)); } /** @@ -30,7 +47,7 @@ class StyleTest extends \PHPUnit_Framework_TestCase { $baseStyle = (new StyleBuilder())->build(); $currentStyle = (new StyleBuilder())->build(); - $mergedStyle = $currentStyle->mergeWith($baseStyle); + $mergedStyle = $this->styleManager->merge($currentStyle, $baseStyle); $this->assertNotSame($mergedStyle, $currentStyle); } @@ -42,7 +59,7 @@ class StyleTest extends \PHPUnit_Framework_TestCase { $baseStyle = (new StyleBuilder())->setFontSize(99)->setFontBold()->build(); $currentStyle = (new StyleBuilder())->setFontName('Font')->setFontUnderline()->build(); - $mergedStyle = $currentStyle->mergeWith($baseStyle); + $mergedStyle = $this->styleManager->merge($currentStyle, $baseStyle); $this->assertNotEquals(99, $currentStyle->getFontSize()); $this->assertFalse($currentStyle->isFontBold()); @@ -60,7 +77,7 @@ class StyleTest extends \PHPUnit_Framework_TestCase { $baseStyle = (new StyleBuilder())->setFontSize(10)->build(); $currentStyle = (new StyleBuilder())->setFontSize(99)->build(); - $mergedStyle = $currentStyle->mergeWith($baseStyle); + $mergedStyle = $this->styleManager->merge($currentStyle, $baseStyle); $this->assertEquals(99, $mergedStyle->getFontSize()); } @@ -72,7 +89,7 @@ class StyleTest extends \PHPUnit_Framework_TestCase { $baseStyle = (new StyleBuilder())->build(); $currentStyle = (new StyleBuilder())->setFontItalic()->setFontStrikethrough()->build(); - $mergedStyle = $currentStyle->mergeWith($baseStyle); + $mergedStyle = $this->styleManager->merge($currentStyle, $baseStyle); $this->assertFalse($baseStyle->isFontItalic()); $this->assertFalse($baseStyle->isFontStrikethrough()); @@ -93,7 +110,7 @@ class StyleTest extends \PHPUnit_Framework_TestCase ->setShouldWrapText() ->build(); $currentStyle = (new StyleBuilder())->build(); - $mergedStyle = $currentStyle->mergeWith($baseStyle); + $mergedStyle = $this->styleManager->merge($currentStyle, $baseStyle); $this->assertFalse($currentStyle->isFontUnderline()); $this->assertTrue($mergedStyle->isFontUnderline()); @@ -109,10 +126,10 @@ class StyleTest extends \PHPUnit_Framework_TestCase { $baseStyle = (new StyleBuilder())->build(); $currentStyle = (new StyleBuilder())->build(); - $mergedStyle = $currentStyle->mergeWith($baseStyle); + $mergedStyle = $this->styleManager->merge($currentStyle, $baseStyle); - $this->assertTrue($baseStyle->serialize() === $currentStyle->serialize()); - $this->assertTrue($currentStyle->serialize() === $mergedStyle->serialize()); + $this->assertTrue($this->styleManager->serialize($baseStyle) === $this->styleManager->serialize($currentStyle)); + $this->assertTrue($this->styleManager->serialize($currentStyle) === $this->styleManager->serialize($mergedStyle)); } /** @@ -125,9 +142,9 @@ class StyleTest extends \PHPUnit_Framework_TestCase ->setFontSize(Style::DEFAULT_FONT_SIZE) ->build(); $currentStyle = (new StyleBuilder())->build(); - $mergedStyle = $currentStyle->mergeWith($baseStyle); + $mergedStyle = $this->styleManager->merge($currentStyle, $baseStyle); - $this->assertTrue($currentStyle->serialize() === $mergedStyle->serialize()); + $this->assertTrue($this->styleManager->serialize($currentStyle) === $this->styleManager->serialize($mergedStyle)); } /** @@ -151,10 +168,10 @@ class StyleTest extends \PHPUnit_Framework_TestCase $baseStyle = (new StyleBuilder())->setBorder($border)->build(); $currentStyle = (new StyleBuilder())->build(); - $mergedStyle = $currentStyle->mergeWith($baseStyle); + $mergedStyle = $this->styleManager->merge($currentStyle, $baseStyle); $this->assertEquals(null, $currentStyle->getBorder(), 'Current style has no border'); - $this->assertInstanceOf('Box\Spout\Writer\Style\Border', $baseStyle->getBorder(), 'Base style has a border'); - $this->assertInstanceOf('Box\Spout\Writer\Style\Border', $mergedStyle->getBorder(), 'Merged style has a border'); + $this->assertInstanceOf('Box\Spout\Writer\Common\Entity\Style\Border', $baseStyle->getBorder(), 'Base style has a border'); + $this->assertInstanceOf('Box\Spout\Writer\Common\Entity\Style\Border', $mergedStyle->getBorder(), 'Merged style has a border'); } } diff --git a/tests/Spout/Writer/ODS/Helper/StyleHelperTest.php b/tests/Spout/Writer/ODS/Helper/StyleHelperTest.php index 763f904..4fc68ae 100644 --- a/tests/Spout/Writer/ODS/Helper/StyleHelperTest.php +++ b/tests/Spout/Writer/ODS/Helper/StyleHelperTest.php @@ -2,7 +2,9 @@ namespace Box\Spout\Writer\ODS\Helper; -use Box\Spout\Writer\Style\StyleBuilder; +use Box\Spout\Writer\Common\Creator\Style\StyleBuilder; +use Box\Spout\Writer\Common\Entity\Style\Style; +use Box\Spout\Writer\Common\Manager\StyleManager; /** * Class StyleHelperTest @@ -11,15 +13,19 @@ use Box\Spout\Writer\Style\StyleBuilder; */ class StyleHelperTest extends \PHPUnit_Framework_TestCase { - /** @var \Box\Spout\Writer\Style\Style */ + /** @var Style */ protected $defaultStyle; + /** @var StyleHelper */ + private $styleHelper; + /** * @return void */ public function setUp() { $this->defaultStyle = (new StyleBuilder())->build(); + $this->styleHelper = new StyleHelper($this->defaultStyle, new StyleManager()); } /** @@ -34,9 +40,8 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase $this->assertNull($style1->getId()); $this->assertNull($style2->getId()); - $styleHelper = new StyleHelper($this->defaultStyle); - $registeredStyle1 = $styleHelper->registerStyle($style1); - $registeredStyle2 = $styleHelper->registerStyle($style2); + $registeredStyle1 = $this->styleHelper->registerStyle($style1); + $registeredStyle2 = $this->styleHelper->registerStyle($style2); $this->assertEquals(1, $registeredStyle1->getId()); $this->assertEquals(2, $registeredStyle2->getId()); @@ -49,9 +54,8 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase { $style = (new StyleBuilder())->setFontBold()->build(); - $styleHelper = new StyleHelper($this->defaultStyle); - $registeredStyle1 = $styleHelper->registerStyle($style); - $registeredStyle2 = $styleHelper->registerStyle($style); + $registeredStyle1 = $this->styleHelper->registerStyle($style); + $registeredStyle2 = $this->styleHelper->registerStyle($style); $this->assertEquals(1, $registeredStyle1->getId()); $this->assertEquals(1, $registeredStyle2->getId()); @@ -63,11 +67,10 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase public function testApplyExtraStylesIfNeededShouldApplyWrapTextIfCellContainsNewLine() { $style = clone $this->defaultStyle; - $styleHelper = new StyleHelper($this->defaultStyle); $this->assertFalse($style->shouldWrapText()); - $updatedStyle = $styleHelper->applyExtraStylesIfNeeded($style, [12, 'single line', "multi\nlines", null]); + $updatedStyle = $this->styleHelper->applyExtraStylesIfNeeded($style, [12, 'single line', "multi\nlines", null]); $this->assertTrue($updatedStyle->shouldWrapText()); } @@ -78,11 +81,10 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase public function testApplyExtraStylesIfNeededShouldDoNothingIfWrapTextAlreadyApplied() { $style = (new StyleBuilder())->setShouldWrapText()->build(); - $styleHelper = new StyleHelper($this->defaultStyle); $this->assertTrue($style->shouldWrapText()); - $updatedStyle = $styleHelper->applyExtraStylesIfNeeded($style, ["multi\nlines"]); + $updatedStyle = $this->styleHelper->applyExtraStylesIfNeeded($style, ["multi\nlines"]); $this->assertTrue($updatedStyle->shouldWrapText()); } diff --git a/tests/Spout/Writer/ODS/WriterWithStyleTest.php b/tests/Spout/Writer/ODS/WriterWithStyleTest.php index b472055..a2eaf0c 100644 --- a/tests/Spout/Writer/ODS/WriterWithStyleTest.php +++ b/tests/Spout/Writer/ODS/WriterWithStyleTest.php @@ -6,11 +6,11 @@ use Box\Spout\Common\Type; use Box\Spout\Reader\Wrapper\XMLReader; use Box\Spout\TestUsingResource; use Box\Spout\Writer\ODS\Helper\BorderHelper; -use Box\Spout\Writer\Style\Border; -use Box\Spout\Writer\Style\BorderBuilder; -use Box\Spout\Writer\Style\Color; -use Box\Spout\Writer\Style\Style; -use Box\Spout\Writer\Style\StyleBuilder; +use Box\Spout\Writer\Common\Entity\Style\Border; +use Box\Spout\Writer\Common\Creator\Style\BorderBuilder; +use Box\Spout\Writer\Common\Entity\Style\Color; +use Box\Spout\Writer\Common\Entity\Style\Style; +use Box\Spout\Writer\Common\Creator\Style\StyleBuilder; use Box\Spout\Writer\WriterFactory; /** @@ -22,7 +22,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase { use TestUsingResource; - /** @var \Box\Spout\Writer\Style\Style */ + /** @var \Box\Spout\Writer\Common\Entity\Style\Style */ protected $defaultStyle; /** @@ -67,7 +67,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase * @dataProvider dataProviderForInvalidStyle * @expectedException \Box\Spout\Common\Exception\InvalidArgumentException * - * @param \Box\Spout\Writer\Style\Style $style + * @param \Box\Spout\Writer\Common\Entity\Style\Style $style */ public function testAddRowWithStyleShouldThrowExceptionIfInvalidStyleGiven($style) { @@ -84,7 +84,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase * @dataProvider dataProviderForInvalidStyle * @expectedException \Box\Spout\Common\Exception\InvalidArgumentException * - * @param \Box\Spout\Writer\Style\Style $style + * @param \Box\Spout\Writer\Common\Entity\Style\Style $style */ public function testAddRowsWithStyleShouldThrowExceptionIfInvalidStyleGiven($style) { @@ -346,7 +346,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase /** * @param array $allRows * @param string $fileName - * @param \Box\Spout\Writer\Style\Style $style + * @param \Box\Spout\Writer\Common\Entity\Style\Style $style * @return Writer */ private function writeToODSFile($allRows, $fileName, $style) @@ -367,7 +367,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase /** * @param array $allRows * @param string $fileName - * @param \Box\Spout\Writer\Style\Style|null $defaultStyle + * @param \Box\Spout\Writer\Common\Entity\Style\Style|null $defaultStyle * @return Writer */ private function writeToODSFileWithDefaultStyle($allRows, $fileName, $defaultStyle) @@ -389,7 +389,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase /** * @param array $allRows * @param string $fileName - * @param \Box\Spout\Writer\Style\Style|null[] $styles + * @param \Box\Spout\Writer\Common\Entity\Style\Style|null[] $styles * @return Writer */ private function writeToODSFileWithMultipleStyles($allRows, $fileName, $styles) diff --git a/tests/Spout/Writer/XLSX/Helper/StyleHelperTest.php b/tests/Spout/Writer/XLSX/Helper/StyleHelperTest.php index bcf9344..3cfc7b2 100644 --- a/tests/Spout/Writer/XLSX/Helper/StyleHelperTest.php +++ b/tests/Spout/Writer/XLSX/Helper/StyleHelperTest.php @@ -2,10 +2,12 @@ namespace Box\Spout\Writer\XLSX\Helper; -use Box\Spout\Writer\Style\Border; -use Box\Spout\Writer\Style\BorderBuilder; -use Box\Spout\Writer\Style\Color; -use Box\Spout\Writer\Style\StyleBuilder; +use Box\Spout\Writer\Common\Entity\Style\Border; +use Box\Spout\Writer\Common\Creator\Style\BorderBuilder; +use Box\Spout\Writer\Common\Entity\Style\Color; +use Box\Spout\Writer\Common\Creator\Style\StyleBuilder; +use Box\Spout\Writer\Common\Entity\Style\Style; +use Box\Spout\Writer\Common\Manager\StyleManager; /** * Class StyleHelperTest @@ -14,15 +16,19 @@ use Box\Spout\Writer\Style\StyleBuilder; */ class StyleHelperTest extends \PHPUnit_Framework_TestCase { - /** @var \Box\Spout\Writer\Style\Style */ + /** @var Style */ protected $defaultStyle; + /** @var StyleHelper */ + private $styleHelper; + /** * @return void */ public function setUp() { $this->defaultStyle = (new StyleBuilder())->build(); + $this->styleHelper = new StyleHelper($this->defaultStyle, new StyleManager()); } /** @@ -37,9 +43,8 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase $this->assertNull($style1->getId()); $this->assertNull($style2->getId()); - $styleHelper = new StyleHelper($this->defaultStyle); - $registeredStyle1 = $styleHelper->registerStyle($style1); - $registeredStyle2 = $styleHelper->registerStyle($style2); + $registeredStyle1 = $this->styleHelper->registerStyle($style1); + $registeredStyle2 = $this->styleHelper->registerStyle($style2); $this->assertEquals(1, $registeredStyle1->getId()); $this->assertEquals(2, $registeredStyle2->getId()); @@ -52,9 +57,8 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase { $style = (new StyleBuilder())->setFontBold()->build(); - $styleHelper = new StyleHelper($this->defaultStyle); - $registeredStyle1 = $styleHelper->registerStyle($style); - $registeredStyle2 = $styleHelper->registerStyle($style); + $registeredStyle1 = $this->styleHelper->registerStyle($style); + $registeredStyle2 = $this->styleHelper->registerStyle($style); $this->assertEquals(1, $registeredStyle1->getId()); $this->assertEquals(1, $registeredStyle2->getId()); @@ -71,14 +75,13 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase $border = (new BorderBuilder())->setBorderBottom(Color::GREEN)->build(); $styleWithBorder = (new StyleBuilder())->setBorder($border)->build(); - $styleHelper = new StyleHelper($this->defaultStyle); - $styleHelper->registerStyle($styleWithFont); - $styleHelper->registerStyle($styleWithBackground); - $styleHelper->registerStyle($styleWithBorder); + $this->styleHelper->registerStyle($styleWithFont); + $this->styleHelper->registerStyle($styleWithBackground); + $this->styleHelper->registerStyle($styleWithBorder); - $this->assertFalse($styleHelper->shouldApplyStyleOnEmptyCell($styleWithFont->getId())); - $this->assertTrue($styleHelper->shouldApplyStyleOnEmptyCell($styleWithBackground->getId())); - $this->assertTrue($styleHelper->shouldApplyStyleOnEmptyCell($styleWithBorder->getId())); + $this->assertFalse($this->styleHelper->shouldApplyStyleOnEmptyCell($styleWithFont->getId())); + $this->assertTrue($this->styleHelper->shouldApplyStyleOnEmptyCell($styleWithBackground->getId())); + $this->assertTrue($this->styleHelper->shouldApplyStyleOnEmptyCell($styleWithBorder->getId())); } /** @@ -87,11 +90,10 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase public function testApplyExtraStylesIfNeededShouldApplyWrapTextIfCellContainsNewLine() { $style = clone $this->defaultStyle; - $styleHelper = new StyleHelper($this->defaultStyle); $this->assertFalse($style->shouldWrapText()); - $updatedStyle = $styleHelper->applyExtraStylesIfNeeded($style, [12, 'single line', "multi\nlines", null]); + $updatedStyle = $this->styleHelper->applyExtraStylesIfNeeded($style, [12, 'single line', "multi\nlines", null]); $this->assertTrue($updatedStyle->shouldWrapText()); } @@ -102,11 +104,10 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase public function testApplyExtraStylesIfNeededShouldDoNothingIfWrapTextAlreadyApplied() { $style = (new StyleBuilder())->setShouldWrapText()->build(); - $styleHelper = new StyleHelper($this->defaultStyle); $this->assertTrue($style->shouldWrapText()); - $updatedStyle = $styleHelper->applyExtraStylesIfNeeded($style, ["multi\nlines"]); + $updatedStyle = $this->styleHelper->applyExtraStylesIfNeeded($style, ["multi\nlines"]); $this->assertTrue($updatedStyle->shouldWrapText()); } diff --git a/tests/Spout/Writer/XLSX/WriterWithStyleTest.php b/tests/Spout/Writer/XLSX/WriterWithStyleTest.php index fd66796..2563d69 100644 --- a/tests/Spout/Writer/XLSX/WriterWithStyleTest.php +++ b/tests/Spout/Writer/XLSX/WriterWithStyleTest.php @@ -5,11 +5,12 @@ namespace Box\Spout\Writer\XLSX; use Box\Spout\Common\Type; use Box\Spout\Reader\Wrapper\XMLReader; use Box\Spout\TestUsingResource; -use Box\Spout\Writer\Style\Border; -use Box\Spout\Writer\Style\BorderBuilder; -use Box\Spout\Writer\Style\Color; -use Box\Spout\Writer\Style\Style; -use Box\Spout\Writer\Style\StyleBuilder; +use Box\Spout\Writer\Common\Entity\Style\Border; +use Box\Spout\Writer\Common\Creator\Style\BorderBuilder; +use Box\Spout\Writer\Common\Entity\Style\Color; +use Box\Spout\Writer\Common\Entity\Style\Style; +use Box\Spout\Writer\Common\Creator\Style\StyleBuilder; +use Box\Spout\Writer\Common\Manager\StyleManager; use Box\Spout\Writer\WriterFactory; use Box\Spout\Writer\XLSX\Manager\OptionsManager; @@ -22,7 +23,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase { use TestUsingResource; - /** @var \Box\Spout\Writer\Style\Style */ + /** @var \Box\Spout\Writer\Common\Entity\Style\Style */ protected $defaultStyle; /** @@ -67,7 +68,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase * @dataProvider dataProviderForInvalidStyle * @expectedException \Box\Spout\Common\Exception\InvalidArgumentException * - * @param \Box\Spout\Writer\Style\Style $style + * @param \Box\Spout\Writer\Common\Entity\Style\Style $style */ public function testAddRowWithStyleShouldThrowExceptionIfInvalidStyleGiven($style) { @@ -84,7 +85,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase * @dataProvider dataProviderForInvalidStyle * @expectedException \Box\Spout\Common\Exception\InvalidArgumentException * - * @param \Box\Spout\Writer\Style\Style $style + * @param \Box\Spout\Writer\Common\Entity\Style\Style $style */ public function testAddRowsWithStyleShouldThrowExceptionIfInvalidStyleGiven($style) { @@ -462,7 +463,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase $fontStyle = (new StyleBuilder())->setFontBold()->build(); $emptyStyle = (new StyleBuilder())->build(); - $borderRightFontBoldStyle = $borderRightStyle->mergeWith($fontStyle); + $borderRightFontBoldStyle = (new StyleManager())->merge($borderRightStyle, $fontStyle); $dataRows = [ ['Border-Left'], @@ -517,7 +518,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase /** * @param array $allRows * @param string $fileName - * @param \Box\Spout\Writer\Style\Style $style + * @param \Box\Spout\Writer\Common\Entity\Style\Style $style * @return Writer */ private function writeToXLSXFile($allRows, $fileName, $style) @@ -539,7 +540,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase /** * @param array $allRows * @param string $fileName - * @param \Box\Spout\Writer\Style\Style|null $defaultStyle + * @param \Box\Spout\Writer\Common\Entity\Style\Style|null $defaultStyle * @return Writer */ private function writeToXLSXFileWithDefaultStyle($allRows, $fileName, $defaultStyle) @@ -562,7 +563,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase /** * @param array $allRows * @param string $fileName - * @param \Box\Spout\Writer\Style\Style|null[] $styles + * @param \Box\Spout\Writer\Common\Entity\Style\Style|null[] $styles * @return Writer */ private function writeToXLSXFileWithMultipleStyles($allRows, $fileName, $styles)