Skip to content

Commit 0f649f3

Browse files
committed
Add test & update documentation
1 parent d5dbfb9 commit 0f649f3

File tree

3 files changed

+44
-24
lines changed

3 files changed

+44
-24
lines changed

docs/styles.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ See ``\PhpOffice\PhpWord\SimpleType\Jc`` class for the details.
7777
- ``spaceAfter``. Space after paragraph.
7878
- ``tabs``. Set of custom tab stops.
7979
- ``widowControl``. Allow first/last line to display on a separate page, *true* or *false*.
80+
- ``contextualSpacing``. Ignore Spacing Above and Below When Using Identical Styles, *true* or *false*.
8081

8182
.. _table-style:
8283

src/PhpWord/Style/Paragraph.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,11 @@ class Paragraph extends Border
159159
private $shading;
160160

161161
/**
162-
* Do not add an interval between paragraphs of the same style
162+
* Ignore Spacing Above and Below When Using Identical Styles
163163
*
164164
* @var bool
165165
*/
166-
private $contextualSpacing = true;
166+
private $contextualSpacing = false;
167167

168168
/**
169169
* Set Style value
@@ -215,7 +215,7 @@ public function getStyleValues()
215215
),
216216
'tabs' => $this->getTabs(),
217217
'shading' => $this->getShading(),
218-
'contextualSpacing' => $this->getContextualSpacing(),
218+
'contextualSpacing' => $this->hasContextualSpacing(),
219219
);
220220

221221
return $styles;
@@ -741,15 +741,20 @@ public function setShading($value = null)
741741
}
742742

743743
/**
744+
* Get contextualSpacing
745+
*
744746
* @return bool
745747
*/
746-
public function getContextualSpacing()
748+
public function hasContextualSpacing()
747749
{
748750
return $this->contextualSpacing;
749751
}
750752

751753
/**
754+
* Set contextualSpacing
755+
*
752756
* @param bool $contextualSpacing
757+
* @return self
753758
*/
754759
public function setContextualSpacing($contextualSpacing)
755760
{

tests/PhpWord/Style/ParagraphTest.php

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,14 @@ public function testSetStyleValueWithNullOrEmpty()
4343
$object = new Paragraph();
4444

4545
$attributes = array(
46-
'widowControl' => true,
47-
'keepNext' => false,
48-
'keepLines' => false,
49-
'pageBreakBefore' => false,
46+
'widowControl' => true,
47+
'keepNext' => false,
48+
'keepLines' => false,
49+
'pageBreakBefore' => false,
50+
'contextualSpacing' => false,
5051
);
5152
foreach ($attributes as $key => $default) {
52-
$get = "get{$key}";
53+
$get = $this->findGetter($key, $default, $object);
5354
$object->setStyleValue($key, null);
5455
$this->assertEquals($default, $object->$get());
5556
$object->setStyleValue($key, '');
@@ -65,22 +66,23 @@ public function testSetStyleValueNormal()
6566
$object = new Paragraph();
6667

6768
$attributes = array(
68-
'spaceAfter' => 240,
69-
'spaceBefore' => 240,
70-
'indent' => 1,
71-
'hanging' => 1,
72-
'spacing' => 120,
73-
'basedOn' => 'Normal',
74-
'next' => 'Normal',
75-
'numStyle' => 'numStyle',
76-
'numLevel' => 1,
77-
'widowControl' => false,
78-
'keepNext' => true,
79-
'keepLines' => true,
80-
'pageBreakBefore' => true,
69+
'spaceAfter' => 240,
70+
'spaceBefore' => 240,
71+
'indent' => 1,
72+
'hanging' => 1,
73+
'spacing' => 120,
74+
'basedOn' => 'Normal',
75+
'next' => 'Normal',
76+
'numStyle' => 'numStyle',
77+
'numLevel' => 1,
78+
'widowControl' => false,
79+
'keepNext' => true,
80+
'keepLines' => true,
81+
'pageBreakBefore' => true,
82+
'contextualSpacing' => true,
8183
);
8284
foreach ($attributes as $key => $value) {
83-
$get = "get{$key}";
85+
$get = $this->findGetter($key, $value, $object);
8486
$object->setStyleValue("$key", $value);
8587
if ('indent' == $key || 'hanging' == $key) {
8688
$value = $value * 720;
@@ -91,6 +93,18 @@ public function testSetStyleValueNormal()
9193
}
9294
}
9395

96+
private function findGetter($key, $value, $object)
97+
{
98+
if (is_bool($value)) {
99+
if (method_exists($object, "is{$key}")) {
100+
return "is{$key}";
101+
} else if (method_exists($object, "has{$key}")) {
102+
return "has{$key}";
103+
}
104+
}
105+
return "get{$key}";
106+
}
107+
94108
/**
95109
* Test get null style value
96110
*/
@@ -100,7 +114,7 @@ public function testGetNullStyleValue()
100114

101115
$attributes = array('spacing', 'indent', 'hanging', 'spaceBefore', 'spaceAfter');
102116
foreach ($attributes as $key) {
103-
$get = "get{$key}";
117+
$get = $this->findGetter($key, null, $object);
104118
$this->assertNull($object->$get());
105119
}
106120
}

0 commit comments

Comments
 (0)