Skip to content

Commit 44d2501

Browse files
committed
TextBreak: Allow font/paragraph styling for text break
1 parent 088caa2 commit 44d2501

File tree

14 files changed

+288
-34
lines changed

14 files changed

+288
-34
lines changed

Classes/PHPWord/Section.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,14 @@ public function addLink($linkSrc, $linkName = null, $styleFont = null, $stylePar
156156
/**
157157
* Add a TextBreak Element
158158
*
159-
* @param int $count
159+
* @param int $count
160+
* @param null|string|array|PHPWord_Style_Font $fontStyle
161+
* @param null|string|array|PHPWord_Style_Paragraph $paragraphStyle
160162
*/
161-
public function addTextBreak($count = 1)
163+
public function addTextBreak($count = 1, $fontStyle = null, $paragraphStyle = null)
162164
{
163165
for ($i = 1; $i <= $count; $i++) {
164-
$this->_elementCollection[] = new PHPWord_Section_TextBreak();
166+
$this->_elementCollection[] = new PHPWord_Section_TextBreak($fontStyle, $paragraphStyle);
165167
}
166168
}
167169

Classes/PHPWord/Section/Footer.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,16 @@ public function addText($text, $styleFont = null, $styleParagraph = null)
7979
}
8080

8181
/**
82-
* Add a TextBreak Element
82+
* Add TextBreak
8383
*
84-
* @param int $count
84+
* @param int $count
85+
* @param null|string|array|PHPWord_Style_Font $fontStyle
86+
* @param null|string|array|PHPWord_Style_Paragraph $paragraphStyle
8587
*/
86-
public function addTextBreak($count = 1)
88+
public function addTextBreak($count = 1, $fontStyle = null, $paragraphStyle = null)
8789
{
8890
for ($i = 1; $i <= $count; $i++) {
89-
$this->_elementCollection[] = new PHPWord_Section_TextBreak();
91+
$this->_elementCollection[] = new PHPWord_Section_TextBreak($fontStyle, $paragraphStyle);
9092
}
9193
}
9294

Classes/PHPWord/Section/Header.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,16 @@ public function addText($text, $styleFont = null, $styleParagraph = null)
108108
}
109109

110110
/**
111-
* Add a TextBreak Element
111+
* Add TextBreak
112112
*
113-
* @param int $count
113+
* @param int $count
114+
* @param null|string|array|PHPWord_Style_Font $fontStyle
115+
* @param null|string|array|PHPWord_Style_Paragraph $paragraphStyle
114116
*/
115-
public function addTextBreak($count = 1)
117+
public function addTextBreak($count = 1, $fontStyle = null, $paragraphStyle = null)
116118
{
117119
for ($i = 1; $i <= $count; $i++) {
118-
$this->_elementCollection[] = new PHPWord_Section_TextBreak();
120+
$this->_elementCollection[] = new PHPWord_Section_TextBreak($fontStyle, $paragraphStyle);
119121
}
120122
}
121123

Classes/PHPWord/Section/Table/Cell.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,17 @@ public function addLink($linkSrc, $linkName = null, $style = null)
146146
}
147147

148148
/**
149-
* Add a TextBreak Element
149+
* Add TextBreak
150150
*
151-
* @param int $count
151+
* @param int $count
152+
* @param null|string|array|PHPWord_Style_Font $fontStyle
153+
* @param null|string|array|PHPWord_Style_Paragraph $paragraphStyle
152154
*/
153-
public function addTextBreak()
155+
public function addTextBreak($count = 1, $fontStyle = null, $paragraphStyle = null)
154156
{
155-
$this->_elementCollection[] = new PHPWord_Section_TextBreak();
157+
for ($i = 1; $i <= $count; $i++) {
158+
$this->_elementCollection[] = new PHPWord_Section_TextBreak($fontStyle, $paragraphStyle);
159+
}
156160
}
157161

158162
/**

Classes/PHPWord/Section/TextBreak.php

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,91 @@
3030
*/
3131
class PHPWord_Section_TextBreak
3232
{
33+
/**
34+
* Paragraph style
35+
*
36+
* @var PHPWord_Style_Pagaraph
37+
*/
38+
private $paragraphStyle = null;
39+
40+
/**
41+
* Text style
42+
*
43+
* @var PHPWord_Style_Font
44+
*/
45+
private $fontStyle = null;
3346

3447
/**
3548
* Create a new TextBreak Element
3649
*/
37-
public function __construct()
50+
public function __construct($fontStyle = null, $paragraphStyle = null)
51+
{
52+
if (!is_null($paragraphStyle)) {
53+
$paragraphStyle = $this->setParagraphStyle($paragraphStyle);
54+
}
55+
if (!is_null($fontStyle)) {
56+
$this->setFontStyle($fontStyle, $paragraphStyle);
57+
}
58+
}
59+
60+
/**
61+
* Set Text style
62+
*
63+
* @param null|array|\PHPWord_Style_Font $style
64+
* @param null|array|\PHPWord_Style_Paragraph $paragraphStyle
65+
* @return PHPWord_Style_Font
66+
*/
67+
public function setFontStyle($style = null, $paragraphStyle = null)
68+
{
69+
if ($style instanceof PHPWord_Style_Font) {
70+
$this->fontStyle = $style;
71+
$this->setParagraphStyle($paragraphStyle);
72+
} elseif (is_array($style)) {
73+
$this->fontStyle = new PHPWord_Style_Font('text', $paragraphStyle);
74+
$this->fontStyle->setArrayStyle($style);
75+
} else {
76+
$this->fontStyle = $style;
77+
$this->setParagraphStyle($paragraphStyle);
78+
}
79+
return $this->fontStyle;
80+
}
81+
82+
/**
83+
* Get Text style
84+
*
85+
* @return PHPWord_Style_Font
86+
*/
87+
public function getFontStyle()
88+
{
89+
return $this->fontStyle;
90+
}
91+
92+
/**
93+
* Set Paragraph style
94+
*
95+
* @param null|array|\PHPWord_Style_Paragraph $style
96+
* @return null|\PHPWord_Style_Paragraph
97+
*/
98+
public function setParagraphStyle($style = null)
99+
{
100+
if (is_array($style)) {
101+
$this->paragraphStyle = new PHPWord_Style_Paragraph;
102+
$this->paragraphStyle->setArrayStyle($style);
103+
} elseif ($style instanceof PHPWord_Style_Paragraph) {
104+
$this->paragraphStyle = $style;
105+
} else {
106+
$this->paragraphStyle = $style;
107+
}
108+
return $this->paragraphStyle;
109+
}
110+
111+
/**
112+
* Get Paragraph style
113+
*
114+
* @return PHPWord_Style_Paragraph
115+
*/
116+
public function getParagraphStyle()
38117
{
118+
return $this->paragraphStyle;
39119
}
40120
}

Classes/PHPWord/Section/TextRun.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,16 @@ public function addImage($imageSrc, $style = null)
132132
}
133133

134134
/**
135-
* Add a Text Break
135+
* Add TextBreak
136136
*
137-
* @param int $count
137+
* @param int $count
138+
* @param null|string|array|PHPWord_Style_Font $fontStyle
139+
* @param null|string|array|PHPWord_Style_Paragraph $paragraphStyle
138140
*/
139-
public function addTextBreak($count = 1)
141+
public function addTextBreak($count = 1, $fontStyle = null, $paragraphStyle = null)
140142
{
141-
for ($i=1; $i<=$count; $i++) {
142-
$this->_elementCollection[] = new PHPWord_Section_TextBreak();
143+
for ($i = 1; $i <= $count; $i++) {
144+
$this->_elementCollection[] = new PHPWord_Section_TextBreak($fontStyle, $paragraphStyle);
143145
}
144146
}
145147

Classes/PHPWord/Writer/Word2007/Base.php

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -477,10 +477,51 @@ protected function _writeTextStyle(PHPWord_Shared_XMLWriter $objWriter = null, P
477477

478478
/**
479479
* Write text break
480+
*
481+
* @param PHPWord_Shared_XMLWriter $objWriter
482+
* @param PHPWord_Section_TextBreak $element
480483
*/
481-
protected function _writeTextBreak(PHPWord_Shared_XMLWriter $objWriter = null)
484+
protected function _writeTextBreak($objWriter, $element = null)
482485
{
483-
$objWriter->writeElement('w:p', null);
486+
$hasStyle = false;
487+
if (!is_null($element)) {
488+
$fontStyle = $element->getFontStyle();
489+
$sfIsObject = ($fontStyle instanceof PHPWord_Style_Font) ? true : false;
490+
$paragraphStyle = $element->getParagraphStyle();
491+
$spIsObject = ($paragraphStyle instanceof PHPWord_Style_Paragraph) ? true : false;
492+
$hasStyle = !is_null($fontStyle) || !is_null($paragraphStyle);
493+
}
494+
if ($hasStyle) {
495+
// Paragraph style
496+
$objWriter->startElement('w:p');
497+
if ($spIsObject) {
498+
$this->_writeParagraphStyle($objWriter, $paragraphStyle);
499+
} elseif (!$spIsObject && !is_null($paragraphStyle)) {
500+
$objWriter->startElement('w:pPr');
501+
$objWriter->startElement('w:pStyle');
502+
$objWriter->writeAttribute('w:val', $paragraphStyle);
503+
$objWriter->endElement(); // w:pStyle
504+
$objWriter->endElement(); // w:pPr
505+
}
506+
// Font style
507+
if (!is_null($fontStyle)) {
508+
$objWriter->startElement('w:pPr');
509+
if ($sfIsObject) {
510+
$this->_writeTextStyle($objWriter, $fontStyle);
511+
} elseif (!$sfIsObject && !is_null($fontStyle)) {
512+
$objWriter->startElement('w:rPr');
513+
$objWriter->startElement('w:rStyle');
514+
$objWriter->writeAttribute('w:val', $fontStyle);
515+
$objWriter->endElement(); // w:rStyle
516+
$objWriter->endElement(); // w:rPr
517+
}
518+
$objWriter->endElement(); // w:pPr
519+
}
520+
$objWriter->endElement(); // w:p
521+
} else {
522+
// Null element. No paragraph nor font style
523+
$objWriter->writeElement('w:p', null);
524+
}
484525
}
485526

486527
/**
@@ -570,7 +611,7 @@ protected function _writeTable(PHPWord_Shared_XMLWriter $objWriter = null, PHPWo
570611
} elseif ($element instanceof PHPWord_Section_Link) {
571612
$this->_writeLink($objWriter, $element);
572613
} elseif ($element instanceof PHPWord_Section_TextBreak) {
573-
$this->_writeTextBreak($objWriter);
614+
$this->_writeTextBreak($objWriter, $element);
574615
} elseif ($element instanceof PHPWord_Section_ListItem) {
575616
$this->_writeListItem($objWriter, $element);
576617
} elseif ($element instanceof PHPWord_Section_Image ||

Classes/PHPWord/Writer/Word2007/Document.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function writeDocument(PHPWord $pPHPWord = null)
7979
} elseif ($element instanceof PHPWord_Section_Title) {
8080
$this->_writeTitle($objWriter, $element);
8181
} elseif ($element instanceof PHPWord_Section_TextBreak) {
82-
$this->_writeTextBreak($objWriter);
82+
$this->_writeTextBreak($objWriter, $element);
8383
} elseif ($element instanceof PHPWord_Section_PageBreak) {
8484
$this->_writePageBreak($objWriter);
8585
} elseif ($element instanceof PHPWord_Section_Table) {

Classes/PHPWord/Writer/Word2007/Footer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function writeFooter(PHPWord_Section_Footer $footer)
6363
} elseif ($element instanceof PHPWord_Section_TextRun) {
6464
$this->_writeTextRun($objWriter, $element);
6565
} elseif ($element instanceof PHPWord_Section_TextBreak) {
66-
$this->_writeTextBreak($objWriter);
66+
$this->_writeTextBreak($objWriter, $element);
6767
} elseif ($element instanceof PHPWord_Section_Table) {
6868
$this->_writeTable($objWriter, $element);
6969
} elseif ($element instanceof PHPWord_Section_Image ||

Classes/PHPWord/Writer/Word2007/Header.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function writeHeader(PHPWord_Section_Header $header)
6363
} elseif ($element instanceof PHPWord_Section_TextRun) {
6464
$this->_writeTextRun($objWriter, $element);
6565
} elseif ($element instanceof PHPWord_Section_TextBreak) {
66-
$this->_writeTextBreak($objWriter);
66+
$this->_writeTextBreak($objWriter, $element);
6767
} elseif ($element instanceof PHPWord_Section_Table) {
6868
$this->_writeTable($objWriter, $element);
6969
} elseif ($element instanceof PHPWord_Section_Image ||

Tests/PHPWord/Section/TextBreakTest.php

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,59 @@
22
namespace PHPWord\Tests\Section;
33

44
use PHPWord_Section_TextBreak;
5+
use PHPWord_Style_Paragraph;
6+
use PHPWord_Style_Font;
57

8+
/**
9+
* @package PHPWord\Tests
10+
* @coversDefaultClass PHPWord_Section_TextBreak
11+
* @runTestsInSeparateProcesses
12+
*/
613
class TextBreakTest extends \PHPUnit_Framework_TestCase
714
{
815
/**
9-
* Executed before each method of the class
16+
* Construct with empty value
1017
*/
1118
public function testConstruct()
1219
{
13-
// Section Settings
14-
$oTextBreak = new PHPWord_Section_TextBreak();
20+
$object = new PHPWord_Section_TextBreak();
21+
$this->assertNull($object->getFontStyle());
22+
$this->assertNull($object->getParagraphStyle());
23+
}
24+
25+
/**
26+
* Construct with style object
27+
*/
28+
public function testConstructWithStyleObject()
29+
{
30+
$fStyle = new PHPWord_Style_Font();
31+
$pStyle = new PHPWord_Style_Paragraph();
32+
$object = new PHPWord_Section_TextBreak($fStyle, $pStyle);
33+
$this->assertEquals($fStyle, $object->getFontStyle());
34+
$this->assertEquals($pStyle, $object->getParagraphStyle());
35+
}
36+
37+
/**
38+
* Construct with style array
39+
*/
40+
public function testConstructWithStyleArray()
41+
{
42+
$fStyle = array('size' => 12);
43+
$pStyle = array('spacing' => 240);
44+
$object = new PHPWord_Section_TextBreak($fStyle, $pStyle);
45+
$this->assertInstanceOf('PHPWord_Style_Font', $object->getFontStyle());
46+
$this->assertInstanceOf('PHPWord_Style_Paragraph', $object->getParagraphStyle());
47+
}
1548

16-
$this->assertInstanceOf('PHPWord_Section_TextBreak', $oTextBreak);
49+
/**
50+
* Construct with style name
51+
*/
52+
public function testConstructWithStyleName()
53+
{
54+
$fStyle = 'fStyle';
55+
$pStyle = 'pStyle';
56+
$object = new PHPWord_Section_TextBreak($fStyle, $pStyle);
57+
$this->assertEquals($fStyle, $object->getFontStyle());
58+
$this->assertEquals($pStyle, $object->getParagraphStyle());
1759
}
1860
}

Tests/PHPWord/Writer/Word2007/BaseTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace PHPWord\Tests\Writer\Word2007;
33

44
use PHPWord;
5+
use PHPWord_Style;
56
use PHPWord\Tests\TestHelperDOCX;
67

78
/**
@@ -103,6 +104,30 @@ public function testWritePreserveText()
103104
$this->assertEquals('PAGE', $preserve->nodeValue);
104105
$this->assertEquals('preserve', $preserve->getAttribute('xml:space'));
105106
}
107+
/**
108+
* covers ::_writeTextBreak
109+
*/
110+
public function testWriteTextBreak()
111+
{
112+
$fArray = array('size' => 12);
113+
$pArray = array('spacing' => 240);
114+
$fName = 'fStyle';
115+
$pName = 'pStyle';
116+
117+
$PHPWord = new PHPWord();
118+
$PHPWord->addFontStyle($fName, $fArray);
119+
$PHPWord->addParagraphStyle($pName, $pArray);
120+
$section = $PHPWord->createSection();
121+
$section->addTextBreak();
122+
$section->addTextBreak(1, $fArray, $pArray);
123+
$section->addTextBreak(1, $fName, $pName);
124+
$doc = TestHelperDOCX::getDocument($PHPWord);
125+
126+
$element = $doc->getElement('/w:document/w:body/w:p/w:pPr/w:rPr/w:rStyle');
127+
$this->assertEquals($fName, $element->getAttribute('w:val'));
128+
$element = $doc->getElement('/w:document/w:body/w:p/w:pPr/w:pStyle');
129+
$this->assertEquals($pName, $element->getAttribute('w:val'));
130+
}
106131

107132
/**
108133
* covers ::_writeParagraphStyle

0 commit comments

Comments
 (0)