Skip to content

Commit 4039304

Browse files
committed
Word2007 Writer: Refactor writer parts using composite pattern
1 parent 704cc2f commit 4039304

File tree

13 files changed

+308
-143
lines changed

13 files changed

+308
-143
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ This release changed PHPWord license from LGPL 2.1 to LGPL 3.
1818
### Deprecated
1919

2020
- Static classes `Footnotes`, `Endnotes`, and `TOC`
21+
- `Writer\Word2007\Part`: `Numbering::writeNumbering()`, `Settings::writeSettings()`, `WebSettings::writeWebSettings()`, `ContentTypes::writeContentTypes()`, `Styles::writeStyles()`, `Document::writeDocument()` all changed into `write()`
22+
- `Writer\Word2007\Part\DocProps`: Split into `Writer\Word2007\Part\DocPropsCore` and `Writer\Word2007\Part\DocPropsApp`
2123

2224
### Miscellaneous
2325

@@ -28,6 +30,7 @@ This release changed PHPWord license from LGPL 2.1 to LGPL 3.
2830
- Refactor: PHPMD recommendation: Change all `get...` method that returns `boolean` into `is...` or `has...` - @ivanlanin
2931
- Docs: Create gh-pages branch for API documentation - @Progi1984 GH-154
3032
- QA: Add `.scrutinizer.yml` and include `composer.lock` for preparation to Scrutinizer - @ivanlanin GH-186
33+
- Word2007 Writer: Refactor writer parts using composite pattern - @ivanlanin
3134

3235
## 0.10.0 - 4 May 2014
3336

src/PhpWord/Writer/Word2007.php

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ class Word2007 extends AbstractWriter implements WriterInterface
3232
*
3333
* @var array
3434
*/
35-
private $cTypes = array('default' => array(), 'override' => array());
35+
private $contentTypes = array('default' => array(), 'override' => array());
3636

3737
/**
3838
* Document relationship
3939
*
4040
* @var array
4141
*/
42-
private $docRels = array();
42+
private $relationships = array();
4343

4444
/**
4545
* Create new Word2007 writer
@@ -52,7 +52,7 @@ public function __construct(PhpWord $phpWord = null)
5252
$this->setPhpWord($phpWord);
5353

5454
// Create parts
55-
$parts = array('ContentTypes', 'Rels', 'DocProps', 'Document', 'Styles',
55+
$parts = array('ContentTypes', 'RelsMain', 'RelsDocument', 'DocPropsApp', 'DocPropsCore', 'Document', 'Styles',
5656
'Numbering', 'Settings', 'WebSettings', 'Header', 'Footer', 'Footnotes',
5757
'Endnotes', 'FontTable', 'Theme');
5858
foreach ($parts as $part) {
@@ -81,7 +81,7 @@ public function save($filename = null)
8181
$objZip = $this->getZipArchive($filename);
8282

8383
// Content types
84-
$this->cTypes['default'] = array(
84+
$this->contentTypes['default'] = array(
8585
'rels' => 'application/vnd.openxmlformats-package.relationships+xml',
8686
'xml' => 'application/xml',
8787
);
@@ -92,7 +92,7 @@ public function save($filename = null)
9292
$this->addFilesToPackage($objZip, $sectionMedia);
9393
$this->registerContentTypes($sectionMedia);
9494
foreach ($sectionMedia as $element) {
95-
$this->docRels[] = $element;
95+
$this->relationships[] = $element;
9696
}
9797
}
9898

@@ -112,16 +112,16 @@ public function save($filename = null)
112112
$this->addNotes($objZip, $rId, 'endnote');
113113

114114
// Write parts
115-
$objZip->addFromString('[Content_Types].xml', $this->getWriterPart('contenttypes')->writeContentTypes($this->cTypes));
116-
$objZip->addFromString('_rels/.rels', $this->getWriterPart('rels')->writeMainRels());
117-
$objZip->addFromString('docProps/app.xml', $this->getWriterPart('docprops')->writeDocPropsApp($this->phpWord));
118-
$objZip->addFromString('docProps/core.xml', $this->getWriterPart('docprops')->writeDocPropsCore($this->phpWord));
119-
$objZip->addFromString('word/_rels/document.xml.rels', $this->getWriterPart('rels')->writeDocRels($this->docRels));
120-
$objZip->addFromString('word/document.xml', $this->getWriterPart('document')->writeDocument($this->phpWord));
121-
$objZip->addFromString('word/styles.xml', $this->getWriterPart('styles')->writeStyles($this->phpWord));
122-
$objZip->addFromString('word/numbering.xml', $this->getWriterPart('numbering')->writeNumbering());
123-
$objZip->addFromString('word/settings.xml', $this->getWriterPart('settings')->writeSettings());
124-
$objZip->addFromString('word/webSettings.xml', $this->getWriterPart('websettings')->writeWebSettings());
115+
$objZip->addFromString('[Content_Types].xml', $this->getWriterPart('contenttypes')->write());
116+
$objZip->addFromString('_rels/.rels', $this->getWriterPart('relsmain')->write());
117+
$objZip->addFromString('docProps/app.xml', $this->getWriterPart('docpropsapp')->write());
118+
$objZip->addFromString('docProps/core.xml', $this->getWriterPart('docpropscore')->write());
119+
$objZip->addFromString('word/_rels/document.xml.rels', $this->getWriterPart('relsdocument')->write());
120+
$objZip->addFromString('word/document.xml', $this->getWriterPart('document')->write());
121+
$objZip->addFromString('word/styles.xml', $this->getWriterPart('styles')->write());
122+
$objZip->addFromString('word/numbering.xml', $this->getWriterPart('numbering')->write());
123+
$objZip->addFromString('word/settings.xml', $this->getWriterPart('settings')->write());
124+
$objZip->addFromString('word/webSettings.xml', $this->getWriterPart('websettings')->write());
125125
$objZip->addFromString('word/fontTable.xml', $this->getWriterPart('fonttable')->write());
126126
$objZip->addFromString('word/theme/theme1.xml', $this->getWriterPart('theme')->write());
127127

@@ -136,6 +136,26 @@ public function save($filename = null)
136136
}
137137
}
138138

139+
/**
140+
* Get content types
141+
*
142+
* @return array
143+
*/
144+
public function getContentTypes()
145+
{
146+
return $this->contentTypes;
147+
}
148+
149+
/**
150+
* Get content types
151+
*
152+
* @return array
153+
*/
154+
public function getRelationships()
155+
{
156+
return $this->relationships;
157+
}
158+
139159
/**
140160
* Add header/footer media files
141161
*
@@ -177,8 +197,8 @@ private function addHeaderFooterContent(Section &$section, $objZip, $elmType, &$
177197
$elmObject->setRelationId(++$rId);
178198
$elmFile = "{$elmType}{$elmCount}.xml";
179199
$objZip->addFromString("word/$elmFile", $this->getWriterPart($elmType)->$writeFunction($elmObject));
180-
$this->cTypes['override']["/word/$elmFile"] = $elmType;
181-
$this->docRels[] = array('target' => $elmFile, 'type' => $elmType, 'rID' => $rId);
200+
$this->contentTypes['override']["/word/$elmFile"] = $elmType;
201+
$this->relationships[] = array('target' => $elmFile, 'type' => $elmType, 'rID' => $rId);
182202
}
183203
}
184204

@@ -192,8 +212,8 @@ private function addHeaderFooterContent(Section &$section, $objZip, $elmType, &$
192212
private function addNotes($objZip, &$rId, $noteType = 'footnote')
193213
{
194214
$noteType = ($noteType == 'endnote') ? 'endnote' : 'footnote';
195-
$noteTypePlural = "{$noteType}s";
196-
$method = 'get' . $noteTypePlural;
215+
$partName = "{$noteType}s";
216+
$method = 'get' . $partName;
197217
$collection = $this->phpWord->$method();
198218

199219
// Add footnotes media files, relations, and contents
@@ -202,12 +222,12 @@ private function addNotes($objZip, &$rId, $noteType = 'footnote')
202222
$this->addFilesToPackage($objZip, $media);
203223
$this->registerContentTypes($media);
204224
if (!empty($media)) {
205-
$objZip->addFromString("word/_rels/{$noteTypePlural}.xml.rels", $this->getWriterPart('rels')->writeMediaRels($media));
225+
$objZip->addFromString("word/_rels/{$partName}.xml.rels", $this->getWriterPart('rels')->writeMediaRels($media));
206226
}
207227
$elements = $collection->getItems();
208-
$objZip->addFromString("word/{$noteTypePlural}.xml", $this->getWriterPart($noteTypePlural)->write($elements));
209-
$this->cTypes['override']["/word/{$noteTypePlural}.xml"] = $noteTypePlural;
210-
$this->docRels[] = array('target' => "{$noteTypePlural}.xml", 'type' => $noteTypePlural, 'rID' => ++$rId);
228+
$objZip->addFromString("word/{$partName}.xml", $this->getWriterPart($partName)->write($elements));
229+
$this->contentTypes['override']["/word/{$partName}.xml"] = $partName;
230+
$this->relationships[] = array('target' => "{$partName}.xml", 'type' => $partName, 'rID' => ++$rId);
211231
}
212232
}
213233

@@ -222,12 +242,12 @@ private function registerContentTypes($media)
222242
$mediumType = $medium['type'];
223243
if ($mediumType == 'image') {
224244
$extension = $medium['imageExtension'];
225-
if (!array_key_exists($extension, $this->cTypes['default'])) {
226-
$this->cTypes['default'][$extension] = $medium['imageType'];
245+
if (!array_key_exists($extension, $this->contentTypes['default'])) {
246+
$this->contentTypes['default'][$extension] = $medium['imageType'];
227247
}
228248
} elseif ($mediumType == 'object') {
229-
if (!array_key_exists('bin', $this->cTypes['default'])) {
230-
$this->cTypes['default']['bin'] = 'application/vnd.openxmlformats-officedocument.oleObject';
249+
if (!array_key_exists('bin', $this->contentTypes['default'])) {
250+
$this->contentTypes['default']['bin'] = 'application/vnd.openxmlformats-officedocument.oleObject';
231251
}
232252
}
233253
}

src/PhpWord/Writer/Word2007/Part/ContentTypes.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ class ContentTypes extends AbstractPart
2727
{
2828
/**
2929
* Write [Content_Types].xml
30-
*
31-
* @param array $contentTypes
3230
*/
33-
public function writeContentTypes($contentTypes)
31+
public function write()
3432
{
33+
$contentTypes = $this->parentWriter->getContentTypes();
34+
3535
$openXMLPrefix = 'application/vnd.openxmlformats-';
3636
$wordMLPrefix = $openXMLPrefix . 'officedocument.wordprocessingml.';
3737
$overrides = array(
@@ -90,4 +90,17 @@ private function writeContentType(XMLWriter $xmlWriter, $parts, $isDefault)
9090
}
9191
}
9292
}
93+
94+
/**
95+
* Write [Content_Types].xml
96+
*
97+
* @param array $contentTypes
98+
* @deprecated 0.11.0
99+
* @codeCoverageIgnore
100+
*/
101+
public function writeContentTypes($contentTypes)
102+
{
103+
$contentTypes = null; // dummy assignment
104+
return $this->write();
105+
}
93106
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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\Word2007\Part;
19+
20+
use PhpOffice\PhpWord\Exception\Exception;
21+
use PhpOffice\PhpWord\PhpWord;
22+
23+
/**
24+
* Word2007 extended document properties part writer
25+
*
26+
* @since 0.11.0
27+
*/
28+
class DocPropsApp extends AbstractPart
29+
{
30+
/**
31+
* Write docProps/app.xml
32+
*/
33+
public function write()
34+
{
35+
$phpWord = $this->parentWriter->getPhpWord();
36+
if (is_null($phpWord)) {
37+
throw new Exception('No PhpWord assigned.');
38+
}
39+
$xmlWriter = $this->getXmlWriter();
40+
41+
$xmlWriter->startDocument('1.0', 'UTF-8', 'yes');
42+
$xmlWriter->startElement('Properties');
43+
$xmlWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/officeDocument/2006/extended-properties');
44+
$xmlWriter->writeAttribute('xmlns:vt', 'http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes');
45+
46+
$xmlWriter->writeElement('Application', 'PHPWord');
47+
$xmlWriter->writeElement('Company', $phpWord->getDocumentProperties()->getCompany());
48+
$xmlWriter->writeElement('Manager', $phpWord->getDocumentProperties()->getManager());
49+
50+
$xmlWriter->endElement(); // Properties
51+
52+
return $xmlWriter->getData();
53+
}
54+
}

src/PhpWord/Writer/Word2007/Part/DocProps.php renamed to src/PhpWord/Writer/Word2007/Part/DocPropsCore.php

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,44 +21,20 @@
2121
use PhpOffice\PhpWord\PhpWord;
2222

2323
/**
24-
* Word2007 document properties part writer
24+
* Word2007 core document properties part writer
25+
*
26+
* @since 0.11.0
2527
*/
26-
class DocProps extends AbstractPart
28+
class DocPropsCore extends AbstractPart
2729
{
28-
/**
29-
* Write docProps/app.xml
30-
*/
31-
public function writeDocPropsApp(PhpWord $phpWord = null)
32-
{
33-
if (is_null($phpWord)) {
34-
throw new Exception("No PhpWord assigned.");
35-
}
36-
$xmlWriter = $this->getXmlWriter();
37-
38-
$xmlWriter->startDocument('1.0', 'UTF-8', 'yes');
39-
$xmlWriter->startElement('Properties');
40-
$xmlWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/officeDocument/2006/extended-properties');
41-
$xmlWriter->writeAttribute('xmlns:vt', 'http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes');
42-
43-
$xmlWriter->writeElement('Application', 'PHPWord');
44-
$xmlWriter->writeElement('Company', $phpWord->getDocumentProperties()->getCompany());
45-
$xmlWriter->writeElement('Manager', $phpWord->getDocumentProperties()->getManager());
46-
47-
$xmlWriter->endElement(); // Properties
48-
49-
return $xmlWriter->getData();
50-
}
51-
52-
5330
/**
5431
* Write docProps/core.xml
55-
*
56-
* @param \PhpOffice\PhpWord\PhpWord $phpWord
5732
*/
58-
public function writeDocPropsCore(PhpWord $phpWord = null)
33+
public function write()
5934
{
35+
$phpWord = $this->parentWriter->getPhpWord();
6036
if (is_null($phpWord)) {
61-
throw new Exception("No PhpWord assigned.");
37+
throw new Exception('No PhpWord assigned.');
6238
}
6339
$xmlWriter = $this->getXmlWriter();
6440

src/PhpWord/Writer/Word2007/Part/Document.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ class Document extends AbstractPart
3131
/**
3232
* Write word/document.xml
3333
*
34-
* @param \PhpOffice\PhpWord\PhpWord $phpWord
3534
* @return string
3635
* @throws \PhpOffice\PhpWord\Exception\Exception
3736
*/
38-
public function writeDocument(PhpWord $phpWord = null)
37+
public function write()
3938
{
39+
$phpWord = $this->parentWriter->getPhpWord();
4040
if (is_null($phpWord)) {
41-
throw new Exception("No PhpWord assigned.");
41+
throw new Exception('No PhpWord assigned.');
4242
}
4343
$xmlWriter = $this->getXmlWriter();
4444
$sections = $phpWord->getSections();
@@ -133,4 +133,17 @@ private function writeSectionSettings(XMLWriter $xmlWriter, Section $section)
133133

134134
$xmlWriter->endElement(); // w:sectPr
135135
}
136+
137+
/**
138+
* Write word/document.xml
139+
*
140+
* @param \PhpOffice\PhpWord\PhpWord $phpWord
141+
* @deprecated 0.11.0
142+
* @codeCoverageIgnore
143+
*/
144+
public function writeDocument(PhpWord $phpWord = null)
145+
{
146+
$this->parentWriter->setPhpWord($phpWord);
147+
return $this->write();
148+
}
136149
}

0 commit comments

Comments
 (0)