Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/styles.rst
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ See ``\PhpOffice\PhpWord\SimpleType\Jc`` class for the details.
- ``spaceAfter``. Space after paragraph.
- ``tabs``. Set of custom tab stops.
- ``widowControl``. Allow first/last line to display on a separate page, *true* or *false*.
- ``contextualSpacing``. Ignore Spacing Above and Below When Using Identical Styles, *true* or *false*.

.. _table-style:

Expand Down
31 changes: 31 additions & 0 deletions src/PhpWord/Style/Paragraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,13 @@ class Paragraph extends Border
* @var \PhpOffice\PhpWord\Style\Shading
*/
private $shading;

/**
* Ignore Spacing Above and Below When Using Identical Styles
*
* @var bool
*/
private $contextualSpacing = false;

/**
* Set Style value
Expand Down Expand Up @@ -208,6 +215,7 @@ public function getStyleValues()
),
'tabs' => $this->getTabs(),
'shading' => $this->getShading(),
'contextualSpacing' => $this->hasContextualSpacing(),
);

return $styles;
Expand Down Expand Up @@ -731,4 +739,27 @@ public function setShading($value = null)

return $this;
}

/**
* Get contextualSpacing
*
* @return bool
*/
public function hasContextualSpacing()
{
return $this->contextualSpacing;
}

/**
* Set contextualSpacing
*
* @param bool $contextualSpacing
* @return self
*/
public function setContextualSpacing($contextualSpacing)
{
$this->contextualSpacing = $contextualSpacing;

return $this;
}
}
3 changes: 3 additions & 0 deletions src/PhpWord/Writer/Word2007/Style/Paragraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ private function writeStyle()
}
$xmlWriter->endElement();
}

//Paragraph contextualSpacing
$xmlWriter->writeElementIf($styles['contextualSpacing'] === true, 'w:contextualSpacing');

// Child style: alignment, indentation, spacing, and shading
$this->writeChildStyle($xmlWriter, 'Indentation', $styles['indentation']);
Expand Down
54 changes: 34 additions & 20 deletions tests/PhpWord/Style/ParagraphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ public function testSetStyleValueWithNullOrEmpty()
$object = new Paragraph();

$attributes = array(
'widowControl' => true,
'keepNext' => false,
'keepLines' => false,
'pageBreakBefore' => false,
'widowControl' => true,
'keepNext' => false,
'keepLines' => false,
'pageBreakBefore' => false,
'contextualSpacing' => false,
);
foreach ($attributes as $key => $default) {
$get = "get{$key}";
$get = $this->findGetter($key, $default, $object);
$object->setStyleValue($key, null);
$this->assertEquals($default, $object->$get());
$object->setStyleValue($key, '');
Expand All @@ -65,22 +66,23 @@ public function testSetStyleValueNormal()
$object = new Paragraph();

$attributes = array(
'spaceAfter' => 240,
'spaceBefore' => 240,
'indent' => 1,
'hanging' => 1,
'spacing' => 120,
'basedOn' => 'Normal',
'next' => 'Normal',
'numStyle' => 'numStyle',
'numLevel' => 1,
'widowControl' => false,
'keepNext' => true,
'keepLines' => true,
'pageBreakBefore' => true,
'spaceAfter' => 240,
'spaceBefore' => 240,
'indent' => 1,
'hanging' => 1,
'spacing' => 120,
'basedOn' => 'Normal',
'next' => 'Normal',
'numStyle' => 'numStyle',
'numLevel' => 1,
'widowControl' => false,
'keepNext' => true,
'keepLines' => true,
'pageBreakBefore' => true,
'contextualSpacing' => true,
);
foreach ($attributes as $key => $value) {
$get = "get{$key}";
$get = $this->findGetter($key, $value, $object);
$object->setStyleValue("$key", $value);
if ('indent' == $key || 'hanging' == $key) {
$value = $value * 720;
Expand All @@ -91,6 +93,18 @@ public function testSetStyleValueNormal()
}
}

private function findGetter($key, $value, $object)
{
if (is_bool($value)) {
if (method_exists($object, "is{$key}")) {
return "is{$key}";
} else if (method_exists($object, "has{$key}")) {
return "has{$key}";
}
}
return "get{$key}";
}

/**
* Test get null style value
*/
Expand All @@ -100,7 +114,7 @@ public function testGetNullStyleValue()

$attributes = array('spacing', 'indent', 'hanging', 'spaceBefore', 'spaceAfter');
foreach ($attributes as $key) {
$get = "get{$key}";
$get = $this->findGetter($key, null, $object);
$this->assertNull($object->$get());
}
}
Expand Down