Skip to content

Commit 0b13b22

Browse files
committed
Refactor HTML & PDF writer with composite pattern
1 parent 8c9c040 commit 0b13b22

32 files changed

+736
-485
lines changed

src/PhpWord/Reader/Word2007/Document.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ private function readParagraph(XMLReader $xmlReader, \DOMElement $domNode, &$par
133133
$headingMatches = array();
134134
if ($xmlReader->elementExists('w:pPr', $domNode)) {
135135
$paragraphStyle = $this->readParagraphStyle($xmlReader, $domNode);
136-
if (is_string($paragraphStyle)) {
137-
preg_match('/Heading(\d)/', $paragraphStyle, $headingMatches);
136+
if (is_array($paragraphStyle) && array_key_exists('styleName', $paragraphStyle)) {
137+
preg_match('/Heading(\d)/', $paragraphStyle['styleName'], $headingMatches);
138138
}
139139
}
140140

src/PhpWord/Style/Paragraph.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ public function getAlign()
148148
public function setAlign($value = null)
149149
{
150150
if (strtolower($value) == 'justify') {
151-
// justify becames both
152151
$value = 'both';
153152
}
154153
$this->align = $value;

src/PhpWord/Writer/HTML.php

Lines changed: 56 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
use PhpOffice\PhpWord\Style\Font;
2424
use PhpOffice\PhpWord\Style\Paragraph;
2525
use PhpOffice\PhpWord\Style;
26-
use PhpOffice\PhpWord\Writer\HTML\Element\Element as ElementWriter;
26+
use PhpOffice\PhpWord\Writer\HTML\Element\Container;
2727
use PhpOffice\PhpWord\Writer\HTML\Element\TextRun as TextRunWriter;
2828
use PhpOffice\PhpWord\Writer\HTML\Style\Font as FontStyleWriter;
2929
use PhpOffice\PhpWord\Writer\HTML\Style\Generic as GenericStyleWriter;
@@ -89,28 +89,28 @@ public function save($filename = null)
8989
*/
9090
public function writeDocument()
9191
{
92-
$html = '';
93-
$html .= '<!DOCTYPE html>' . PHP_EOL;
94-
$html .= '<!-- Generated by PHPWord -->' . PHP_EOL;
95-
$html .= '<html>' . PHP_EOL;
96-
$html .= '<head>' . PHP_EOL;
97-
$html .= $this->writeHTMLHead();
98-
$html .= '</head>' . PHP_EOL;
99-
$html .= '<body>' . PHP_EOL;
100-
$html .= $this->writeHTMLBody();
101-
$html .= $this->writeNotes();
102-
$html .= '</body>' . PHP_EOL;
103-
$html .= '</html>' . PHP_EOL;
92+
$content = '';
93+
$content .= '<!DOCTYPE html>' . PHP_EOL;
94+
$content .= '<!-- Generated by PHPWord -->' . PHP_EOL;
95+
$content .= '<html>' . PHP_EOL;
96+
$content .= '<head>' . PHP_EOL;
97+
$content .= $this->writeHead();
98+
$content .= '</head>' . PHP_EOL;
99+
$content .= '<body>' . PHP_EOL;
100+
$content .= $this->writeBody();
101+
$content .= $this->writeNotes();
102+
$content .= '</body>' . PHP_EOL;
103+
$content .= '</html>' . PHP_EOL;
104104

105-
return $html;
105+
return $content;
106106
}
107107

108108
/**
109109
* Generate HTML header
110110
*
111111
* @return string
112112
*/
113-
private function writeHTMLHead()
113+
private function writeHead()
114114
{
115115
$properties = $this->getPhpWord()->getDocumentProperties();
116116
$propertiesMapping = array(
@@ -126,76 +126,43 @@ private function writeHTMLHead()
126126
$title = $properties->getTitle();
127127
$title = ($title != '') ? $title : 'PHPWord';
128128

129-
$html = '';
130-
$html .= '<meta charset="UTF-8" />' . PHP_EOL;
131-
$html .= '<title>' . htmlspecialchars($title) . '</title>' . PHP_EOL;
129+
$content = '';
130+
$content .= '<meta charset="UTF-8" />' . PHP_EOL;
131+
$content .= '<title>' . htmlspecialchars($title) . '</title>' . PHP_EOL;
132132
foreach ($propertiesMapping as $key => $value) {
133133
$value = ($value == '') ? $key : $value;
134134
$method = "get" . $key;
135135
if ($properties->$method() != '') {
136-
$html .= '<meta name="' . $value . '" content="' .
136+
$content .= '<meta name="' . $value . '" content="' .
137137
htmlspecialchars($properties->$method()) . '" />' . PHP_EOL;
138138
}
139139
}
140-
$html .= $this->writeStyles();
140+
$content .= $this->writeStyles();
141141

142-
return $html;
142+
return $content;
143143
}
144144

145145
/**
146146
* Get content
147147
*
148148
* @return string
149149
*/
150-
private function writeHTMLBody()
150+
private function writeBody()
151151
{
152152
$phpWord = $this->getPhpWord();
153-
$html = '';
153+
$content = '';
154154

155155
$sections = $phpWord->getSections();
156156
$countSections = count($sections);
157157

158158
if ($countSections > 0) {
159159
foreach ($sections as $section) {
160-
$elements = $section->getElements();
161-
foreach ($elements as $element) {
162-
if ($element instanceof AbstractElement) {
163-
$elementWriter = new ElementWriter($this, $element, false);
164-
$html .= $elementWriter->write();
165-
}
166-
}
167-
}
168-
}
169-
170-
return $html;
171-
}
172-
173-
/**
174-
* Write footnote/endnote contents as textruns
175-
*/
176-
private function writeNotes()
177-
{
178-
$phpWord = $this->getPhpWord();
179-
$html = '';
180-
181-
if (!empty($this->notes)) {
182-
$html .= "<hr />";
183-
foreach ($this->notes as $noteId => $noteMark) {
184-
$noteAnchor = "note-{$noteId}";
185-
list($noteType, $noteTypeId) = explode('-', $noteMark);
186-
$method = 'get' . ($noteType == 'endnote' ? 'Endnotes' : 'Footnotes');
187-
$collection = $phpWord->$method()->getItems();
188-
if (array_key_exists($noteTypeId, $collection)) {
189-
$element = $collection[$noteTypeId];
190-
$elmWriter = new TextRunWriter($this, $element, true);
191-
$content = "<a href=\"#{$noteMark}\" class=\"NoteRef\"><sup>{$noteId}</sup></a>";
192-
$content .= $elmWriter->write();
193-
$html .= "<p><a name=\"{$noteAnchor}\" />{$content}</p>" . PHP_EOL;
194-
}
160+
$writer = new Container($this, $section);
161+
$content .= $writer->write();
195162
}
196163
}
197164

198-
return $html;
165+
return $content;
199166
}
200167

201168
/**
@@ -253,6 +220,36 @@ private function writeStyles()
253220
return $css;
254221
}
255222

223+
/**
224+
* Write footnote/endnote contents as textruns
225+
*/
226+
private function writeNotes()
227+
{
228+
$phpWord = $this->getPhpWord();
229+
$content = PHP_EOL;
230+
231+
if (!empty($this->notes)) {
232+
$content .= "<hr />" . PHP_EOL;
233+
foreach ($this->notes as $noteId => $noteMark) {
234+
list($noteType, $noteTypeId) = explode('-', $noteMark);
235+
$method = 'get' . ($noteType == 'endnote' ? 'Endnotes' : 'Footnotes');
236+
$collection = $phpWord->$method()->getItems();
237+
238+
if (array_key_exists($noteTypeId, $collection)) {
239+
$element = $collection[$noteTypeId];
240+
$noteAnchor = "<a name=\"note-{$noteId}\" />";
241+
$noteAnchor .= "<a href=\"#{$noteMark}\" class=\"NoteRef\"><sup>{$noteId}</sup></a>";
242+
243+
$writer = new TextRunWriter($this, $element);
244+
$writer->setOpeningText($noteAnchor);
245+
$content .= $writer->write();
246+
}
247+
}
248+
}
249+
250+
return $content;
251+
}
252+
256253
/**
257254
* Get is PDF
258255
*

src/PhpWord/Writer/RTF/Element/Element.php renamed to src/PhpWord/Writer/HTML/Element/AbstractElement.php

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,22 @@
1515
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
1616
*/
1717

18-
namespace PhpOffice\PhpWord\Writer\RTF\Element;
18+
namespace PhpOffice\PhpWord\Writer\HTML\Element;
1919

20-
use PhpOffice\PhpWord\Element\AbstractElement;
21-
use PhpOffice\PhpWord\Writer\RTF;
20+
use PhpOffice\PhpWord\Element\AbstractElement as Element;
21+
use PhpOffice\PhpWord\Writer\AbstractWriter;
2222

2323
/**
24-
* Generic element writer
24+
* Abstract HTML element writer
2525
*
26-
* @since 0.10.0
26+
* @since 0.11.0
2727
*/
28-
class Element
28+
abstract class AbstractElement
2929
{
3030
/**
3131
* Parent writer
3232
*
33-
* @var \PhpOffice\PhpWord\Writer\RTF
33+
* @var \PhpOffice\PhpWord\Writer\AbstractWriter
3434
*/
3535
protected $parentWriter;
3636

@@ -53,27 +53,20 @@ class Element
5353
*
5454
* @param bool $withoutP
5555
*/
56-
public function __construct(RTF $parentWriter, AbstractElement $element, $withoutP = false)
56+
public function __construct(AbstractWriter $parentWriter, Element $element, $withoutP = false)
5757
{
5858
$this->parentWriter = $parentWriter;
5959
$this->element = $element;
6060
$this->withoutP = $withoutP;
6161
}
6262

6363
/**
64-
* Write element
64+
* Set without paragraph
6565
*
66-
* @return string
66+
* @param bool $value
6767
*/
68-
public function write()
68+
public function setWithoutP($value)
6969
{
70-
$content = '';
71-
$writerClass = str_replace('\\Element\\', '\\Writer\\RTF\\Element\\', get_class($this->element));
72-
if (class_exists($writerClass)) {
73-
$writer = new $writerClass($this->parentWriter, $this->element, $this->withoutP);
74-
$content = $writer->write();
75-
}
76-
77-
return $content;
70+
$this->withoutP = $value;
7871
}
7972
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* This file is part of PHPWord - A pure PHP library for reading and writing
4+
* word processing documents.
5+
*
6+
* PHPWord is free software distributed under the terms of the GNU Lesser
7+
* General Public License version 3 as published by the Free Software Foundation.
8+
*
9+
* For the full copyright and license information, please read the LICENSE
10+
* file that was distributed with this source code. For the full list of
11+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12+
*
13+
* @link https://github.com/PHPOffice/PHPWord
14+
* @copyright 2010-2014 PHPWord contributors
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWord\Writer\HTML\Element;
19+
20+
/**
21+
* Container element HTML writer
22+
*
23+
* @since 0.11.0
24+
*/
25+
class Container extends AbstractElement
26+
{
27+
/**
28+
* Write container
29+
*
30+
* @return string
31+
*/
32+
public function write()
33+
{
34+
$container = $this->element;
35+
$containerClass = substr(get_class($container), strrpos(get_class($container), '\\') + 1);
36+
$withoutP = in_array($containerClass, array('TextRun', 'Footnote', 'Endnote')) ? true : false;
37+
$content = '';
38+
39+
$elements = $container->getElements();
40+
foreach ($elements as $element) {
41+
$writerClass = str_replace('\\Element', '\\Writer\\HTML\\Element', get_class($element));
42+
if (class_exists($writerClass)) {
43+
$writer = new $writerClass($this->parentWriter, $element, $withoutP);
44+
$content .= $writer->write();
45+
}
46+
}
47+
48+
return $content;
49+
}
50+
}

src/PhpWord/Writer/HTML/Element/Element.php

Lines changed: 0 additions & 84 deletions
This file was deleted.

0 commit comments

Comments
 (0)