Move Style classes into Common folder (#432)
This commit is contained in:
1 parent
c4e25a168e
commit
238756ab6e
38 files changed
+463
-298
No files matched your search
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
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
|
||||
{
|
||||
/**
|
||||
* @var Border
|
||||
*/
|
||||
protected $border;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->border = new Border();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|void $color Border A RGB color code
|
||||
* @param string|void $width Border width @see BorderPart::allowedWidths
|
||||
* @param string|void $style Border style @see BorderPart::allowedStyles
|
||||
* @return BorderBuilder
|
||||
*/
|
||||
public function setBorderTop($color = Color::BLACK, $width = Border::WIDTH_MEDIUM, $style = Border::STYLE_SOLID)
|
||||
{
|
||||
$this->border->addPart(new BorderPart(Border::TOP, $color, $width, $style));
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|void $color Border A RGB color code
|
||||
* @param string|void $width Border width @see BorderPart::allowedWidths
|
||||
* @param string|void $style Border style @see BorderPart::allowedStyles
|
||||
* @return BorderBuilder
|
||||
*/
|
||||
public function setBorderRight($color = Color::BLACK, $width = Border::WIDTH_MEDIUM, $style = Border::STYLE_SOLID)
|
||||
{
|
||||
$this->border->addPart(new BorderPart(Border::RIGHT, $color, $width, $style));
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|void $color Border A RGB color code
|
||||
* @param string|void $width Border width @see BorderPart::allowedWidths
|
||||
* @param string|void $style Border style @see BorderPart::allowedStyles
|
||||
* @return BorderBuilder
|
||||
*/
|
||||
public function setBorderBottom($color = Color::BLACK, $width = Border::WIDTH_MEDIUM, $style = Border::STYLE_SOLID)
|
||||
{
|
||||
$this->border->addPart(new BorderPart(Border::BOTTOM, $color, $width, $style));
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|void $color Border A RGB color code
|
||||
* @param string|void $width Border width @see BorderPart::allowedWidths
|
||||
* @param string|void $style Border style @see BorderPart::allowedStyles
|
||||
* @return BorderBuilder
|
||||
*/
|
||||
public function setBorderLeft($color = Color::BLACK, $width = Border::WIDTH_MEDIUM, $style = Border::STYLE_SOLID)
|
||||
{
|
||||
$this->border->addPart(new BorderPart(Border::LEFT, $color, $width, $style));
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Border
|
||||
*/
|
||||
public function build()
|
||||
{
|
||||
return $this->border;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,162 @@
|
||||
<?php
|
||||
|
||||
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\Common\Creator\Style
|
||||
*/
|
||||
class StyleBuilder
|
||||
{
|
||||
/** @var Style Style to be created */
|
||||
protected $style;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->style = new Style();
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes the font bold.
|
||||
*
|
||||
* @api
|
||||
* @return StyleBuilder
|
||||
*/
|
||||
public function setFontBold()
|
||||
{
|
||||
$this->style->setFontBold();
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes the font italic.
|
||||
*
|
||||
* @api
|
||||
* @return StyleBuilder
|
||||
*/
|
||||
public function setFontItalic()
|
||||
{
|
||||
$this->style->setFontItalic();
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes the font underlined.
|
||||
*
|
||||
* @api
|
||||
* @return StyleBuilder
|
||||
*/
|
||||
public function setFontUnderline()
|
||||
{
|
||||
$this->style->setFontUnderline();
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes the font struck through.
|
||||
*
|
||||
* @api
|
||||
* @return StyleBuilder
|
||||
*/
|
||||
public function setFontStrikethrough()
|
||||
{
|
||||
$this->style->setFontStrikethrough();
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the font size.
|
||||
*
|
||||
* @api
|
||||
* @param int $fontSize Font size, in pixels
|
||||
* @return StyleBuilder
|
||||
*/
|
||||
public function setFontSize($fontSize)
|
||||
{
|
||||
$this->style->setFontSize($fontSize);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the font color.
|
||||
*
|
||||
* @api
|
||||
* @param string $fontColor ARGB color (@see Color)
|
||||
* @return StyleBuilder
|
||||
*/
|
||||
public function setFontColor($fontColor)
|
||||
{
|
||||
$this->style->setFontColor($fontColor);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the font name.
|
||||
*
|
||||
* @api
|
||||
* @param string $fontName Name of the font to use
|
||||
* @return StyleBuilder
|
||||
*/
|
||||
public function setFontName($fontName)
|
||||
{
|
||||
$this->style->setFontName($fontName);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes the text wrap in the cell if requested
|
||||
*
|
||||
* @api
|
||||
* @param bool $shouldWrap Should the text be wrapped
|
||||
* @return StyleBuilder
|
||||
*/
|
||||
public function setShouldWrapText($shouldWrap = true)
|
||||
{
|
||||
$this->style->setShouldWrapText($shouldWrap);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a border
|
||||
*
|
||||
* @param Border $border
|
||||
* @return $this
|
||||
*/
|
||||
public function setBorder(Border $border)
|
||||
{
|
||||
$this->style->setBorder($border);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a background color
|
||||
*
|
||||
* @api
|
||||
* @param string $color ARGB color (@see Color)
|
||||
* @return StyleBuilder
|
||||
*/
|
||||
public function setBackgroundColor($color)
|
||||
{
|
||||
$this->style->setBackgroundColor($color);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the configured style. The style is cached and can be reused.
|
||||
*
|
||||
* @api
|
||||
* @return Style
|
||||
*/
|
||||
public function build()
|
||||
{
|
||||
return $this->style;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Writer\Common\Entity\Style;
|
||||
|
||||
/**
|
||||
* Class Border
|
||||
*
|
||||
* @package \Box\Spout\Writer\Common\Entity\Style
|
||||
*/
|
||||
class Border
|
||||
{
|
||||
const LEFT = 'left';
|
||||
const RIGHT = 'right';
|
||||
const TOP = 'top';
|
||||
const BOTTOM = 'bottom';
|
||||
|
||||
const STYLE_NONE = 'none';
|
||||
const STYLE_SOLID = 'solid';
|
||||
const STYLE_DASHED = 'dashed';
|
||||
const STYLE_DOTTED = 'dotted';
|
||||
const STYLE_DOUBLE = 'double';
|
||||
|
||||
const WIDTH_THIN = 'thin';
|
||||
const WIDTH_MEDIUM = 'medium';
|
||||
const WIDTH_THICK = 'thick';
|
||||
|
||||
/** @var array A list of BorderPart objects for this border. */
|
||||
private $parts = [];
|
||||
|
||||
/**
|
||||
* @param array|void $borderParts
|
||||
*/
|
||||
public function __construct(array $borderParts = [])
|
||||
{
|
||||
$this->setParts($borderParts);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name The name of the border part
|
||||
* @return null|BorderPart
|
||||
*/
|
||||
public function getPart($name)
|
||||
{
|
||||
return $this->hasPart($name) ? $this->parts[$name] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name The name of the border part
|
||||
* @return bool
|
||||
*/
|
||||
public function hasPart($name)
|
||||
{
|
||||
return isset($this->parts[$name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getParts()
|
||||
{
|
||||
return $this->parts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set BorderParts
|
||||
* @param array $parts
|
||||
* @return void
|
||||
*/
|
||||
public function setParts($parts)
|
||||
{
|
||||
unset($this->parts);
|
||||
foreach ($parts as $part) {
|
||||
$this->addPart($part);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param BorderPart $borderPart
|
||||
* @return Border
|
||||
*/
|
||||
public function addPart(BorderPart $borderPart)
|
||||
{
|
||||
$this->parts[$borderPart->getName()] = $borderPart;
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Writer\Common\Entity\Style;
|
||||
|
||||
use Box\Spout\Writer\Exception\Border\InvalidNameException;
|
||||
use Box\Spout\Writer\Exception\Border\InvalidStyleException;
|
||||
use Box\Spout\Writer\Exception\Border\InvalidWidthException;
|
||||
|
||||
/**
|
||||
* Class BorderPart
|
||||
*
|
||||
* @package \Box\Spout\Writer\Common\Entity\Style
|
||||
*/
|
||||
class BorderPart
|
||||
{
|
||||
/**
|
||||
* @var string The style of this border part.
|
||||
*/
|
||||
protected $style;
|
||||
|
||||
/**
|
||||
* @var string The name of this border part.
|
||||
*/
|
||||
protected $name;
|
||||
|
||||
/**
|
||||
* @var string The color of this border part.
|
||||
*/
|
||||
protected $color;
|
||||
|
||||
/**
|
||||
* @var string The width of this border part.
|
||||
*/
|
||||
protected $width;
|
||||
|
||||
/**
|
||||
* @var array Allowed style constants for parts.
|
||||
*/
|
||||
protected static $allowedStyles = [
|
||||
'none',
|
||||
'solid',
|
||||
'dashed',
|
||||
'dotted',
|
||||
'double'
|
||||
];
|
||||
|
||||
/**
|
||||
* @var array Allowed names constants for border parts.
|
||||
*/
|
||||
protected static $allowedNames = [
|
||||
'left',
|
||||
'right',
|
||||
'top',
|
||||
'bottom',
|
||||
];
|
||||
|
||||
/**
|
||||
* @var array Allowed width constants for border parts.
|
||||
*/
|
||||
protected static $allowedWidths = [
|
||||
'thin',
|
||||
'medium',
|
||||
'thick',
|
||||
];
|
||||
|
||||
/**
|
||||
* @param string $name @see BorderPart::$allowedNames
|
||||
* @param string $color A RGB color code
|
||||
* @param string $width @see BorderPart::$allowedWidths
|
||||
* @param string $style @see BorderPart::$allowedStyles
|
||||
* @throws InvalidNameException
|
||||
* @throws InvalidStyleException
|
||||
* @throws InvalidWidthException
|
||||
*/
|
||||
public function __construct($name, $color = Color::BLACK, $width = Border::WIDTH_MEDIUM, $style = Border::STYLE_SOLID)
|
||||
{
|
||||
$this->setName($name);
|
||||
$this->setColor($color);
|
||||
$this->setWidth($width);
|
||||
$this->setStyle($style);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name The name of the border part @see BorderPart::$allowedNames
|
||||
* @throws InvalidNameException
|
||||
* @return void
|
||||
*/
|
||||
public function setName($name)
|
||||
{
|
||||
if (!in_array($name, self::$allowedNames)) {
|
||||
throw new InvalidNameException($name);
|
||||
}
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getStyle()
|
||||
{
|
||||
return $this->style;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $style The style of the border part @see BorderPart::$allowedStyles
|
||||
* @throws InvalidStyleException
|
||||
* @return void
|
||||
*/
|
||||
public function setStyle($style)
|
||||
{
|
||||
if (!in_array($style, self::$allowedStyles)) {
|
||||
throw new InvalidStyleException($style);
|
||||
}
|
||||
$this->style = $style;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getColor()
|
||||
{
|
||||
return $this->color;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $color The color of the border part @see Color::rgb()
|
||||
* @return void
|
||||
*/
|
||||
public function setColor($color)
|
||||
{
|
||||
$this->color = $color;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getWidth()
|
||||
{
|
||||
return $this->width;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $width The width of the border part @see BorderPart::$allowedWidths
|
||||
* @throws InvalidWidthException
|
||||
* @return void
|
||||
*/
|
||||
public function setWidth($width)
|
||||
{
|
||||
if (!in_array($width, self::$allowedWidths)) {
|
||||
throw new InvalidWidthException($width);
|
||||
}
|
||||
$this->width = $width;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public static function getAllowedStyles()
|
||||
{
|
||||
return self::$allowedStyles;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public static function getAllowedNames()
|
||||
{
|
||||
return self::$allowedNames;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public static function getAllowedWidths()
|
||||
{
|
||||
return self::$allowedWidths;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Writer\Common\Entity\Style;
|
||||
|
||||
use Box\Spout\Writer\Exception\InvalidColorException;
|
||||
|
||||
/**
|
||||
* Class Color
|
||||
* This class provides constants and functions to work with colors
|
||||
*
|
||||
* @package Box\Spout\Writer\Common\Entity\Style
|
||||
*/
|
||||
class Color
|
||||
{
|
||||
/** Standard colors - based on Office Online */
|
||||
const BLACK = '000000';
|
||||
const WHITE = 'FFFFFF';
|
||||
const RED = 'FF0000';
|
||||
const DARK_RED = 'C00000';
|
||||
const ORANGE = 'FFC000';
|
||||
const YELLOW = 'FFFF00';
|
||||
const LIGHT_GREEN = '92D040';
|
||||
const GREEN = '00B050';
|
||||
const LIGHT_BLUE = '00B0E0';
|
||||
const BLUE = '0070C0';
|
||||
const DARK_BLUE = '002060';
|
||||
const PURPLE = '7030A0';
|
||||
|
||||
/**
|
||||
* Returns an RGB color from R, G and B values
|
||||
*
|
||||
* @api
|
||||
* @param int $red Red component, 0 - 255
|
||||
* @param int $green Green component, 0 - 255
|
||||
* @param int $blue Blue component, 0 - 255
|
||||
* @return string RGB color
|
||||
*/
|
||||
public static function rgb($red, $green, $blue)
|
||||
{
|
||||
self::throwIfInvalidColorComponentValue($red);
|
||||
self::throwIfInvalidColorComponentValue($green);
|
||||
self::throwIfInvalidColorComponentValue($blue);
|
||||
|
||||
return strtoupper(
|
||||
self::convertColorComponentToHex($red) .
|
||||
self::convertColorComponentToHex($green) .
|
||||
self::convertColorComponentToHex($blue)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws an exception is the color component value is outside of bounds (0 - 255)
|
||||
*
|
||||
* @param int $colorComponent
|
||||
* @return void
|
||||
* @throws \Box\Spout\Writer\Exception\InvalidColorException
|
||||
*/
|
||||
protected static function throwIfInvalidColorComponentValue($colorComponent)
|
||||
{
|
||||
if (!is_int($colorComponent) || $colorComponent < 0 || $colorComponent > 255) {
|
||||
throw new InvalidColorException("The RGB components must be between 0 and 255. Received: $colorComponent");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the color component to its corresponding hexadecimal value
|
||||
*
|
||||
* @param int $colorComponent Color component, 0 - 255
|
||||
* @return string Corresponding hexadecimal value, with a leading 0 if needed. E.g "0f", "2d"
|
||||
*/
|
||||
protected static function convertColorComponentToHex($colorComponent)
|
||||
{
|
||||
return str_pad(dechex($colorComponent), 2, '0', STR_PAD_LEFT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the ARGB color of the given RGB color,
|
||||
* assuming that alpha value is always 1.
|
||||
*
|
||||
* @param string $rgbColor RGB color like "FF08B2"
|
||||
* @return string ARGB color
|
||||
*/
|
||||
public static function toARGB($rgbColor)
|
||||
{
|
||||
return 'FF' . $rgbColor;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,379 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Writer\Common\Entity\Style;
|
||||
|
||||
/**
|
||||
* Class Style
|
||||
* Represents a style to be applied to a cell
|
||||
*
|
||||
* @package Box\Spout\Writer\Common\Entity\Style
|
||||
*/
|
||||
class Style
|
||||
{
|
||||
/** Default font values */
|
||||
const DEFAULT_FONT_SIZE = 11;
|
||||
const DEFAULT_FONT_COLOR = Color::BLACK;
|
||||
const DEFAULT_FONT_NAME = 'Arial';
|
||||
|
||||
/** @var int|null Style ID */
|
||||
private $id = null;
|
||||
|
||||
/** @var bool Whether the font should be bold */
|
||||
private $fontBold = false;
|
||||
/** @var bool Whether the bold property was set */
|
||||
private $hasSetFontBold = false;
|
||||
|
||||
/** @var bool Whether the font should be italic */
|
||||
private $fontItalic = false;
|
||||
/** @var bool Whether the italic property was set */
|
||||
private $hasSetFontItalic = false;
|
||||
|
||||
/** @var bool Whether the font should be underlined */
|
||||
private $fontUnderline = false;
|
||||
/** @var bool Whether the underline property was set */
|
||||
private $hasSetFontUnderline = false;
|
||||
|
||||
/** @var bool Whether the font should be struck through */
|
||||
private $fontStrikethrough = false;
|
||||
/** @var bool Whether the strikethrough property was set */
|
||||
private $hasSetFontStrikethrough = false;
|
||||
|
||||
/** @var int Font size */
|
||||
private $fontSize = self::DEFAULT_FONT_SIZE;
|
||||
/** @var bool Whether the font size property was set */
|
||||
private $hasSetFontSize = false;
|
||||
|
||||
/** @var string Font color */
|
||||
private $fontColor = self::DEFAULT_FONT_COLOR;
|
||||
/** @var bool Whether the font color property was set */
|
||||
private $hasSetFontColor = false;
|
||||
|
||||
/** @var string Font name */
|
||||
private $fontName = self::DEFAULT_FONT_NAME;
|
||||
/** @var bool Whether the font name property was set */
|
||||
private $hasSetFontName = false;
|
||||
|
||||
/** @var bool Whether specific font properties should be applied */
|
||||
private $shouldApplyFont = false;
|
||||
|
||||
/** @var bool Whether the text should wrap in the cell (useful for long or multi-lines text) */
|
||||
private $shouldWrapText = false;
|
||||
/** @var bool Whether the wrap text property was set */
|
||||
private $hasSetWrapText = false;
|
||||
|
||||
/** @var Border */
|
||||
private $border = null;
|
||||
|
||||
/** @var bool Whether border properties should be applied */
|
||||
private $shouldApplyBorder = false;
|
||||
|
||||
/** @var string Background color */
|
||||
private $backgroundColor = null;
|
||||
|
||||
/** @var bool */
|
||||
private $hasSetBackgroundColor = false;
|
||||
|
||||
|
||||
/**
|
||||
* @return int|null
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @return Style
|
||||
*/
|
||||
public function setId($id)
|
||||
{
|
||||
$this->id = $id;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Border
|
||||
*/
|
||||
public function getBorder()
|
||||
{
|
||||
return $this->border;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Border $border
|
||||
* @return Style
|
||||
*/
|
||||
public function setBorder(Border $border)
|
||||
{
|
||||
$this->shouldApplyBorder = true;
|
||||
$this->border = $border;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function shouldApplyBorder()
|
||||
{
|
||||
return $this->shouldApplyBorder;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isFontBold()
|
||||
{
|
||||
return $this->fontBold;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Style
|
||||
*/
|
||||
public function setFontBold()
|
||||
{
|
||||
$this->fontBold = true;
|
||||
$this->hasSetFontBold = true;
|
||||
$this->shouldApplyFont = true;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function hasSetFontBold()
|
||||
{
|
||||
return $this->hasSetFontBold;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isFontItalic()
|
||||
{
|
||||
return $this->fontItalic;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Style
|
||||
*/
|
||||
public function setFontItalic()
|
||||
{
|
||||
$this->fontItalic = true;
|
||||
$this->hasSetFontItalic = true;
|
||||
$this->shouldApplyFont = true;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function hasSetFontItalic()
|
||||
{
|
||||
return $this->hasSetFontItalic;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isFontUnderline()
|
||||
{
|
||||
return $this->fontUnderline;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Style
|
||||
*/
|
||||
public function setFontUnderline()
|
||||
{
|
||||
$this->fontUnderline = true;
|
||||
$this->hasSetFontUnderline = true;
|
||||
$this->shouldApplyFont = true;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function hasSetFontUnderline()
|
||||
{
|
||||
return $this->hasSetFontUnderline;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isFontStrikethrough()
|
||||
{
|
||||
return $this->fontStrikethrough;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Style
|
||||
*/
|
||||
public function setFontStrikethrough()
|
||||
{
|
||||
$this->fontStrikethrough = true;
|
||||
$this->hasSetFontStrikethrough = true;
|
||||
$this->shouldApplyFont = true;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function hasSetFontStrikethrough()
|
||||
{
|
||||
return $this->hasSetFontStrikethrough;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getFontSize()
|
||||
{
|
||||
return $this->fontSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $fontSize Font size, in pixels
|
||||
* @return Style
|
||||
*/
|
||||
public function setFontSize($fontSize)
|
||||
{
|
||||
$this->fontSize = $fontSize;
|
||||
$this->hasSetFontSize = true;
|
||||
$this->shouldApplyFont = true;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function hasSetFontSize()
|
||||
{
|
||||
return $this->hasSetFontSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getFontColor()
|
||||
{
|
||||
return $this->fontColor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the font color.
|
||||
*
|
||||
* @param string $fontColor ARGB color (@see Color)
|
||||
* @return Style
|
||||
*/
|
||||
public function setFontColor($fontColor)
|
||||
{
|
||||
$this->fontColor = $fontColor;
|
||||
$this->hasSetFontColor = true;
|
||||
$this->shouldApplyFont = true;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function hasSetFontColor()
|
||||
{
|
||||
return $this->hasSetFontColor;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getFontName()
|
||||
{
|
||||
return $this->fontName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $fontName Name of the font to use
|
||||
* @return Style
|
||||
*/
|
||||
public function setFontName($fontName)
|
||||
{
|
||||
$this->fontName = $fontName;
|
||||
$this->hasSetFontName = true;
|
||||
$this->shouldApplyFont = true;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function hasSetFontName()
|
||||
{
|
||||
return $this->hasSetFontName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function shouldWrapText()
|
||||
{
|
||||
return $this->shouldWrapText;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool|void $shouldWrap Should the text be wrapped
|
||||
* @return Style
|
||||
*/
|
||||
public function setShouldWrapText($shouldWrap = true)
|
||||
{
|
||||
$this->shouldWrapText = $shouldWrap;
|
||||
$this->hasSetWrapText = true;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function hasSetWrapText()
|
||||
{
|
||||
return $this->hasSetWrapText;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool Whether specific font properties should be applied
|
||||
*/
|
||||
public function shouldApplyFont()
|
||||
{
|
||||
return $this->shouldApplyFont;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the background color
|
||||
* @param string $color ARGB color (@see Color)
|
||||
* @return Style
|
||||
*/
|
||||
public function setBackgroundColor($color)
|
||||
{
|
||||
$this->hasSetBackgroundColor = true;
|
||||
$this->backgroundColor = $color;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getBackgroundColor()
|
||||
{
|
||||
return $this->backgroundColor;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return bool Whether the background color should be applied
|
||||
*/
|
||||
public function shouldApplyBackgroundColor()
|
||||
{
|
||||
return $this->hasSetBackgroundColor;
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user