Refactor table and font styles to reduce duplication

This commit is contained in:
Ivan Lanin 2014-05-15 11:46:28 +07:00
parent abbf60a3be
commit 1c3735fc08
4 changed files with 85 additions and 75 deletions

View File

@ -17,7 +17,6 @@ tools:
enabled: true enabled: true
timeout: 900 timeout: 900
php_sim: php_sim:
enabled: true
min_mass: 40 min_mass: 40
php_pdepend: true php_pdepend: true
php_analyzer: true php_analyzer: true

View File

@ -404,10 +404,7 @@ class Font extends AbstractStyle
*/ */
public function setSuperScript($value = true) public function setSuperScript($value = true)
{ {
$this->superScript = $this->setBoolVal($value, $this->superScript); return $this->setPairedProperty($this->superScript, $this->subScript, $value);
$this->toggleFalse($this->subScript, $this->superScript);
return $this;
} }
/** /**
@ -428,10 +425,7 @@ class Font extends AbstractStyle
*/ */
public function setSubScript($value = true) public function setSubScript($value = true)
{ {
$this->subScript = $this->setBoolVal($value, $this->subScript); return $this->setPairedProperty($this->subScript, $this->superScript, $value);
$this->toggleFalse($this->subScript, $this->superScript);
return $this;
} }
/** /**
@ -452,10 +446,7 @@ class Font extends AbstractStyle
*/ */
public function setStrikethrough($value = true) public function setStrikethrough($value = true)
{ {
$this->strikethrough = $this->setBoolVal($value, $this->strikethrough); return $this->setPairedProperty($this->strikethrough, $this->doubleStrikethrough, $value);
$this->toggleFalse($this->doubleStrikethrough, $this->strikethrough);
return $this;
} }
/** /**
@ -476,10 +467,7 @@ class Font extends AbstractStyle
*/ */
public function setDoubleStrikethrough($value = true) public function setDoubleStrikethrough($value = true)
{ {
$this->doubleStrikethrough = $this->setBoolVal($value, $this->doubleStrikethrough); return $this->setPairedProperty($this->doubleStrikethrough, $this->strikethrough, $value);
$this->toggleFalse($this->strikethrough, $this->doubleStrikethrough);
return $this;
} }
/** /**
@ -500,10 +488,7 @@ class Font extends AbstractStyle
*/ */
public function setSmallCaps($value = true) public function setSmallCaps($value = true)
{ {
$this->smallCaps = $this->setBoolVal($value, $this->smallCaps); return $this->setPairedProperty($this->smallCaps, $this->allCaps, $value);
$this->toggleFalse($this->allCaps, $this->smallCaps);
return $this;
} }
/** /**
@ -524,10 +509,7 @@ class Font extends AbstractStyle
*/ */
public function setAllCaps($value = true) public function setAllCaps($value = true)
{ {
$this->allCaps = $this->setBoolVal($value, $this->allCaps); return $this->setPairedProperty($this->allCaps, $this->smallCaps, $value);
$this->toggleFalse($this->smallCaps, $this->allCaps);
return $this;
} }
/** /**
@ -648,16 +630,21 @@ class Font extends AbstractStyle
} }
/** /**
* Toggle $target property to false when $source true * Set $property value and set $pairProperty = false when $value = true
* *
* @param bool|null $target Target property * @param bool $property
* @param bool $sourceValue * @param bool $pair
* @param bool $value
* @return self
*/ */
private function toggleFalse(&$target, $sourceValue) private function setPairedProperty(&$property, &$pairProperty, $value)
{ {
if ($sourceValue == true) { $property = $this->setBoolVal($value, $property);
$target = false; if ($value == true) {
$pairProperty = false;
} }
return $this;
} }
/** /**

View File

@ -124,6 +124,7 @@ class Table extends Border
{ {
$this->alignment = new Alignment(); $this->alignment = new Alignment();
// Clone first row from table style, but with certain properties disabled
if ($firstRowStyle !== null && is_array($firstRowStyle)) { if ($firstRowStyle !== null && is_array($firstRowStyle)) {
$this->firstRow = clone $this; $this->firstRow = clone $this;
unset($this->firstRow->firstRow); unset($this->firstRow->firstRow);
@ -257,7 +258,7 @@ class Table extends Border
*/ */
public function getBorderInsideHSize() public function getBorderInsideHSize()
{ {
return isset($this->borderInsideHSize) ? $this->borderInsideHSize : null; return $this->getTableOnlyProperty('borderInsideHSize');
} }
/** /**
@ -268,9 +269,7 @@ class Table extends Border
*/ */
public function setBorderInsideHSize($value = null) public function setBorderInsideHSize($value = null)
{ {
$this->borderInsideHSize = $this->setNumericVal($value, $this->borderInsideHSize); return $this->setTableOnlyProperty('borderInsideHSize', $value);
return $this;
} }
/** /**
@ -280,7 +279,7 @@ class Table extends Border
*/ */
public function getBorderInsideHColor() public function getBorderInsideHColor()
{ {
return isset($this->borderInsideHColor) ? $this->borderInsideHColor : null; return $this->getTableOnlyProperty('borderInsideHColor');
} }
/** /**
@ -291,9 +290,7 @@ class Table extends Border
*/ */
public function setBorderInsideHColor($value = null) public function setBorderInsideHColor($value = null)
{ {
$this->borderInsideHColor = $value ; return $this->setTableOnlyProperty('borderInsideHColor', $value, false);
return $this;
} }
/** /**
@ -303,7 +300,7 @@ class Table extends Border
*/ */
public function getBorderInsideVSize() public function getBorderInsideVSize()
{ {
return isset($this->borderInsideVSize) ? $this->borderInsideVSize : null; return $this->getTableOnlyProperty('borderInsideVSize');
} }
/** /**
@ -314,9 +311,7 @@ class Table extends Border
*/ */
public function setBorderInsideVSize($value = null) public function setBorderInsideVSize($value = null)
{ {
$this->borderInsideVSize = $this->setNumericVal($value, $this->borderInsideVSize); return $this->setTableOnlyProperty('borderInsideVSize', $value);
return $this;
} }
/** /**
@ -326,7 +321,7 @@ class Table extends Border
*/ */
public function getBorderInsideVColor() public function getBorderInsideVColor()
{ {
return isset($this->borderInsideVColor) ? $this->borderInsideVColor : null; return $this->getTableOnlyProperty('borderInsideVColor');
} }
/** /**
@ -337,9 +332,7 @@ class Table extends Border
*/ */
public function setBorderInsideVColor($value = null) public function setBorderInsideVColor($value = null)
{ {
$this->borderInsideVColor = $value; return $this->setTableOnlyProperty('borderInsideVColor', $value, false);
return $this;
} }
/** /**
@ -349,7 +342,7 @@ class Table extends Border
*/ */
public function getCellMarginTop() public function getCellMarginTop()
{ {
return $this->cellMarginTop; return $this->getTableOnlyProperty('cellMarginTop');
} }
/** /**
@ -360,9 +353,7 @@ class Table extends Border
*/ */
public function setCellMarginTop($value = null) public function setCellMarginTop($value = null)
{ {
$this->cellMarginTop = $this->setNumericVal($value, $this->cellMarginTop); return $this->setTableOnlyProperty('cellMarginTop', $value);
return $this;
} }
/** /**
@ -372,7 +363,7 @@ class Table extends Border
*/ */
public function getCellMarginLeft() public function getCellMarginLeft()
{ {
return $this->cellMarginLeft; return $this->getTableOnlyProperty('cellMarginLeft');
} }
/** /**
@ -383,9 +374,7 @@ class Table extends Border
*/ */
public function setCellMarginLeft($value = null) public function setCellMarginLeft($value = null)
{ {
$this->cellMarginLeft = $this->setNumericVal($value, $this->cellMarginLeft); return $this->setTableOnlyProperty('cellMarginLeft', $value);
return $this;
} }
/** /**
@ -395,7 +384,7 @@ class Table extends Border
*/ */
public function getCellMarginRight() public function getCellMarginRight()
{ {
return $this->cellMarginRight; return $this->getTableOnlyProperty('cellMarginRight');
} }
/** /**
@ -406,9 +395,7 @@ class Table extends Border
*/ */
public function setCellMarginRight($value = null) public function setCellMarginRight($value = null)
{ {
$this->cellMarginRight = $this->setNumericVal($value, $this->cellMarginRight); return $this->setTableOnlyProperty('cellMarginRight', $value);
return $this;
} }
/** /**
@ -418,7 +405,7 @@ class Table extends Border
*/ */
public function getCellMarginBottom() public function getCellMarginBottom()
{ {
return $this->cellMarginBottom; return $this->getTableOnlyProperty('cellMarginBottom');
} }
/** /**
@ -429,9 +416,7 @@ class Table extends Border
*/ */
public function setCellMarginBottom($value = null) public function setCellMarginBottom($value = null)
{ {
$this->cellMarginBottom = $this->setNumericVal($value, $this->cellMarginBottom); return $this->setTableOnlyProperty('cellMarginBottom', $value);
return $this;
} }
/** /**
@ -569,4 +554,46 @@ class Table extends Border
return $this; return $this;
} }
/**
* Get table style only property by checking if firstRow is set
*
* This is necessary since firstRow style is cloned from table style but
* without certain properties activated, e.g. margins
*
* @param string $property
* @return int|string|null
*/
private function getTableOnlyProperty($property)
{
if (isset($this->firstRow)) {
return $this->$property;
}
return null;
}
/**
* Set table style only property by checking if firstRow is set
*
* This is necessary since firstRow style is cloned from table style but
* without certain properties activated, e.g. margins
*
* @param string $property
* @param int|string $value
* @param bool $isNumeric
* @return self
*/
private function setTableOnlyProperty($property, $value, $isNumeric = true)
{
if (isset($this->firstRow)) {
if ($isNumeric) {
$this->$property = $this->setNumericVal($value, $this->$property);
} else {
$this->$property = $value;
}
}
return $this;
}
} }

View File

@ -49,22 +49,19 @@ class StylesTest extends \PHPUnit_Framework_TestCase
$rStyle = array('size' => 20); $rStyle = array('size' => 20);
$tStyle = array( $tStyle = array(
'bgColor' => 'FF0000', 'bgColor' => 'FF0000',
'cellMarginTop' => 120, 'cellMargin' => 120,
'cellMarginBottom' => 120, 'borderSize' => 120,
'cellMarginLeft' => 120, );
'cellMarginRight' => 120, $firstRowStyle = array(
'borderTopSize' => 120, 'bgColor' => '0000FF',
'borderBottomSize' => 120, 'borderSize' => 120,
'borderLeftSize' => 120, 'borderColor' => '00FF00',
'borderRightSize' => 120,
'borderInsideHSize' => 120,
'borderInsideVSize' => 120,
); );
$phpWord->setDefaultParagraphStyle($pStyle); $phpWord->setDefaultParagraphStyle($pStyle);
$phpWord->addParagraphStyle('Base Style', $pBase); $phpWord->addParagraphStyle('Base Style', $pBase);
$phpWord->addParagraphStyle('New Style', $pNew); $phpWord->addParagraphStyle('New Style', $pNew);
$phpWord->addFontStyle('New Style', $rStyle, $pStyle); $phpWord->addFontStyle('New Style', $rStyle, $pStyle);
$phpWord->addTableStyle('Table Style', $tStyle, $tStyle); $phpWord->addTableStyle('Table Style', $tStyle, $firstRowStyle);
$phpWord->addTitleStyle(1, $rStyle, $pStyle); $phpWord->addTitleStyle(1, $rStyle, $pStyle);
$doc = TestHelperDOCX::getDocument($phpWord); $doc = TestHelperDOCX::getDocument($phpWord);