Move Style classes into Common folder

This commit is contained in:
Adrien Loison 2017-05-30 00:47:14 +02:00
parent c4e25a168e
commit d9d31ef701
38 changed files with 463 additions and 298 deletions

View File

@ -137,7 +137,7 @@ It is possible to apply some formatting options to a row. Spout supports fonts,
```php ```php
use Box\Spout\Common\Type; use Box\Spout\Common\Type;
use Box\Spout\Writer\WriterFactory; 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; use Box\Spout\Writer\Style\Color;
$style = (new StyleBuilder()) $style = (new StyleBuilder())
@ -163,9 +163,9 @@ Adding borders to a row requires a ```Border``` object.
```php ```php
use Box\Spout\Common\Type; use Box\Spout\Common\Type;
use Box\Spout\Writer\Style\Border; 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\Color;
use Box\Spout\Writer\Style\StyleBuilder; use Box\Spout\Writer\Common\Creator\Style\StyleBuilder;
use Box\Spout\Writer\WriterFactory; use Box\Spout\Writer\WriterFactory;
$border = (new BorderBuilder()) $border = (new BorderBuilder())

View File

@ -81,7 +81,7 @@ class Writer extends WriterAbstract
* *
* @param array $dataRow Array containing data to be written. * @param array $dataRow Array containing data to be written.
* Example $dataRow = ['data1', 1234, null, '', 'data5']; * 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 * @return void
* @throws \Box\Spout\Common\Exception\IOException If unable to write data * @throws \Box\Spout\Common\Exception\IOException If unable to write data
*/ */

View File

@ -1,9 +1,15 @@
<?php <?php
namespace Box\Spout\Writer\Style; namespace Box\Spout\Writer\Common\Creator\Style;
use Box\Spout\Writer\Common\Entity\Style\Border;
use Box\Spout\Writer\Common\Entity\Style\BorderPart;
use Box\Spout\Writer\Common\Entity\Style\Color;
/** /**
* Class BorderBuilder * Class BorderBuilder
*
* @package \Box\Spout\Writer\Common\Creator\Style
*/ */
class BorderBuilder class BorderBuilder
{ {

View File

@ -1,12 +1,15 @@
<?php <?php
namespace Box\Spout\Writer\Style; namespace Box\Spout\Writer\Common\Creator\Style;
use Box\Spout\Writer\Common\Entity\Style\Border;
use Box\Spout\Writer\Common\Entity\Style\Style;
/** /**
* Class StyleBuilder * Class StyleBuilder
* Builder to create new styles * Builder to create new styles
* *
* @package Box\Spout\Writer\Style * @package Box\Spout\Writer\Common\Creator\Style
*/ */
class StyleBuilder class StyleBuilder
{ {

View File

@ -1,9 +1,11 @@
<?php <?php
namespace Box\Spout\Writer\Style; namespace Box\Spout\Writer\Common\Entity\Style;
/** /**
* Class Border * Class Border
*
* @package \Box\Spout\Writer\Common\Entity\Style
*/ */
class Border class Border
{ {
@ -22,10 +24,8 @@ class Border
const WIDTH_MEDIUM = 'medium'; const WIDTH_MEDIUM = 'medium';
const WIDTH_THICK = 'thick'; const WIDTH_THICK = 'thick';
/** /** @var array A list of BorderPart objects for this border. */
* @var array A list of BorderPart objects for this border. private $parts = [];
*/
protected $parts = [];
/** /**
* @param array|void $borderParts * @param array|void $borderParts
@ -36,7 +36,7 @@ class Border
} }
/** /**
* @param $name The name of the border part * @param string $name The name of the border part
* @return null|BorderPart * @return null|BorderPart
*/ */
public function getPart($name) public function getPart($name)
@ -45,7 +45,7 @@ class Border
} }
/** /**
* @param $name The name of the border part * @param string $name The name of the border part
* @return bool * @return bool
*/ */
public function hasPart($name) public function hasPart($name)
@ -64,6 +64,7 @@ class Border
/** /**
* Set BorderParts * Set BorderParts
* @param array $parts * @param array $parts
* @return void
*/ */
public function setParts($parts) public function setParts($parts)
{ {
@ -75,7 +76,7 @@ class Border
/** /**
* @param BorderPart $borderPart * @param BorderPart $borderPart
* @return self * @return Border
*/ */
public function addPart(BorderPart $borderPart) public function addPart(BorderPart $borderPart)
{ {

View File

@ -1,6 +1,6 @@
<?php <?php
namespace Box\Spout\Writer\Style; namespace Box\Spout\Writer\Common\Entity\Style;
use Box\Spout\Writer\Exception\Border\InvalidNameException; use Box\Spout\Writer\Exception\Border\InvalidNameException;
use Box\Spout\Writer\Exception\Border\InvalidStyleException; use Box\Spout\Writer\Exception\Border\InvalidStyleException;
@ -8,6 +8,8 @@ use Box\Spout\Writer\Exception\Border\InvalidWidthException;
/** /**
* Class BorderPart * Class BorderPart
*
* @package \Box\Spout\Writer\Common\Entity\Style
*/ */
class BorderPart class BorderPart
{ {

View File

@ -1,6 +1,6 @@
<?php <?php
namespace Box\Spout\Writer\Style; namespace Box\Spout\Writer\Common\Entity\Style;
use Box\Spout\Writer\Exception\InvalidColorException; use Box\Spout\Writer\Exception\InvalidColorException;
@ -8,7 +8,7 @@ use Box\Spout\Writer\Exception\InvalidColorException;
* Class Color * Class Color
* This class provides constants and functions to work with colors * This class provides constants and functions to work with colors
* *
* @package Box\Spout\Writer\Style * @package Box\Spout\Writer\Common\Entity\Style
*/ */
class Color class Color
{ {

View File

@ -1,12 +1,12 @@
<?php <?php
namespace Box\Spout\Writer\Style; namespace Box\Spout\Writer\Common\Entity\Style;
/** /**
* Class Style * Class Style
* Represents a style to be applied to a cell * Represents a style to be applied to a cell
* *
* @package Box\Spout\Writer\Style * @package Box\Spout\Writer\Common\Entity\Style
*/ */
class Style class Style
{ {
@ -16,62 +16,62 @@ class Style
const DEFAULT_FONT_NAME = 'Arial'; const DEFAULT_FONT_NAME = 'Arial';
/** @var int|null Style ID */ /** @var int|null Style ID */
protected $id = null; private $id = null;
/** @var bool Whether the font should be bold */ /** @var bool Whether the font should be bold */
protected $fontBold = false; private $fontBold = false;
/** @var bool Whether the bold property was set */ /** @var bool Whether the bold property was set */
protected $hasSetFontBold = false; private $hasSetFontBold = false;
/** @var bool Whether the font should be italic */ /** @var bool Whether the font should be italic */
protected $fontItalic = false; private $fontItalic = false;
/** @var bool Whether the italic property was set */ /** @var bool Whether the italic property was set */
protected $hasSetFontItalic = false; private $hasSetFontItalic = false;
/** @var bool Whether the font should be underlined */ /** @var bool Whether the font should be underlined */
protected $fontUnderline = false; private $fontUnderline = false;
/** @var bool Whether the underline property was set */ /** @var bool Whether the underline property was set */
protected $hasSetFontUnderline = false; private $hasSetFontUnderline = false;
/** @var bool Whether the font should be struck through */ /** @var bool Whether the font should be struck through */
protected $fontStrikethrough = false; private $fontStrikethrough = false;
/** @var bool Whether the strikethrough property was set */ /** @var bool Whether the strikethrough property was set */
protected $hasSetFontStrikethrough = false; private $hasSetFontStrikethrough = false;
/** @var int Font size */ /** @var int Font size */
protected $fontSize = self::DEFAULT_FONT_SIZE; private $fontSize = self::DEFAULT_FONT_SIZE;
/** @var bool Whether the font size property was set */ /** @var bool Whether the font size property was set */
protected $hasSetFontSize = false; private $hasSetFontSize = false;
/** @var string Font color */ /** @var string Font color */
protected $fontColor = self::DEFAULT_FONT_COLOR; private $fontColor = self::DEFAULT_FONT_COLOR;
/** @var bool Whether the font color property was set */ /** @var bool Whether the font color property was set */
protected $hasSetFontColor = false; private $hasSetFontColor = false;
/** @var string Font name */ /** @var string Font name */
protected $fontName = self::DEFAULT_FONT_NAME; private $fontName = self::DEFAULT_FONT_NAME;
/** @var bool Whether the font name property was set */ /** @var bool Whether the font name property was set */
protected $hasSetFontName = false; private $hasSetFontName = false;
/** @var bool Whether specific font properties should be applied */ /** @var bool Whether specific font properties should be applied */
protected $shouldApplyFont = false; private $shouldApplyFont = false;
/** @var bool Whether the text should wrap in the cell (useful for long or multi-lines text) */ /** @var bool Whether the text should wrap in the cell (useful for long or multi-lines text) */
protected $shouldWrapText = false; private $shouldWrapText = false;
/** @var bool Whether the wrap text property was set */ /** @var bool Whether the wrap text property was set */
protected $hasSetWrapText = false; private $hasSetWrapText = false;
/** @var Border */ /** @var Border */
protected $border = null; private $border = null;
/** @var bool Whether border properties should be applied */ /** @var bool Whether border properties should be applied */
protected $shouldApplyBorder = false; private $shouldApplyBorder = false;
/** @var string Background color */ /** @var string Background color */
protected $backgroundColor = null; private $backgroundColor = null;
/** @var bool */ /** @var bool */
protected $hasSetBackgroundColor = false; private $hasSetBackgroundColor = false;
/** /**
@ -138,6 +138,14 @@ class Style
return $this; return $this;
} }
/**
* @return bool
*/
public function hasSetFontBold()
{
return $this->hasSetFontBold;
}
/** /**
* @return bool * @return bool
*/ */
@ -157,6 +165,14 @@ class Style
return $this; return $this;
} }
/**
* @return bool
*/
public function hasSetFontItalic()
{
return $this->hasSetFontItalic;
}
/** /**
* @return bool * @return bool
*/ */
@ -176,6 +192,14 @@ class Style
return $this; return $this;
} }
/**
* @return bool
*/
public function hasSetFontUnderline()
{
return $this->hasSetFontUnderline;
}
/** /**
* @return bool * @return bool
*/ */
@ -195,6 +219,14 @@ class Style
return $this; return $this;
} }
/**
* @return bool
*/
public function hasSetFontStrikethrough()
{
return $this->hasSetFontStrikethrough;
}
/** /**
* @return int * @return int
*/ */
@ -215,6 +247,14 @@ class Style
return $this; return $this;
} }
/**
* @return bool
*/
public function hasSetFontSize()
{
return $this->hasSetFontSize;
}
/** /**
* @return string * @return string
*/ */
@ -237,6 +277,14 @@ class Style
return $this; return $this;
} }
/**
* @return bool
*/
public function hasSetFontColor()
{
return $this->hasSetFontColor;
}
/** /**
* @return string * @return string
*/ */
@ -257,6 +305,14 @@ class Style
return $this; return $this;
} }
/**
* @return bool
*/
public function hasSetFontName()
{
return $this->hasSetFontName;
}
/** /**
* @return bool * @return bool
*/ */
@ -320,103 +376,4 @@ class Style
{ {
return $this->hasSetBackgroundColor; 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());
}
}
} }

View File

@ -2,6 +2,9 @@
namespace Box\Spout\Writer\Common\Helper; namespace Box\Spout\Writer\Common\Helper;
use Box\Spout\Writer\Common\Entity\Style\Style;
use Box\Spout\Writer\Common\Manager\StyleManager;
/** /**
* Class StyleHelperAbstract * Class StyleHelperAbstract
* This class provides helper functions to manage styles * This class provides helper functions to manage styles
@ -10,6 +13,9 @@ namespace Box\Spout\Writer\Common\Helper;
*/ */
abstract class StyleHelperAbstract implements StyleHelperInterface 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 */ /** @var array [SERIALIZED_STYLE] => [STYLE_ID] mapping table, keeping track of the registered styles */
protected $serializedStyleToStyleIdMappingTable = []; protected $serializedStyleToStyleIdMappingTable = [];
@ -17,10 +23,13 @@ abstract class StyleHelperAbstract implements StyleHelperInterface
protected $styleIdToStyleMappingTable = []; 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 ensures that the default style is the first one to be registered
$this->registerStyle($defaultStyle); $this->registerStyle($defaultStyle);
} }
@ -29,12 +38,12 @@ abstract class StyleHelperAbstract implements StyleHelperInterface
* Registers the given style as a used style. * Registers the given style as a used style.
* Duplicate styles won't be registered more than once. * Duplicate styles won't be registered more than once.
* *
* @param \Box\Spout\Writer\Style\Style $style The style to be registered * @param Style $style The style to be registered
* @return \Box\Spout\Writer\Style\Style The registered style, updated with an internal ID. * @return Style The registered style, updated with an internal ID.
*/ */
public function registerStyle($style) public function registerStyle($style)
{ {
$serializedStyle = $style->serialize(); $serializedStyle = $this->styleManager->serialize($style);
if (!$this->hasStyleAlreadyBeenRegistered($style)) { if (!$this->hasStyleAlreadyBeenRegistered($style)) {
$nextStyleId = count($this->serializedStyleToStyleIdMappingTable); $nextStyleId = count($this->serializedStyleToStyleIdMappingTable);
@ -50,12 +59,12 @@ abstract class StyleHelperAbstract implements StyleHelperInterface
/** /**
* Returns whether the given style has already been registered. * Returns whether the given style has already been registered.
* *
* @param \Box\Spout\Writer\Style\Style $style * @param Style $style
* @return bool * @return bool
*/ */
protected function hasStyleAlreadyBeenRegistered($style) protected function hasStyleAlreadyBeenRegistered($style)
{ {
$serializedStyle = $style->serialize(); $serializedStyle = $this->styleManager->serialize($style);
// Using isset here because it is way faster than array_key_exists... // Using isset here because it is way faster than array_key_exists...
return isset($this->serializedStyleToStyleIdMappingTable[$serializedStyle]); return isset($this->serializedStyleToStyleIdMappingTable[$serializedStyle]);
@ -65,7 +74,7 @@ abstract class StyleHelperAbstract implements StyleHelperInterface
* Returns the registered style associated to the given serialization. * Returns the registered style associated to the given serialization.
* *
* @param string $serializedStyle The serialized style from which the actual style should be fetched from * @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) 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() protected function getRegisteredStyles()
{ {
@ -84,7 +93,7 @@ abstract class StyleHelperAbstract implements StyleHelperInterface
/** /**
* Returns the default style * Returns the default style
* *
* @return \Box\Spout\Writer\Style\Style Default style * @return Style Default style
*/ */
protected function getDefaultStyle() protected function getDefaultStyle()
{ {
@ -96,9 +105,9 @@ abstract class StyleHelperAbstract implements StyleHelperInterface
* Apply additional styles if the given row needs it. * Apply additional styles if the given row needs it.
* Typically, set "wrap text" if a cell contains a new line. * 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 * @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) 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 * A workaround would be to encode "\n" as "_x000D_" but it does not work
* on the Windows version of Excel... * 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 * @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) protected function applyWrapTextIfCellContainsNewLine($style, $dataRow)
{ {

View File

@ -13,8 +13,8 @@ interface StyleHelperInterface
* Registers the given style as a used style. * Registers the given style as a used style.
* Duplicate styles won't be registered more than once. * Duplicate styles won't be registered more than once.
* *
* @param \Box\Spout\Writer\Style\Style $style The style to be registered * @param \Box\Spout\Writer\Common\Entity\Style\Style $style The style to be registered
* @return \Box\Spout\Writer\Style\Style The registered style, updated with an internal ID. * @return \Box\Spout\Writer\Common\Entity\Style\Style The registered style, updated with an internal ID.
*/ */
public function registerStyle($style); public function registerStyle($style);
@ -22,9 +22,9 @@ interface StyleHelperInterface
* Apply additional styles if the given row needs it. * Apply additional styles if the given row needs it.
* Typically, set "wrap text" if a cell contains a new line. * 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 * @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); public function applyExtraStylesIfNeeded($style, $dataRow);
} }

View File

@ -0,0 +1,118 @@
<?php
namespace Box\Spout\Writer\Common\Manager;
use Box\Spout\Writer\Common\Entity\Style\Style;
/**
* Class StyleManager
* Manages styles to be applied to a cell
*
* @package Box\Spout\Writer\Common\Manager\Style
*/
class StyleManager
{
/**
* Serializes the style for future comparison with other styles.
* The ID is excluded from the comparison, as we only care about
* actual style properties.
*
* @param Style $style
* @return string The serialized style
*/
public function serialize(Style $style)
{
// In order to be able to properly compare style, set static ID value
$currentId = $style->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());
}
}
}

View File

@ -12,7 +12,7 @@ use Box\Spout\Writer\Common\Entity\Worksheet;
use Box\Spout\Writer\Exception\SheetNotFoundException; use Box\Spout\Writer\Exception\SheetNotFoundException;
use Box\Spout\Writer\Exception\WriterException; use Box\Spout\Writer\Exception\WriterException;
use Box\Spout\Writer\Common\Creator\EntityFactory; use Box\Spout\Writer\Common\Creator\EntityFactory;
use Box\Spout\Writer\Style\Style; use Box\Spout\Writer\Common\Entity\Style\Style;
/** /**
* Class WorkbookManagerAbstract * Class WorkbookManagerAbstract

View File

@ -8,7 +8,7 @@ use Box\Spout\Writer\Common\Entity\Workbook;
use Box\Spout\Writer\Common\Entity\Worksheet; use Box\Spout\Writer\Common\Entity\Worksheet;
use Box\Spout\Writer\Exception\SheetNotFoundException; use Box\Spout\Writer\Exception\SheetNotFoundException;
use Box\Spout\Writer\Exception\WriterException; use Box\Spout\Writer\Exception\WriterException;
use Box\Spout\Writer\Style\Style; use Box\Spout\Writer\Common\Entity\Style\Style;
/** /**
* Interface WorkbookManagerInterface * Interface WorkbookManagerInterface

View File

@ -3,7 +3,7 @@
namespace Box\Spout\Writer\Common\Manager; namespace Box\Spout\Writer\Common\Manager;
use Box\Spout\Writer\Common\Entity\Worksheet; use Box\Spout\Writer\Common\Entity\Worksheet;
use Box\Spout\Writer\Style\Style; use Box\Spout\Writer\Common\Entity\Style\Style;
/** /**
* Interface WorksheetManagerInterface * Interface WorksheetManagerInterface

View File

@ -3,7 +3,7 @@
namespace Box\Spout\Writer\Exception\Border; namespace Box\Spout\Writer\Exception\Border;
use Box\Spout\Writer\Exception\WriterException; use Box\Spout\Writer\Exception\WriterException;
use Box\Spout\Writer\Style\BorderPart; use Box\Spout\Writer\Common\Entity\Style\BorderPart;
class InvalidNameException extends WriterException class InvalidNameException extends WriterException
{ {

View File

@ -3,7 +3,7 @@
namespace Box\Spout\Writer\Exception\Border; namespace Box\Spout\Writer\Exception\Border;
use Box\Spout\Writer\Exception\WriterException; use Box\Spout\Writer\Exception\WriterException;
use Box\Spout\Writer\Style\BorderPart; use Box\Spout\Writer\Common\Entity\Style\BorderPart;
class InvalidStyleException extends WriterException class InvalidStyleException extends WriterException
{ {

View File

@ -3,7 +3,7 @@
namespace Box\Spout\Writer\Exception\Border; namespace Box\Spout\Writer\Exception\Border;
use Box\Spout\Writer\Exception\WriterException; use Box\Spout\Writer\Exception\WriterException;
use Box\Spout\Writer\Style\BorderPart; use Box\Spout\Writer\Common\Entity\Style\BorderPart;
class InvalidWidthException extends WriterException class InvalidWidthException extends WriterException
{ {

View File

@ -7,6 +7,7 @@ use Box\Spout\Writer\Common\Manager\OptionsManagerInterface;
use Box\Spout\Writer\Common\Entity\Options; use Box\Spout\Writer\Common\Entity\Options;
use Box\Spout\Writer\Common\Creator\EntityFactory; use Box\Spout\Writer\Common\Creator\EntityFactory;
use Box\Spout\Writer\Common\Creator\InternalFactoryInterface; 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\FileSystemHelper;
use Box\Spout\Writer\ODS\Helper\StyleHelper; use Box\Spout\Writer\ODS\Helper\StyleHelper;
use Box\Spout\Writer\ODS\Manager\WorkbookManager; use Box\Spout\Writer\ODS\Manager\WorkbookManager;
@ -80,7 +81,17 @@ class InternalFactory implements InternalFactoryInterface
private function createStyleHelper(OptionsManagerInterface $optionsManager) private function createStyleHelper(OptionsManagerInterface $optionsManager)
{ {
$defaultRowStyle = $optionsManager->getOption(Options::DEFAULT_ROW_STYLE); $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();
} }
/** /**

View File

@ -2,8 +2,8 @@
namespace Box\Spout\Writer\ODS\Helper; namespace Box\Spout\Writer\ODS\Helper;
use Box\Spout\Writer\Style\BorderPart; use Box\Spout\Writer\Common\Entity\Style\BorderPart;
use Box\Spout\Writer\Style\Border; use Box\Spout\Writer\Common\Entity\Style\Border;
/** /**
* Class BorderHelper * Class BorderHelper

View File

@ -3,7 +3,7 @@
namespace Box\Spout\Writer\ODS\Helper; namespace Box\Spout\Writer\ODS\Helper;
use Box\Spout\Writer\Common\Helper\StyleHelperAbstract; use Box\Spout\Writer\Common\Helper\StyleHelperAbstract;
use Box\Spout\Writer\Style\BorderPart; use Box\Spout\Writer\Common\Entity\Style\BorderPart;
/** /**
* Class StyleHelper * Class StyleHelper
@ -20,8 +20,8 @@ class StyleHelper extends StyleHelperAbstract
* Registers the given style as a used style. * Registers the given style as a used style.
* Duplicate styles won't be registered more than once. * Duplicate styles won't be registered more than once.
* *
* @param \Box\Spout\Writer\Style\Style $style The style to be registered * @param \Box\Spout\Writer\Common\Entity\Style\Style $style The style to be registered
* @return \Box\Spout\Writer\Style\Style The registered style, updated with an internal ID. * @return \Box\Spout\Writer\Common\Entity\Style\Style The registered style, updated with an internal ID.
*/ */
public function registerStyle($style) public function registerStyle($style)
{ {
@ -209,7 +209,7 @@ EOD;
/** /**
* Returns the contents of the "<style:style>" section, inside "<office:automatic-styles>" section * Returns the contents of the "<style:style>" section, inside "<office:automatic-styles>" section
* *
* @param \Box\Spout\Writer\Style\Style $style * @param \Box\Spout\Writer\Common\Entity\Style\Style $style
* @return string * @return string
*/ */
protected function getStyleSectionContent($style) protected function getStyleSectionContent($style)
@ -229,7 +229,7 @@ EOD;
/** /**
* Returns the contents of the "<style:text-properties>" section, inside "<style:style>" section * Returns the contents of the "<style:text-properties>" section, inside "<style:style>" section
* *
* @param \Box\Spout\Writer\Style\Style $style * @param \Box\Spout\Writer\Common\Entity\Style\Style $style
* @return string * @return string
*/ */
private function getTextPropertiesSectionContent($style) private function getTextPropertiesSectionContent($style)
@ -246,7 +246,7 @@ EOD;
/** /**
* Returns the contents of the "<style:text-properties>" section, inside "<style:style>" section * Returns the contents of the "<style:text-properties>" section, inside "<style:style>" section
* *
* @param \Box\Spout\Writer\Style\Style $style * @param \Box\Spout\Writer\Common\Entity\Style\Style $style
* @return string * @return string
*/ */
private function getFontSectionContent($style) private function getFontSectionContent($style)
@ -291,7 +291,7 @@ EOD;
/** /**
* Returns the contents of the "<style:table-cell-properties>" section, inside "<style:style>" section * Returns the contents of the "<style:table-cell-properties>" section, inside "<style:style>" section
* *
* @param \Box\Spout\Writer\Style\Style $style * @param \Box\Spout\Writer\Common\Entity\Style\Style $style
* @return string * @return string
*/ */
private function getTableCellPropertiesSectionContent($style) private function getTableCellPropertiesSectionContent($style)
@ -326,7 +326,7 @@ EOD;
/** /**
* Returns the contents of the borders definition for the "<style:table-cell-properties>" section * Returns the contents of the borders definition for the "<style:table-cell-properties>" section
* *
* @param \Box\Spout\Writer\Style\Style $style * @param \Box\Spout\Writer\Common\Entity\Style\Style $style
* @return string * @return string
*/ */
private function getBorderXMLContent($style) private function getBorderXMLContent($style)
@ -343,7 +343,7 @@ EOD;
/** /**
* Returns the contents of the background color definition for the "<style:table-cell-properties>" section * Returns the contents of the background color definition for the "<style:table-cell-properties>" section
* *
* @param \Box\Spout\Writer\Style\Style $style * @param \Box\Spout\Writer\Common\Entity\Style\Style $style
* @return string * @return string
*/ */
private function getBackgroundColorXMLContent($style) private function getBackgroundColorXMLContent($style)

View File

@ -4,7 +4,7 @@ namespace Box\Spout\Writer\ODS\Manager;
use Box\Spout\Writer\Common\Entity\Options; use Box\Spout\Writer\Common\Entity\Options;
use Box\Spout\Writer\Common\Manager\OptionsManagerAbstract; use Box\Spout\Writer\Common\Manager\OptionsManagerAbstract;
use Box\Spout\Writer\Style\StyleBuilder; use Box\Spout\Writer\Common\Creator\Style\StyleBuilder;
/** /**
* Class OptionsManager * Class OptionsManager

View File

@ -9,7 +9,7 @@ use Box\Spout\Writer\Common\Entity\Cell;
use Box\Spout\Writer\Common\Entity\Worksheet; use Box\Spout\Writer\Common\Entity\Worksheet;
use Box\Spout\Writer\Common\Manager\WorksheetManagerInterface; use Box\Spout\Writer\Common\Manager\WorksheetManagerInterface;
use Box\Spout\Writer\ODS\Helper\StyleHelper; use Box\Spout\Writer\ODS\Helper\StyleHelper;
use Box\Spout\Writer\Style\Style; use Box\Spout\Writer\Common\Entity\Style\Style;
/** /**
* Class WorksheetManager * Class WorksheetManager

View File

@ -6,8 +6,11 @@ use Box\Spout\Common\Exception\InvalidArgumentException;
use Box\Spout\Common\Exception\IOException; use Box\Spout\Common\Exception\IOException;
use Box\Spout\Common\Exception\SpoutException; use Box\Spout\Common\Exception\SpoutException;
use Box\Spout\Common\Helper\FileSystemHelper; 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\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\WriterAlreadyOpenedException;
use Box\Spout\Writer\Exception\WriterNotOpenedException; 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 */ /** @var bool Indicates whether the writer has been opened or not */
protected $isWriterOpened = false; 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; protected $globalFunctionsHelper;
/** @var \Box\Spout\Writer\Common\Manager\OptionsManagerInterface Writer options manager */ /** @var OptionsManagerInterface Writer options manager */
protected $optionsManager; 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; protected $rowStyle;
/** @var string Content-Type value for the header - to be defined by child class */ /** @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. * @param array $dataRow Array containing data to be streamed.
* Example $dataRow = ['data1', 1234, null, '', 'data5']; * 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 * @return void
*/ */
abstract protected function addRowToWriter(array $dataRow, $style); abstract protected function addRowToWriter(array $dataRow, $style);
@ -66,11 +72,13 @@ abstract class WriterAbstract implements WriterInterface
abstract protected function closeWriter(); 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->optionsManager = $optionsManager;
$this->styleManager = $styleManager;
$this->resetRowStyleToDefault(); $this->resetRowStyleToDefault();
} }
@ -79,7 +87,7 @@ abstract class WriterAbstract implements WriterInterface
* Overriding the default style instead of using "addRowWithStyle" improves performance by 20%. * Overriding the default style instead of using "addRowWithStyle" improves performance by 20%.
* @see https://github.com/box/spout/issues/272 * @see https://github.com/box/spout/issues/272
* *
* @param Style\Style $defaultStyle * @param Style $defaultStyle
* @return WriterAbstract * @return WriterAbstract
*/ */
public function setDefaultRowStyle($defaultStyle) public function setDefaultRowStyle($defaultStyle)
@ -233,7 +241,7 @@ abstract class WriterAbstract implements WriterInterface
* *
* @api * @api
* @param array $dataRow Array of array containing data to be streamed. * @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 * @return WriterAbstract
* @throws \Box\Spout\Common\Exception\InvalidArgumentException If the input param is not valid * @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 * @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) 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.'); 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 * @api
* @param array $dataRows Array of array containing data to be streamed. * @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 * @return WriterAbstract
* @throws \Box\Spout\Common\Exception\InvalidArgumentException If the input param is not valid * @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 * @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) 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.'); 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 * Sets the style to be applied to the next written rows
* until it is changed or reset. * until it is changed or reset.
* *
* @param Style\Style $style * @param Style $style
* @return void * @return void
*/ */
private function setRowStyle($style) private function setRowStyle($style)
{ {
// Merge given style with the default one to inherit custom properties // Merge given style with the default one to inherit custom properties
$defaultRowStyle = $this->optionsManager->getOption(Options::DEFAULT_ROW_STYLE); $defaultRowStyle = $this->optionsManager->getOption(Options::DEFAULT_ROW_STYLE);
$this->rowStyle = $style->mergeWith($defaultRowStyle); $this->rowStyle = $this->styleManager->merge($style, $defaultRowStyle);
} }
/** /**

View File

@ -6,7 +6,8 @@ use Box\Spout\Common\Exception\UnsupportedTypeException;
use Box\Spout\Common\Helper\GlobalFunctionsHelper; use Box\Spout\Common\Helper\GlobalFunctionsHelper;
use Box\Spout\Common\Type; use Box\Spout\Common\Type;
use Box\Spout\Writer\Common\Creator\EntityFactory; 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 * Class WriterFactory
@ -54,8 +55,9 @@ class WriterFactory
private static function getCSVWriter() private static function getCSVWriter()
{ {
$optionsManager = new CSV\Manager\OptionsManager(); $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(); $styleBuilder = new StyleBuilder();
$optionsManager = new XLSX\Manager\OptionsManager($styleBuilder); $optionsManager = new XLSX\Manager\OptionsManager($styleBuilder);
$styleManager = new StyleManager();
$generalFactory = new XLSX\Creator\InternalFactory(new EntityFactory()); $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(); $styleBuilder = new StyleBuilder();
$optionsManager = new ODS\Manager\OptionsManager($styleBuilder); $optionsManager = new ODS\Manager\OptionsManager($styleBuilder);
$styleManager = new StyleManager();
$generalFactory = new ODS\Creator\InternalFactory(new EntityFactory()); $generalFactory = new ODS\Creator\InternalFactory(new EntityFactory());
return new ODS\Writer($optionsManager, $generalFactory); return new ODS\Writer($optionsManager, $styleManager, $generalFactory);
} }
} }

View File

@ -2,6 +2,8 @@
namespace Box\Spout\Writer; namespace Box\Spout\Writer;
use Box\Spout\Writer\Common\Entity\Style\Style;
/** /**
* Interface WriterInterface * Interface WriterInterface
* *
@ -45,7 +47,7 @@ interface WriterInterface
* @see addRow * @see addRow
* *
* @param array $dataRow Array of array containing data to be streamed. * @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 * @return WriterInterface
* @throws \Box\Spout\Common\Exception\InvalidArgumentException If the input param is not valid * @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 * @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If this function is called before opening the writer
@ -73,7 +75,7 @@ interface WriterInterface
* @see addRows * @see addRows
* *
* @param array $dataRows Array of array containing data to be streamed. * @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 * @return WriterInterface
* @throws \Box\Spout\Common\Exception\InvalidArgumentException If the input param is not valid * @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 * @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If this function is called before opening the writer

View File

@ -5,6 +5,7 @@ namespace Box\Spout\Writer;
use Box\Spout\Writer\Common\Manager\OptionsManagerInterface; use Box\Spout\Writer\Common\Manager\OptionsManagerInterface;
use Box\Spout\Writer\Common\Entity\Options; use Box\Spout\Writer\Common\Entity\Options;
use Box\Spout\Writer\Common\Entity\Worksheet; use Box\Spout\Writer\Common\Entity\Worksheet;
use Box\Spout\Writer\Common\Manager\StyleManager;
use Box\Spout\Writer\Exception\WriterNotOpenedException; use Box\Spout\Writer\Exception\WriterNotOpenedException;
use Box\Spout\Writer\Common\Creator\InternalFactoryInterface; use Box\Spout\Writer\Common\Creator\InternalFactoryInterface;
use Box\Spout\Writer\Common\Manager\WorkbookManagerInterface; use Box\Spout\Writer\Common\Manager\WorkbookManagerInterface;
@ -26,11 +27,12 @@ abstract class WriterMultiSheetsAbstract extends WriterAbstract
/** /**
* @param OptionsManagerInterface $optionsManager * @param OptionsManagerInterface $optionsManager
* @param StyleManager $styleManager
* @param InternalFactoryInterface $internalFactory * @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; $this->internalFactory = $internalFactory;
} }
@ -151,7 +153,7 @@ abstract class WriterMultiSheetsAbstract extends WriterAbstract
* *
* @param array $dataRow Array containing data to be written. * @param array $dataRow Array containing data to be written.
* Example $dataRow = ['data1', 1234, null, '', 'data5']; * 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 * @return void
* @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If the book is not created yet * @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If the book is not created yet
* @throws \Box\Spout\Common\Exception\IOException If unable to write data * @throws \Box\Spout\Common\Exception\IOException If unable to write data

View File

@ -10,6 +10,7 @@ use Box\Spout\Writer\Common\Creator\EntityFactory;
use Box\Spout\Writer\Common\Creator\InternalFactoryInterface; use Box\Spout\Writer\Common\Creator\InternalFactoryInterface;
use Box\Spout\Writer\Common\Creator\WorkbookFactory; use Box\Spout\Writer\Common\Creator\WorkbookFactory;
use Box\Spout\Writer\Common\Creator\WorksheetFactory; 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\FileSystemHelper;
use Box\Spout\Writer\XLSX\Helper\SharedStringsHelper; use Box\Spout\Writer\XLSX\Helper\SharedStringsHelper;
use Box\Spout\Writer\XLSX\Helper\StyleHelper; use Box\Spout\Writer\XLSX\Helper\StyleHelper;
@ -102,7 +103,17 @@ class InternalFactory implements InternalFactoryInterface
private function createStyleHelper(OptionsManagerInterface $optionsManager) private function createStyleHelper(OptionsManagerInterface $optionsManager)
{ {
$defaultRowStyle = $optionsManager->getOption(Options::DEFAULT_ROW_STYLE); $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();
} }
/** /**

View File

@ -2,8 +2,8 @@
namespace Box\Spout\Writer\XLSX\Helper; namespace Box\Spout\Writer\XLSX\Helper;
use Box\Spout\Writer\Style\Border; use Box\Spout\Writer\Common\Entity\Style\Border;
use Box\Spout\Writer\Style\BorderPart; use Box\Spout\Writer\Common\Entity\Style\BorderPart;
class BorderHelper class BorderHelper
{ {

View File

@ -3,8 +3,8 @@
namespace Box\Spout\Writer\XLSX\Helper; namespace Box\Spout\Writer\XLSX\Helper;
use Box\Spout\Writer\Common\Helper\StyleHelperAbstract; use Box\Spout\Writer\Common\Helper\StyleHelperAbstract;
use Box\Spout\Writer\Style\Color; use Box\Spout\Writer\Common\Entity\Style\Color;
use Box\Spout\Writer\Style\Style; use Box\Spout\Writer\Common\Entity\Style\Style;
/** /**
* Class StyleHelper * Class StyleHelper
@ -45,8 +45,8 @@ class StyleHelper extends StyleHelperAbstract
/** /**
* XLSX specific operations on the registered styles * XLSX specific operations on the registered styles
* *
* @param \Box\Spout\Writer\Style\Style $style * @param \Box\Spout\Writer\Common\Entity\Style\Style $style
* @return \Box\Spout\Writer\Style\Style * @return \Box\Spout\Writer\Common\Entity\Style\Style
*/ */
public function registerStyle($style) public function registerStyle($style)
{ {
@ -59,7 +59,7 @@ class StyleHelper extends StyleHelperAbstract
/** /**
* Register a fill definition * Register a fill definition
* *
* @param \Box\Spout\Writer\Style\Style $style * @param \Box\Spout\Writer\Common\Entity\Style\Style $style
*/ */
protected function registerFill($style) protected function registerFill($style)
{ {
@ -92,7 +92,7 @@ class StyleHelper extends StyleHelperAbstract
/** /**
* Register a border definition * Register a border definition
* *
* @param \Box\Spout\Writer\Style\Style $style * @param \Box\Spout\Writer\Common\Entity\Style\Style $style
*/ */
protected function registerBorder($style) protected function registerBorder($style)
{ {
@ -174,7 +174,7 @@ EOD;
{ {
$content = '<fonts count="' . count($this->styleIdToStyleMappingTable) . '">'; $content = '<fonts count="' . count($this->styleIdToStyleMappingTable) . '">';
/** @var \Box\Spout\Writer\Style\Style $style */ /** @var \Box\Spout\Writer\Common\Entity\Style\Style $style */
foreach ($this->getRegisteredStyles() as $style) { foreach ($this->getRegisteredStyles() as $style) {
$content .= '<font>'; $content .= '<font>';
@ -251,7 +251,7 @@ EOD;
$content .= '<border><left/><right/><top/><bottom/></border>'; $content .= '<border><left/><right/><top/><bottom/></border>';
foreach ($this->registeredBorders as $styleId) { 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]; $style = $this->styleIdToStyleMappingTable[$styleId];
$border = $style->getBorder(); $border = $style->getBorder();
$content .= '<border>'; $content .= '<border>';
@ -261,7 +261,7 @@ EOD;
foreach ($sortOrder as $partName) { foreach ($sortOrder as $partName) {
if ($border->hasPart($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); $part = $border->getPart($partName);
$content .= BorderHelper::serializeBorderPart($part); $content .= BorderHelper::serializeBorderPart($part);
} }

View File

@ -4,7 +4,7 @@ namespace Box\Spout\Writer\XLSX\Manager;
use Box\Spout\Writer\Common\Entity\Options; use Box\Spout\Writer\Common\Entity\Options;
use Box\Spout\Writer\Common\Manager\OptionsManagerAbstract; use Box\Spout\Writer\Common\Manager\OptionsManagerAbstract;
use Box\Spout\Writer\Style\StyleBuilder; use Box\Spout\Writer\Common\Creator\Style\StyleBuilder;
/** /**
* Class OptionsManager * Class OptionsManager

View File

@ -11,7 +11,7 @@ use Box\Spout\Writer\Common\Entity\Options;
use Box\Spout\Writer\Common\Entity\Cell; use Box\Spout\Writer\Common\Entity\Cell;
use Box\Spout\Writer\Common\Entity\Worksheet; use Box\Spout\Writer\Common\Entity\Worksheet;
use Box\Spout\Writer\Common\Manager\WorksheetManagerInterface; 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\SharedStringsHelper;
use Box\Spout\Writer\XLSX\Helper\StyleHelper; use Box\Spout\Writer\XLSX\Helper\StyleHelper;
@ -155,7 +155,7 @@ EOD;
* @param Worksheet $worksheet The worksheet to add the row to * @param Worksheet $worksheet The worksheet to add the row to
* @param array $dataRow Array containing data to be written. Cannot be empty. * @param array $dataRow Array containing data to be written. Cannot be empty.
* Example $dataRow = ['data1', 1234, null, '', 'data5']; * 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 * @return void
* @throws \Box\Spout\Common\Exception\IOException If the data cannot be written * @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 * @throws \Box\Spout\Common\Exception\InvalidArgumentException If a cell value's type is not supported

View File

@ -1,7 +1,17 @@
<?php <?php
namespace Box\Spout\Writer\Style; namespace Box\Spout\Writer\Common\EntityStyle;
use Box\Spout\Writer\Common\Creator\Style\BorderBuilder;
use Box\Spout\Writer\Common\Entity\Style\Border;
use Box\Spout\Writer\Common\Entity\Style\BorderPart;
use Box\Spout\Writer\Common\Entity\Style\Color;
/**
* Class BorderTest
*
* @package Box\Spout\Writer\Common\EntityStyle
*/
class BorderTest extends \PHPUnit_Framework_TestCase class BorderTest extends \PHPUnit_Framework_TestCase
{ {
/** /**
@ -20,7 +30,7 @@ class BorderTest extends \PHPUnit_Framework_TestCase
*/ */
public function testInvalidBorderPart() public function testInvalidBorderPart()
{ {
$invalidBorderPart = new BorderPart('invalid'); new BorderPart('invalid');
} }
/** /**
@ -28,7 +38,7 @@ class BorderTest extends \PHPUnit_Framework_TestCase
*/ */
public function testInvalidBorderPartStyle() public function testInvalidBorderPartStyle()
{ {
$invalidBorderPartStyle = new BorderPart(Border::LEFT, Color::BLACK, Border::WIDTH_THIN, 'invalid'); new BorderPart(Border::LEFT, Color::BLACK, Border::WIDTH_THIN, 'invalid');
} }
/** /**
@ -36,7 +46,7 @@ class BorderTest extends \PHPUnit_Framework_TestCase
*/ */
public function testInvalidBorderPartWidth() public function testInvalidBorderPartWidth()
{ {
$invalidBorderPartStyle = new BorderPart(Border::LEFT, Color::BLACK, 'invalid', Border::STYLE_DASHED); new BorderPart(Border::LEFT, Color::BLACK, 'invalid', Border::STYLE_DASHED);
} }
/** /**

View File

@ -1,11 +1,11 @@
<?php <?php
namespace Box\Spout\Writer\Style; namespace Box\Spout\Writer\Common\Entity\Style;
/** /**
* Class ColorTest * Class ColorTest
* *
* @package Box\Spout\Writer\Style * @package Box\Spout\Writer\Common\Entity\Style
*/ */
class ColorTest extends \PHPUnit_Framework_TestCase class ColorTest extends \PHPUnit_Framework_TestCase
{ {

View File

@ -1,14 +1,31 @@
<?php <?php
namespace Box\Spout\Writer\Style; namespace Box\Spout\Writer\Common\Manager;
use Box\Spout\Writer\Common\Creator\Style\BorderBuilder;
use Box\Spout\Writer\Common\Creator\Style\StyleBuilder;
use Box\Spout\Writer\Common\Entity\Style\Border;
use Box\Spout\Writer\Common\Entity\Style\Color;
use Box\Spout\Writer\Common\Entity\Style\Style;
/** /**
* Class StyleTest * Class StyleManagerTest
* *
* @package Box\Spout\Writer\Style * @package Box\Spout\Writer\Common\Manager
*/ */
class StyleTest extends \PHPUnit_Framework_TestCase class StyleManagerTest extends \PHPUnit_Framework_TestCase
{ {
/** @var StyleManager */
private $styleManager;
/**
* @return void
*/
public function setUp()
{
$this->styleManager = new StyleManager();
}
/** /**
* @return void * @return void
*/ */
@ -20,7 +37,7 @@ class StyleTest extends \PHPUnit_Framework_TestCase
$style2 = (new StyleBuilder())->setFontBold()->build(); $style2 = (new StyleBuilder())->setFontBold()->build();
$style2->setId(2); $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(); $baseStyle = (new StyleBuilder())->build();
$currentStyle = (new StyleBuilder())->build(); $currentStyle = (new StyleBuilder())->build();
$mergedStyle = $currentStyle->mergeWith($baseStyle); $mergedStyle = $this->styleManager->merge($currentStyle, $baseStyle);
$this->assertNotSame($mergedStyle, $currentStyle); $this->assertNotSame($mergedStyle, $currentStyle);
} }
@ -42,7 +59,7 @@ class StyleTest extends \PHPUnit_Framework_TestCase
{ {
$baseStyle = (new StyleBuilder())->setFontSize(99)->setFontBold()->build(); $baseStyle = (new StyleBuilder())->setFontSize(99)->setFontBold()->build();
$currentStyle = (new StyleBuilder())->setFontName('Font')->setFontUnderline()->build(); $currentStyle = (new StyleBuilder())->setFontName('Font')->setFontUnderline()->build();
$mergedStyle = $currentStyle->mergeWith($baseStyle); $mergedStyle = $this->styleManager->merge($currentStyle, $baseStyle);
$this->assertNotEquals(99, $currentStyle->getFontSize()); $this->assertNotEquals(99, $currentStyle->getFontSize());
$this->assertFalse($currentStyle->isFontBold()); $this->assertFalse($currentStyle->isFontBold());
@ -60,7 +77,7 @@ class StyleTest extends \PHPUnit_Framework_TestCase
{ {
$baseStyle = (new StyleBuilder())->setFontSize(10)->build(); $baseStyle = (new StyleBuilder())->setFontSize(10)->build();
$currentStyle = (new StyleBuilder())->setFontSize(99)->build(); $currentStyle = (new StyleBuilder())->setFontSize(99)->build();
$mergedStyle = $currentStyle->mergeWith($baseStyle); $mergedStyle = $this->styleManager->merge($currentStyle, $baseStyle);
$this->assertEquals(99, $mergedStyle->getFontSize()); $this->assertEquals(99, $mergedStyle->getFontSize());
} }
@ -72,7 +89,7 @@ class StyleTest extends \PHPUnit_Framework_TestCase
{ {
$baseStyle = (new StyleBuilder())->build(); $baseStyle = (new StyleBuilder())->build();
$currentStyle = (new StyleBuilder())->setFontItalic()->setFontStrikethrough()->build(); $currentStyle = (new StyleBuilder())->setFontItalic()->setFontStrikethrough()->build();
$mergedStyle = $currentStyle->mergeWith($baseStyle); $mergedStyle = $this->styleManager->merge($currentStyle, $baseStyle);
$this->assertFalse($baseStyle->isFontItalic()); $this->assertFalse($baseStyle->isFontItalic());
$this->assertFalse($baseStyle->isFontStrikethrough()); $this->assertFalse($baseStyle->isFontStrikethrough());
@ -93,7 +110,7 @@ class StyleTest extends \PHPUnit_Framework_TestCase
->setShouldWrapText() ->setShouldWrapText()
->build(); ->build();
$currentStyle = (new StyleBuilder())->build(); $currentStyle = (new StyleBuilder())->build();
$mergedStyle = $currentStyle->mergeWith($baseStyle); $mergedStyle = $this->styleManager->merge($currentStyle, $baseStyle);
$this->assertFalse($currentStyle->isFontUnderline()); $this->assertFalse($currentStyle->isFontUnderline());
$this->assertTrue($mergedStyle->isFontUnderline()); $this->assertTrue($mergedStyle->isFontUnderline());
@ -109,10 +126,10 @@ class StyleTest extends \PHPUnit_Framework_TestCase
{ {
$baseStyle = (new StyleBuilder())->build(); $baseStyle = (new StyleBuilder())->build();
$currentStyle = (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($this->styleManager->serialize($baseStyle) === $this->styleManager->serialize($currentStyle));
$this->assertTrue($currentStyle->serialize() === $mergedStyle->serialize()); $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) ->setFontSize(Style::DEFAULT_FONT_SIZE)
->build(); ->build();
$currentStyle = (new StyleBuilder())->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(); $baseStyle = (new StyleBuilder())->setBorder($border)->build();
$currentStyle = (new StyleBuilder())->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->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\Common\Entity\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', $mergedStyle->getBorder(), 'Merged style has a border');
} }
} }

View File

@ -2,7 +2,9 @@
namespace Box\Spout\Writer\ODS\Helper; 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 * Class StyleHelperTest
@ -11,15 +13,19 @@ use Box\Spout\Writer\Style\StyleBuilder;
*/ */
class StyleHelperTest extends \PHPUnit_Framework_TestCase class StyleHelperTest extends \PHPUnit_Framework_TestCase
{ {
/** @var \Box\Spout\Writer\Style\Style */ /** @var Style */
protected $defaultStyle; protected $defaultStyle;
/** @var StyleHelper */
private $styleHelper;
/** /**
* @return void * @return void
*/ */
public function setUp() public function setUp()
{ {
$this->defaultStyle = (new StyleBuilder())->build(); $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($style1->getId());
$this->assertNull($style2->getId()); $this->assertNull($style2->getId());
$styleHelper = new StyleHelper($this->defaultStyle); $registeredStyle1 = $this->styleHelper->registerStyle($style1);
$registeredStyle1 = $styleHelper->registerStyle($style1); $registeredStyle2 = $this->styleHelper->registerStyle($style2);
$registeredStyle2 = $styleHelper->registerStyle($style2);
$this->assertEquals(1, $registeredStyle1->getId()); $this->assertEquals(1, $registeredStyle1->getId());
$this->assertEquals(2, $registeredStyle2->getId()); $this->assertEquals(2, $registeredStyle2->getId());
@ -49,9 +54,8 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase
{ {
$style = (new StyleBuilder())->setFontBold()->build(); $style = (new StyleBuilder())->setFontBold()->build();
$styleHelper = new StyleHelper($this->defaultStyle); $registeredStyle1 = $this->styleHelper->registerStyle($style);
$registeredStyle1 = $styleHelper->registerStyle($style); $registeredStyle2 = $this->styleHelper->registerStyle($style);
$registeredStyle2 = $styleHelper->registerStyle($style);
$this->assertEquals(1, $registeredStyle1->getId()); $this->assertEquals(1, $registeredStyle1->getId());
$this->assertEquals(1, $registeredStyle2->getId()); $this->assertEquals(1, $registeredStyle2->getId());
@ -63,11 +67,10 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase
public function testApplyExtraStylesIfNeededShouldApplyWrapTextIfCellContainsNewLine() public function testApplyExtraStylesIfNeededShouldApplyWrapTextIfCellContainsNewLine()
{ {
$style = clone $this->defaultStyle; $style = clone $this->defaultStyle;
$styleHelper = new StyleHelper($this->defaultStyle);
$this->assertFalse($style->shouldWrapText()); $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()); $this->assertTrue($updatedStyle->shouldWrapText());
} }
@ -78,11 +81,10 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase
public function testApplyExtraStylesIfNeededShouldDoNothingIfWrapTextAlreadyApplied() public function testApplyExtraStylesIfNeededShouldDoNothingIfWrapTextAlreadyApplied()
{ {
$style = (new StyleBuilder())->setShouldWrapText()->build(); $style = (new StyleBuilder())->setShouldWrapText()->build();
$styleHelper = new StyleHelper($this->defaultStyle);
$this->assertTrue($style->shouldWrapText()); $this->assertTrue($style->shouldWrapText());
$updatedStyle = $styleHelper->applyExtraStylesIfNeeded($style, ["multi\nlines"]); $updatedStyle = $this->styleHelper->applyExtraStylesIfNeeded($style, ["multi\nlines"]);
$this->assertTrue($updatedStyle->shouldWrapText()); $this->assertTrue($updatedStyle->shouldWrapText());
} }

View File

@ -6,11 +6,11 @@ use Box\Spout\Common\Type;
use Box\Spout\Reader\Wrapper\XMLReader; use Box\Spout\Reader\Wrapper\XMLReader;
use Box\Spout\TestUsingResource; use Box\Spout\TestUsingResource;
use Box\Spout\Writer\ODS\Helper\BorderHelper; use Box\Spout\Writer\ODS\Helper\BorderHelper;
use Box\Spout\Writer\Style\Border; use Box\Spout\Writer\Common\Entity\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\Common\Entity\Style\Color;
use Box\Spout\Writer\Style\Style; use Box\Spout\Writer\Common\Entity\Style\Style;
use Box\Spout\Writer\Style\StyleBuilder; use Box\Spout\Writer\Common\Creator\Style\StyleBuilder;
use Box\Spout\Writer\WriterFactory; use Box\Spout\Writer\WriterFactory;
/** /**
@ -22,7 +22,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
{ {
use TestUsingResource; use TestUsingResource;
/** @var \Box\Spout\Writer\Style\Style */ /** @var \Box\Spout\Writer\Common\Entity\Style\Style */
protected $defaultStyle; protected $defaultStyle;
/** /**
@ -67,7 +67,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
* @dataProvider dataProviderForInvalidStyle * @dataProvider dataProviderForInvalidStyle
* @expectedException \Box\Spout\Common\Exception\InvalidArgumentException * @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) public function testAddRowWithStyleShouldThrowExceptionIfInvalidStyleGiven($style)
{ {
@ -84,7 +84,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
* @dataProvider dataProviderForInvalidStyle * @dataProvider dataProviderForInvalidStyle
* @expectedException \Box\Spout\Common\Exception\InvalidArgumentException * @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) public function testAddRowsWithStyleShouldThrowExceptionIfInvalidStyleGiven($style)
{ {
@ -346,7 +346,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
/** /**
* @param array $allRows * @param array $allRows
* @param string $fileName * @param string $fileName
* @param \Box\Spout\Writer\Style\Style $style * @param \Box\Spout\Writer\Common\Entity\Style\Style $style
* @return Writer * @return Writer
*/ */
private function writeToODSFile($allRows, $fileName, $style) private function writeToODSFile($allRows, $fileName, $style)
@ -367,7 +367,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
/** /**
* @param array $allRows * @param array $allRows
* @param string $fileName * @param string $fileName
* @param \Box\Spout\Writer\Style\Style|null $defaultStyle * @param \Box\Spout\Writer\Common\Entity\Style\Style|null $defaultStyle
* @return Writer * @return Writer
*/ */
private function writeToODSFileWithDefaultStyle($allRows, $fileName, $defaultStyle) private function writeToODSFileWithDefaultStyle($allRows, $fileName, $defaultStyle)
@ -389,7 +389,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
/** /**
* @param array $allRows * @param array $allRows
* @param string $fileName * @param string $fileName
* @param \Box\Spout\Writer\Style\Style|null[] $styles * @param \Box\Spout\Writer\Common\Entity\Style\Style|null[] $styles
* @return Writer * @return Writer
*/ */
private function writeToODSFileWithMultipleStyles($allRows, $fileName, $styles) private function writeToODSFileWithMultipleStyles($allRows, $fileName, $styles)

View File

@ -2,10 +2,12 @@
namespace Box\Spout\Writer\XLSX\Helper; namespace Box\Spout\Writer\XLSX\Helper;
use Box\Spout\Writer\Style\Border; use Box\Spout\Writer\Common\Entity\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\Common\Entity\Style\Color;
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 * Class StyleHelperTest
@ -14,15 +16,19 @@ use Box\Spout\Writer\Style\StyleBuilder;
*/ */
class StyleHelperTest extends \PHPUnit_Framework_TestCase class StyleHelperTest extends \PHPUnit_Framework_TestCase
{ {
/** @var \Box\Spout\Writer\Style\Style */ /** @var Style */
protected $defaultStyle; protected $defaultStyle;
/** @var StyleHelper */
private $styleHelper;
/** /**
* @return void * @return void
*/ */
public function setUp() public function setUp()
{ {
$this->defaultStyle = (new StyleBuilder())->build(); $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($style1->getId());
$this->assertNull($style2->getId()); $this->assertNull($style2->getId());
$styleHelper = new StyleHelper($this->defaultStyle); $registeredStyle1 = $this->styleHelper->registerStyle($style1);
$registeredStyle1 = $styleHelper->registerStyle($style1); $registeredStyle2 = $this->styleHelper->registerStyle($style2);
$registeredStyle2 = $styleHelper->registerStyle($style2);
$this->assertEquals(1, $registeredStyle1->getId()); $this->assertEquals(1, $registeredStyle1->getId());
$this->assertEquals(2, $registeredStyle2->getId()); $this->assertEquals(2, $registeredStyle2->getId());
@ -52,9 +57,8 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase
{ {
$style = (new StyleBuilder())->setFontBold()->build(); $style = (new StyleBuilder())->setFontBold()->build();
$styleHelper = new StyleHelper($this->defaultStyle); $registeredStyle1 = $this->styleHelper->registerStyle($style);
$registeredStyle1 = $styleHelper->registerStyle($style); $registeredStyle2 = $this->styleHelper->registerStyle($style);
$registeredStyle2 = $styleHelper->registerStyle($style);
$this->assertEquals(1, $registeredStyle1->getId()); $this->assertEquals(1, $registeredStyle1->getId());
$this->assertEquals(1, $registeredStyle2->getId()); $this->assertEquals(1, $registeredStyle2->getId());
@ -71,14 +75,13 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase
$border = (new BorderBuilder())->setBorderBottom(Color::GREEN)->build(); $border = (new BorderBuilder())->setBorderBottom(Color::GREEN)->build();
$styleWithBorder = (new StyleBuilder())->setBorder($border)->build(); $styleWithBorder = (new StyleBuilder())->setBorder($border)->build();
$styleHelper = new StyleHelper($this->defaultStyle); $this->styleHelper->registerStyle($styleWithFont);
$styleHelper->registerStyle($styleWithFont); $this->styleHelper->registerStyle($styleWithBackground);
$styleHelper->registerStyle($styleWithBackground); $this->styleHelper->registerStyle($styleWithBorder);
$styleHelper->registerStyle($styleWithBorder);
$this->assertFalse($styleHelper->shouldApplyStyleOnEmptyCell($styleWithFont->getId())); $this->assertFalse($this->styleHelper->shouldApplyStyleOnEmptyCell($styleWithFont->getId()));
$this->assertTrue($styleHelper->shouldApplyStyleOnEmptyCell($styleWithBackground->getId())); $this->assertTrue($this->styleHelper->shouldApplyStyleOnEmptyCell($styleWithBackground->getId()));
$this->assertTrue($styleHelper->shouldApplyStyleOnEmptyCell($styleWithBorder->getId())); $this->assertTrue($this->styleHelper->shouldApplyStyleOnEmptyCell($styleWithBorder->getId()));
} }
/** /**
@ -87,11 +90,10 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase
public function testApplyExtraStylesIfNeededShouldApplyWrapTextIfCellContainsNewLine() public function testApplyExtraStylesIfNeededShouldApplyWrapTextIfCellContainsNewLine()
{ {
$style = clone $this->defaultStyle; $style = clone $this->defaultStyle;
$styleHelper = new StyleHelper($this->defaultStyle);
$this->assertFalse($style->shouldWrapText()); $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()); $this->assertTrue($updatedStyle->shouldWrapText());
} }
@ -102,11 +104,10 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase
public function testApplyExtraStylesIfNeededShouldDoNothingIfWrapTextAlreadyApplied() public function testApplyExtraStylesIfNeededShouldDoNothingIfWrapTextAlreadyApplied()
{ {
$style = (new StyleBuilder())->setShouldWrapText()->build(); $style = (new StyleBuilder())->setShouldWrapText()->build();
$styleHelper = new StyleHelper($this->defaultStyle);
$this->assertTrue($style->shouldWrapText()); $this->assertTrue($style->shouldWrapText());
$updatedStyle = $styleHelper->applyExtraStylesIfNeeded($style, ["multi\nlines"]); $updatedStyle = $this->styleHelper->applyExtraStylesIfNeeded($style, ["multi\nlines"]);
$this->assertTrue($updatedStyle->shouldWrapText()); $this->assertTrue($updatedStyle->shouldWrapText());
} }

View File

@ -5,11 +5,12 @@ namespace Box\Spout\Writer\XLSX;
use Box\Spout\Common\Type; use Box\Spout\Common\Type;
use Box\Spout\Reader\Wrapper\XMLReader; use Box\Spout\Reader\Wrapper\XMLReader;
use Box\Spout\TestUsingResource; use Box\Spout\TestUsingResource;
use Box\Spout\Writer\Style\Border; use Box\Spout\Writer\Common\Entity\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\Common\Entity\Style\Color;
use Box\Spout\Writer\Style\Style; use Box\Spout\Writer\Common\Entity\Style\Style;
use Box\Spout\Writer\Style\StyleBuilder; use Box\Spout\Writer\Common\Creator\Style\StyleBuilder;
use Box\Spout\Writer\Common\Manager\StyleManager;
use Box\Spout\Writer\WriterFactory; use Box\Spout\Writer\WriterFactory;
use Box\Spout\Writer\XLSX\Manager\OptionsManager; use Box\Spout\Writer\XLSX\Manager\OptionsManager;
@ -22,7 +23,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
{ {
use TestUsingResource; use TestUsingResource;
/** @var \Box\Spout\Writer\Style\Style */ /** @var \Box\Spout\Writer\Common\Entity\Style\Style */
protected $defaultStyle; protected $defaultStyle;
/** /**
@ -67,7 +68,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
* @dataProvider dataProviderForInvalidStyle * @dataProvider dataProviderForInvalidStyle
* @expectedException \Box\Spout\Common\Exception\InvalidArgumentException * @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) public function testAddRowWithStyleShouldThrowExceptionIfInvalidStyleGiven($style)
{ {
@ -84,7 +85,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
* @dataProvider dataProviderForInvalidStyle * @dataProvider dataProviderForInvalidStyle
* @expectedException \Box\Spout\Common\Exception\InvalidArgumentException * @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) public function testAddRowsWithStyleShouldThrowExceptionIfInvalidStyleGiven($style)
{ {
@ -462,7 +463,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
$fontStyle = (new StyleBuilder())->setFontBold()->build(); $fontStyle = (new StyleBuilder())->setFontBold()->build();
$emptyStyle = (new StyleBuilder())->build(); $emptyStyle = (new StyleBuilder())->build();
$borderRightFontBoldStyle = $borderRightStyle->mergeWith($fontStyle); $borderRightFontBoldStyle = (new StyleManager())->merge($borderRightStyle, $fontStyle);
$dataRows = [ $dataRows = [
['Border-Left'], ['Border-Left'],
@ -517,7 +518,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
/** /**
* @param array $allRows * @param array $allRows
* @param string $fileName * @param string $fileName
* @param \Box\Spout\Writer\Style\Style $style * @param \Box\Spout\Writer\Common\Entity\Style\Style $style
* @return Writer * @return Writer
*/ */
private function writeToXLSXFile($allRows, $fileName, $style) private function writeToXLSXFile($allRows, $fileName, $style)
@ -539,7 +540,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
/** /**
* @param array $allRows * @param array $allRows
* @param string $fileName * @param string $fileName
* @param \Box\Spout\Writer\Style\Style|null $defaultStyle * @param \Box\Spout\Writer\Common\Entity\Style\Style|null $defaultStyle
* @return Writer * @return Writer
*/ */
private function writeToXLSXFileWithDefaultStyle($allRows, $fileName, $defaultStyle) private function writeToXLSXFileWithDefaultStyle($allRows, $fileName, $defaultStyle)
@ -562,7 +563,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
/** /**
* @param array $allRows * @param array $allRows
* @param string $fileName * @param string $fileName
* @param \Box\Spout\Writer\Style\Style|null[] $styles * @param \Box\Spout\Writer\Common\Entity\Style\Style|null[] $styles
* @return Writer * @return Writer
*/ */
private function writeToXLSXFileWithMultipleStyles($allRows, $fileName, $styles) private function writeToXLSXFileWithMultipleStyles($allRows, $fileName, $styles)