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

@@ -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