Refactor table and font styles to reduce duplication
This commit is contained in:
parent
abbf60a3be
commit
1c3735fc08
|
|
@ -17,7 +17,6 @@ tools:
|
|||
enabled: true
|
||||
timeout: 900
|
||||
php_sim:
|
||||
enabled: true
|
||||
min_mass: 40
|
||||
php_pdepend: true
|
||||
php_analyzer: true
|
||||
|
|
|
|||
|
|
@ -404,10 +404,7 @@ class Font extends AbstractStyle
|
|||
*/
|
||||
public function setSuperScript($value = true)
|
||||
{
|
||||
$this->superScript = $this->setBoolVal($value, $this->superScript);
|
||||
$this->toggleFalse($this->subScript, $this->superScript);
|
||||
|
||||
return $this;
|
||||
return $this->setPairedProperty($this->superScript, $this->subScript, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -428,10 +425,7 @@ class Font extends AbstractStyle
|
|||
*/
|
||||
public function setSubScript($value = true)
|
||||
{
|
||||
$this->subScript = $this->setBoolVal($value, $this->subScript);
|
||||
$this->toggleFalse($this->subScript, $this->superScript);
|
||||
|
||||
return $this;
|
||||
return $this->setPairedProperty($this->subScript, $this->superScript, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -452,10 +446,7 @@ class Font extends AbstractStyle
|
|||
*/
|
||||
public function setStrikethrough($value = true)
|
||||
{
|
||||
$this->strikethrough = $this->setBoolVal($value, $this->strikethrough);
|
||||
$this->toggleFalse($this->doubleStrikethrough, $this->strikethrough);
|
||||
|
||||
return $this;
|
||||
return $this->setPairedProperty($this->strikethrough, $this->doubleStrikethrough, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -476,10 +467,7 @@ class Font extends AbstractStyle
|
|||
*/
|
||||
public function setDoubleStrikethrough($value = true)
|
||||
{
|
||||
$this->doubleStrikethrough = $this->setBoolVal($value, $this->doubleStrikethrough);
|
||||
$this->toggleFalse($this->strikethrough, $this->doubleStrikethrough);
|
||||
|
||||
return $this;
|
||||
return $this->setPairedProperty($this->doubleStrikethrough, $this->strikethrough, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -500,10 +488,7 @@ class Font extends AbstractStyle
|
|||
*/
|
||||
public function setSmallCaps($value = true)
|
||||
{
|
||||
$this->smallCaps = $this->setBoolVal($value, $this->smallCaps);
|
||||
$this->toggleFalse($this->allCaps, $this->smallCaps);
|
||||
|
||||
return $this;
|
||||
return $this->setPairedProperty($this->smallCaps, $this->allCaps, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -524,10 +509,7 @@ class Font extends AbstractStyle
|
|||
*/
|
||||
public function setAllCaps($value = true)
|
||||
{
|
||||
$this->allCaps = $this->setBoolVal($value, $this->allCaps);
|
||||
$this->toggleFalse($this->smallCaps, $this->allCaps);
|
||||
|
||||
return $this;
|
||||
return $this->setPairedProperty($this->allCaps, $this->smallCaps, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -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 $sourceValue
|
||||
* @param bool $property
|
||||
* @param bool $pair
|
||||
* @param bool $value
|
||||
* @return self
|
||||
*/
|
||||
private function toggleFalse(&$target, $sourceValue)
|
||||
private function setPairedProperty(&$property, &$pairProperty, $value)
|
||||
{
|
||||
if ($sourceValue == true) {
|
||||
$target = false;
|
||||
$property = $this->setBoolVal($value, $property);
|
||||
if ($value == true) {
|
||||
$pairProperty = false;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -124,6 +124,7 @@ class Table extends Border
|
|||
{
|
||||
$this->alignment = new Alignment();
|
||||
|
||||
// Clone first row from table style, but with certain properties disabled
|
||||
if ($firstRowStyle !== null && is_array($firstRowStyle)) {
|
||||
$this->firstRow = clone $this;
|
||||
unset($this->firstRow->firstRow);
|
||||
|
|
@ -257,7 +258,7 @@ class Table extends Border
|
|||
*/
|
||||
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)
|
||||
{
|
||||
$this->borderInsideHSize = $this->setNumericVal($value, $this->borderInsideHSize);
|
||||
|
||||
return $this;
|
||||
return $this->setTableOnlyProperty('borderInsideHSize', $value);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -280,7 +279,7 @@ class Table extends Border
|
|||
*/
|
||||
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)
|
||||
{
|
||||
$this->borderInsideHColor = $value ;
|
||||
|
||||
return $this;
|
||||
return $this->setTableOnlyProperty('borderInsideHColor', $value, false);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -303,7 +300,7 @@ class Table extends Border
|
|||
*/
|
||||
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)
|
||||
{
|
||||
$this->borderInsideVSize = $this->setNumericVal($value, $this->borderInsideVSize);
|
||||
|
||||
return $this;
|
||||
return $this->setTableOnlyProperty('borderInsideVSize', $value);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -326,7 +321,7 @@ class Table extends Border
|
|||
*/
|
||||
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)
|
||||
{
|
||||
$this->borderInsideVColor = $value;
|
||||
|
||||
return $this;
|
||||
return $this->setTableOnlyProperty('borderInsideVColor', $value, false);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -349,7 +342,7 @@ class Table extends Border
|
|||
*/
|
||||
public function getCellMarginTop()
|
||||
{
|
||||
return $this->cellMarginTop;
|
||||
return $this->getTableOnlyProperty('cellMarginTop');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -360,9 +353,7 @@ class Table extends Border
|
|||
*/
|
||||
public function setCellMarginTop($value = null)
|
||||
{
|
||||
$this->cellMarginTop = $this->setNumericVal($value, $this->cellMarginTop);
|
||||
|
||||
return $this;
|
||||
return $this->setTableOnlyProperty('cellMarginTop', $value);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -372,7 +363,7 @@ class Table extends Border
|
|||
*/
|
||||
public function getCellMarginLeft()
|
||||
{
|
||||
return $this->cellMarginLeft;
|
||||
return $this->getTableOnlyProperty('cellMarginLeft');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -383,9 +374,7 @@ class Table extends Border
|
|||
*/
|
||||
public function setCellMarginLeft($value = null)
|
||||
{
|
||||
$this->cellMarginLeft = $this->setNumericVal($value, $this->cellMarginLeft);
|
||||
|
||||
return $this;
|
||||
return $this->setTableOnlyProperty('cellMarginLeft', $value);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -395,7 +384,7 @@ class Table extends Border
|
|||
*/
|
||||
public function getCellMarginRight()
|
||||
{
|
||||
return $this->cellMarginRight;
|
||||
return $this->getTableOnlyProperty('cellMarginRight');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -406,9 +395,7 @@ class Table extends Border
|
|||
*/
|
||||
public function setCellMarginRight($value = null)
|
||||
{
|
||||
$this->cellMarginRight = $this->setNumericVal($value, $this->cellMarginRight);
|
||||
|
||||
return $this;
|
||||
return $this->setTableOnlyProperty('cellMarginRight', $value);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -418,7 +405,7 @@ class Table extends Border
|
|||
*/
|
||||
public function getCellMarginBottom()
|
||||
{
|
||||
return $this->cellMarginBottom;
|
||||
return $this->getTableOnlyProperty('cellMarginBottom');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -429,9 +416,7 @@ class Table extends Border
|
|||
*/
|
||||
public function setCellMarginBottom($value = null)
|
||||
{
|
||||
$this->cellMarginBottom = $this->setNumericVal($value, $this->cellMarginBottom);
|
||||
|
||||
return $this;
|
||||
return $this->setTableOnlyProperty('cellMarginBottom', $value);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -569,4 +554,46 @@ class Table extends Border
|
|||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -49,22 +49,19 @@ class StylesTest extends \PHPUnit_Framework_TestCase
|
|||
$rStyle = array('size' => 20);
|
||||
$tStyle = array(
|
||||
'bgColor' => 'FF0000',
|
||||
'cellMarginTop' => 120,
|
||||
'cellMarginBottom' => 120,
|
||||
'cellMarginLeft' => 120,
|
||||
'cellMarginRight' => 120,
|
||||
'borderTopSize' => 120,
|
||||
'borderBottomSize' => 120,
|
||||
'borderLeftSize' => 120,
|
||||
'borderRightSize' => 120,
|
||||
'borderInsideHSize' => 120,
|
||||
'borderInsideVSize' => 120,
|
||||
'cellMargin' => 120,
|
||||
'borderSize' => 120,
|
||||
);
|
||||
$firstRowStyle = array(
|
||||
'bgColor' => '0000FF',
|
||||
'borderSize' => 120,
|
||||
'borderColor' => '00FF00',
|
||||
);
|
||||
$phpWord->setDefaultParagraphStyle($pStyle);
|
||||
$phpWord->addParagraphStyle('Base Style', $pBase);
|
||||
$phpWord->addParagraphStyle('New Style', $pNew);
|
||||
$phpWord->addFontStyle('New Style', $rStyle, $pStyle);
|
||||
$phpWord->addTableStyle('Table Style', $tStyle, $tStyle);
|
||||
$phpWord->addTableStyle('Table Style', $tStyle, $firstRowStyle);
|
||||
$phpWord->addTitleStyle(1, $rStyle, $pStyle);
|
||||
$doc = TestHelperDOCX::getDocument($phpWord);
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue