From 0f649f3f376cbeff390e21945d6b03cfb0aca7e6 Mon Sep 17 00:00:00 2001 From: troosan Date: Sat, 1 Jul 2017 22:51:53 +0200 Subject: [PATCH] Add test & update documentation --- docs/styles.rst | 1 + src/PhpWord/Style/Paragraph.php | 13 +++++-- tests/PhpWord/Style/ParagraphTest.php | 54 +++++++++++++++++---------- 3 files changed, 44 insertions(+), 24 deletions(-) diff --git a/docs/styles.rst b/docs/styles.rst index b71059a6..4f8a53fe 100644 --- a/docs/styles.rst +++ b/docs/styles.rst @@ -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: diff --git a/src/PhpWord/Style/Paragraph.php b/src/PhpWord/Style/Paragraph.php index ec23dce3..a9b53b2b 100644 --- a/src/PhpWord/Style/Paragraph.php +++ b/src/PhpWord/Style/Paragraph.php @@ -159,11 +159,11 @@ class Paragraph extends Border private $shading; /** - * Do not add an interval between paragraphs of the same style + * Ignore Spacing Above and Below When Using Identical Styles * * @var bool */ - private $contextualSpacing = true; + private $contextualSpacing = false; /** * Set Style value @@ -215,7 +215,7 @@ class Paragraph extends Border ), 'tabs' => $this->getTabs(), 'shading' => $this->getShading(), - 'contextualSpacing' => $this->getContextualSpacing(), + 'contextualSpacing' => $this->hasContextualSpacing(), ); return $styles; @@ -741,15 +741,20 @@ class Paragraph extends Border } /** + * Get contextualSpacing + * * @return bool */ - public function getContextualSpacing() + public function hasContextualSpacing() { return $this->contextualSpacing; } /** + * Set contextualSpacing + * * @param bool $contextualSpacing + * @return self */ public function setContextualSpacing($contextualSpacing) { diff --git a/tests/PhpWord/Style/ParagraphTest.php b/tests/PhpWord/Style/ParagraphTest.php index c0096b0b..86d6e896 100644 --- a/tests/PhpWord/Style/ParagraphTest.php +++ b/tests/PhpWord/Style/ParagraphTest.php @@ -43,13 +43,14 @@ class ParagraphTest extends \PHPUnit_Framework_TestCase $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, ''); @@ -65,22 +66,23 @@ class ParagraphTest extends \PHPUnit_Framework_TestCase $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; @@ -91,6 +93,18 @@ class ParagraphTest extends \PHPUnit_Framework_TestCase } } + 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 */ @@ -100,7 +114,7 @@ class ParagraphTest extends \PHPUnit_Framework_TestCase $attributes = array('spacing', 'indent', 'hanging', 'spaceBefore', 'spaceAfter'); foreach ($attributes as $key) { - $get = "get{$key}"; + $get = $this->findGetter($key, null, $object); $this->assertNull($object->$get()); } }