Skip to content

Commit cf3132a

Browse files
ndenchtroosan
authored andcommitted
Add ability to pass a Style object to a Section element (#1416)
* Add ability to pass a Style object to a Section * Fix typo * update changelog
1 parent c2b54cc commit cf3132a

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ v0.16.0 (xx dec 2018)
77
----------------------
88
### Added
99
- Add setting Chart Title and Legend visibility @Tom-Magill #1433
10+
- Add ability to pass a Style object in Section constructor @ndench #1416
1011
- Add support for hidden text @Alexmg86 #1527
1112

1213
### Fixed

src/PhpWord/Element/Section.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,16 @@ class Section extends AbstractContainer
5959
* Create new instance
6060
*
6161
* @param int $sectionCount
62-
* @param array $style
62+
* @param null|array|\PhpOffice\PhpWord\Style $style
6363
*/
6464
public function __construct($sectionCount, $style = null)
6565
{
6666
$this->sectionId = $sectionCount;
6767
$this->setDocPart($this->container, $this->sectionId);
68-
$this->style = new SectionStyle();
69-
$this->setStyle($style);
68+
if (null === $style) {
69+
$style = new SectionStyle();
70+
}
71+
$this->style = $this->setNewStyle(new SectionStyle(), $style);
7072
}
7173

7274
/**

tests/PhpWord/Element/SectionTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
use PhpOffice\PhpWord\PhpWord;
2121
use PhpOffice\PhpWord\Style;
22+
use PhpOffice\PhpWord\Style\Section as SectionStyle;
2223

2324
/**
2425
* @covers \PhpOffice\PhpWord\Element\Section
@@ -27,6 +28,27 @@
2728
*/
2829
class SectionTest extends \PHPUnit\Framework\TestCase
2930
{
31+
public function testConstructorWithDefaultStyle()
32+
{
33+
$section = new Section(0);
34+
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Section', $section->getStyle());
35+
}
36+
37+
public function testConstructorWithArrayStyle()
38+
{
39+
$section = new Section(0, array('orientation' => 'landscape'));
40+
$style = $section->getStyle();
41+
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Section', $style);
42+
$this->assertEquals('landscape', $style->getOrientation());
43+
}
44+
45+
public function testConstructorWithObjectStyle()
46+
{
47+
$style = new SectionStyle();
48+
$section = new Section(0, $style);
49+
$this->assertSame($style, $section->getStyle());
50+
}
51+
3052
/**
3153
* @covers ::setStyle
3254
*/

tests/PhpWord/Writer/Word2007/ElementTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,4 +492,19 @@ public function testTitleAndHeading()
492492
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p[2]/w:pPr/w:pStyle'));
493493
$this->assertEquals('Heading1', $doc->getElementAttribute('/w:document/w:body/w:p[2]/w:pPr/w:pStyle', 'w:val'));
494494
}
495+
496+
/**
497+
* Test correct writing of text with ampersant in it
498+
*/
499+
public function testTextWithAmpersant()
500+
{
501+
\PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(true);
502+
$phpWord = new PhpWord();
503+
$section = $phpWord->addSection();
504+
$section->addText('this text contains an & (ampersant)');
505+
506+
$doc = TestHelperDOCX::getDocument($phpWord);
507+
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:r/w:t'));
508+
$this->assertEquals('this text contains an & (ampersant)', $doc->getElement('/w:document/w:body/w:p/w:r/w:t')->nodeValue);
509+
}
495510
}

0 commit comments

Comments
 (0)