Move Style classes into Common folder (#432)

This commit is contained in:
Adrien Loison authored and GitHub committed 2017-05-30 00:56:50 +02:00
1 parent c4e25a168e
commit 238756ab6e
38 files changed
+463 -298

No files matched your search

+1 -1
View File
@@ -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
*/
@@ -1,9 +1,15 @@
<?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
*
* @package \Box\Spout\Writer\Common\Creator\Style
*/
class BorderBuilder
{
@@ -1,12 +1,15 @@
<?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
* Builder to create new styles
*
* @package Box\Spout\Writer\Style
* @package Box\Spout\Writer\Common\Creator\Style
*/
class StyleBuilder
{
@@ -1,9 +1,11 @@
<?php
namespace Box\Spout\Writer\Style;
namespace Box\Spout\Writer\Common\Entity\Style;
/**
* Class Border
*
* @package \Box\Spout\Writer\Common\Entity\Style
*/
class Border
{
@@ -22,10 +24,8 @@ class Border
const WIDTH_MEDIUM = 'medium';
const WIDTH_THICK = 'thick';
/**
* @var array A list of BorderPart objects for this border.
*/
protected $parts = [];
/** @var array A list of BorderPart objects for this border. */
private $parts = [];
/**
* @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
*/
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
*/
public function hasPart($name)
@@ -64,6 +64,7 @@ class Border
/**
* Set BorderParts
* @param array $parts
* @return void
*/
public function setParts($parts)
{
@@ -75,7 +76,7 @@ class Border
/**
* @param BorderPart $borderPart
* @return self
* @return Border
*/
public function addPart(BorderPart $borderPart)
{
@@ -1,6 +1,6 @@
<?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\InvalidStyleException;
@@ -8,6 +8,8 @@ use Box\Spout\Writer\Exception\Border\InvalidWidthException;
/**
* Class BorderPart
*
* @package \Box\Spout\Writer\Common\Entity\Style
*/
class BorderPart
{
@@ -1,6 +1,6 @@
<?php
namespace Box\Spout\Writer\Style;
namespace Box\Spout\Writer\Common\Entity\Style;
use Box\Spout\Writer\Exception\InvalidColorException;
@@ -8,7 +8,7 @@ use Box\Spout\Writer\Exception\InvalidColorException;
* Class Color
* This class provides constants and functions to work with colors
*
* @package Box\Spout\Writer\Style
* @package Box\Spout\Writer\Common\Entity\Style
*/
class Color
{
@@ -1,12 +1,12 @@
<?php
namespace Box\Spout\Writer\Style;
namespace Box\Spout\Writer\Common\Entity\Style;
/**
* Class Style
* Represents a style to be applied to a cell
*
* @package Box\Spout\Writer\Style
* @package Box\Spout\Writer\Common\Entity\Style
*/
class Style
{
@@ -16,62 +16,62 @@ class Style
const DEFAULT_FONT_NAME = 'Arial';
/** @var int|null Style ID */
protected $id = null;
private $id = null;
/** @var bool Whether the font should be bold */
protected $fontBold = false;
private $fontBold = false;
/** @var bool Whether the bold property was set */
protected $hasSetFontBold = false;
private $hasSetFontBold = false;
/** @var bool Whether the font should be italic */
protected $fontItalic = false;
private $fontItalic = false;
/** @var bool Whether the italic property was set */
protected $hasSetFontItalic = false;
private $hasSetFontItalic = false;
/** @var bool Whether the font should be underlined */
protected $fontUnderline = false;
private $fontUnderline = false;
/** @var bool Whether the underline property was set */
protected $hasSetFontUnderline = false;
private $hasSetFontUnderline = false;
/** @var bool Whether the font should be struck through */
protected $fontStrikethrough = false;
private $fontStrikethrough = false;
/** @var bool Whether the strikethrough property was set */
protected $hasSetFontStrikethrough = false;
private $hasSetFontStrikethrough = false;
/** @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 */
protected $hasSetFontSize = false;
private $hasSetFontSize = false;
/** @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 */
protected $hasSetFontColor = false;
private $hasSetFontColor = false;
/** @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 */
protected $hasSetFontName = false;
private $hasSetFontName = false;
/** @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) */
protected $shouldWrapText = false;
private $shouldWrapText = false;
/** @var bool Whether the wrap text property was set */
protected $hasSetWrapText = false;
private $hasSetWrapText = false;
/** @var Border */
protected $border = null;
private $border = null;
/** @var bool Whether border properties should be applied */
protected $shouldApplyBorder = false;
private $shouldApplyBorder = false;
/** @var string Background color */
protected $backgroundColor = null;
private $backgroundColor = null;
/** @var bool */
protected $hasSetBackgroundColor = false;
private $hasSetBackgroundColor = false;
/**
@@ -138,6 +138,14 @@ class Style
return $this;
}
/**
* @return bool
*/
public function hasSetFontBold()
{
return $this->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());
}
}
}
@@ -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)
{
@@ -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);
}
@@ -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());
}
}
}
@@ -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
@@ -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
@@ -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
@@ -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
{
@@ -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
{
@@ -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
{
@@ -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();
}
/**
+2 -2
View File
@@ -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
+9 -9
View File
@@ -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 "<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
*/
protected function getStyleSectionContent($style)
@@ -229,7 +229,7 @@ EOD;
/**
* 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
*/
private function getTextPropertiesSectionContent($style)
@@ -246,7 +246,7 @@ EOD;
/**
* 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
*/
private function getFontSectionContent($style)
@@ -291,7 +291,7 @@ EOD;
/**
* 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
*/
private function getTableCellPropertiesSectionContent($style)
@@ -326,7 +326,7 @@ EOD;
/**
* 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
*/
private function getBorderXMLContent($style)
@@ -343,7 +343,7 @@ EOD;
/**
* 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
*/
private function getBackgroundColorXMLContent($style)
@@ -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
@@ -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
+22 -14
View File
@@ -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);
}
/**
+8 -4
View File
@@ -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);
}
}
+4 -2
View File
@@ -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
@@ -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
@@ -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();
}
/**
@@ -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
{
+9 -9
View File
@@ -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 = '<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) {
$content .= '<font>';
@@ -251,7 +251,7 @@ EOD;
$content .= '<border><left/><right/><top/><bottom/></border>';
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 .= '<border>';
@@ -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);
}
@@ -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
@@ -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