Skip to content

Commit 13c178d

Browse files
committed
Refactor: Composite Word2007 & ODText writer parts
1 parent 5c69253 commit 13c178d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+350
-508
lines changed

src/PhpWord/Settings.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,8 @@ public static function getMeasurementUnit()
237237
*/
238238
public static function setMeasurementUnit($value)
239239
{
240-
$units = array(self::UNIT_TWIP, self::UNIT_CM, self::UNIT_MM, self::UNIT_INCH, self::UNIT_POINT, self::UNIT_PICA);
240+
$units = array(self::UNIT_TWIP, self::UNIT_CM, self::UNIT_MM, self::UNIT_INCH,
241+
self::UNIT_POINT, self::UNIT_PICA);
241242
if (!in_array($value, $units)) {
242243
return false;
243244
}

src/PhpWord/Template.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,8 @@ public function cloneRow($search, $numberOfClones)
205205

206206
// If tmpXmlRow doesn't contain continue, this row is no longer part of the spanned row.
207207
$tmpXmlRow = $this->getSlice($extraRowStart, $extraRowEnd);
208-
if (!preg_match('#<w:vMerge/>#', $tmpXmlRow) && !preg_match('#<w:vMerge w:val="continue" />#', $tmpXmlRow)) {
208+
if (!preg_match('#<w:vMerge/>#', $tmpXmlRow) &&
209+
!preg_match('#<w:vMerge w:val="continue" />#', $tmpXmlRow)) {
209210
break;
210211
}
211212
// This row was a spanned row, update $rowEnd and search for the next row.
@@ -234,7 +235,12 @@ public function cloneRow($search, $numberOfClones)
234235
public function cloneBlock($blockname, $clones = 1, $replace = true)
235236
{
236237
$xmlBlock = null;
237-
preg_match('/(<\?xml.*)(<w:p.*>\${' . $blockname . '}<\/w:.*?p>)(.*)(<w:p.*\${\/' . $blockname . '}<\/w:.*?p>)/is', $this->documentXML, $matches);
238+
$pattern =
239+
preg_match(
240+
'/(<\?xml.*)(<w:p.*>\${' . $blockname . '}<\/w:.*?p>)(.*)(<w:p.*\${\/' . $blockname . '}<\/w:.*?p>)/is',
241+
$this->documentXML,
242+
$matches
243+
);
238244

239245
if (isset($matches[3])) {
240246
$xmlBlock = $matches[3];
@@ -244,7 +250,11 @@ public function cloneBlock($blockname, $clones = 1, $replace = true)
244250
}
245251

246252
if ($replace) {
247-
$this->documentXML = str_replace($matches[2] . $matches[3] . $matches[4], implode('', $cloned), $this->documentXML);
253+
$this->documentXML = str_replace(
254+
$matches[2] . $matches[3] . $matches[4],
255+
implode('', $cloned),
256+
$this->documentXML
257+
);
248258
}
249259
}
250260

@@ -259,10 +269,18 @@ public function cloneBlock($blockname, $clones = 1, $replace = true)
259269
*/
260270
public function replaceBlock($blockname, $replacement)
261271
{
262-
preg_match('/(<\?xml.*)(<w:p.*>\${' . $blockname . '}<\/w:.*?p>)(.*)(<w:p.*\${\/' . $blockname . '}<\/w:.*?p>)/is', $this->documentXML, $matches);
272+
preg_match(
273+
'/(<\?xml.*)(<w:p.*>\${' . $blockname . '}<\/w:.*?p>)(.*)(<w:p.*\${\/' . $blockname . '}<\/w:.*?p>)/is',
274+
$this->documentXML,
275+
$matches
276+
);
263277

264278
if (isset($matches[3])) {
265-
$this->documentXML = str_replace($matches[2] . $matches[3] . $matches[4], $replacement, $this->documentXML);
279+
$this->documentXML = str_replace(
280+
$matches[2] . $matches[3] . $matches[4],
281+
$replacement,
282+
$this->documentXML
283+
);
266284
}
267285
}
268286

src/PhpWord/Writer/AbstractWriter.php

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ abstract class AbstractWriter implements WriterInterface
3535
*/
3636
protected $phpWord = null;
3737

38+
/**
39+
* Part name and file name pairs
40+
*
41+
* @var array
42+
*/
43+
protected $parts = array();
44+
3845
/**
3946
* Individual writers
4047
*
@@ -114,13 +121,13 @@ public function setPhpWord(PhpWord $phpWord = null)
114121
/**
115122
* Get writer part
116123
*
117-
* @param string $pPartName Writer part name
124+
* @param string $partName Writer part name
118125
* @return mixed
119126
*/
120-
public function getWriterPart($pPartName = '')
127+
public function getWriterPart($partName = '')
121128
{
122-
if ($pPartName != '' && isset($this->writerParts[strtolower($pPartName)])) {
123-
return $this->writerParts[strtolower($pPartName)];
129+
if ($partName != '' && isset($this->writerParts[strtolower($partName)])) {
130+
return $this->writerParts[strtolower($partName)];
124131
} else {
125132
return null;
126133
}
@@ -139,19 +146,19 @@ public function isUseDiskCaching()
139146
/**
140147
* Set use disk caching status
141148
*
142-
* @param bool $pValue
143-
* @param string $pDirectory
149+
* @param bool $value
150+
* @param string $directory
144151
* @return self
145152
*/
146-
public function setUseDiskCaching($pValue = false, $pDirectory = null)
153+
public function setUseDiskCaching($value = false, $directory = null)
147154
{
148-
$this->useDiskCaching = $pValue;
155+
$this->useDiskCaching = $value;
149156

150-
if (!is_null($pDirectory)) {
151-
if (is_dir($pDirectory)) {
152-
$this->diskCachingDirectory = $pDirectory;
157+
if (!is_null($directory)) {
158+
if (is_dir($directory)) {
159+
$this->diskCachingDirectory = $directory;
153160
} else {
154-
throw new Exception("Directory does not exist: $pDirectory");
161+
throw new Exception("Directory does not exist: $directory");
155162
}
156163
}
157164

@@ -227,7 +234,7 @@ protected function cleanupTempFile()
227234
{
228235
if ($this->originalFilename != $this->tempFilename) {
229236
if (copy($this->tempFilename, $this->originalFilename) === false) {
230-
throw new Exception("Could not copy temporary zip file {$this->tempFilename} to {$this->originalFilename}.");
237+
throw new Exception("Could not copy temporary zip file.");
231238
}
232239
@unlink($this->tempFilename);
233240
}
@@ -258,7 +265,6 @@ protected function getZipArchive($filename)
258265
$objZip = new $zipClass();
259266

260267
// Retrieve OVERWRITE and CREATE constants from the instantiated zip class
261-
// This method of accessing constant values from a dynamic class should work with all appropriate versions of PHP
262268
$reflection = new \ReflectionObject($objZip);
263269
$zipOverWrite = $reflection->getConstant('OVERWRITE');
264270
$zipCreate = $reflection->getConstant('CREATE');

src/PhpWord/Writer/HTML.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,8 @@ private function writeNotes()
188188
if (array_key_exists($noteTypeId, $collection)) {
189189
$element = $collection[$noteTypeId];
190190
$elmWriter = new TextRunWriter($this, $element, true);
191-
$content = "<a href=\"#{$noteMark}\" class=\"NoteRef\"><sup>{$noteId}</sup></a>" . $elmWriter->write();
191+
$content = "<a href=\"#{$noteMark}\" class=\"NoteRef\"><sup>{$noteId}</sup></a>";
192+
$content .= $elmWriter->write();
192193
$html .= "<p><a name=\"{$noteAnchor}\" />{$content}</p>" . PHP_EOL;
193194
}
194195
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
/**
2424
* Generic element HTML writer
2525
*
26-
* Section: Text, TextRun, Link, Title, PreserveText, TextBreak, PageBreak, Table, ListItem, Image, Object, Endnote, Footnote
26+
* Section: Text, TextRun, Link, Title, PreserveText, TextBreak, PageBreak, Table, ListItem, Image,
27+
* Object, Endnote, Footnote
2728
* Cell: Text, TextRun, Link, PreserveText, TextBreak, ListItem, Image, Object, Endnote, Footnote
2829
* TextRun: Text, Link, TextBreak, Image, Endnote, Footnote
2930
*

src/PhpWord/Writer/ODText.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,19 @@ public function __construct(PhpWord $phpWord = null)
3939
$this->setPhpWord($phpWord);
4040

4141
// Create parts
42-
$parts = array('Content', 'Manifest', 'Meta', 'Mimetype', 'Styles');
43-
foreach ($parts as $part) {
44-
$partName = strtolower($part);
45-
$partClass = 'PhpOffice\\PhpWord\\Writer\\ODText\\Part\\' . $part;
42+
$this->parts = array(
43+
'Mimetype' => 'mimetype',
44+
'Content' => 'content.xml',
45+
'Meta' => 'meta.xml',
46+
'Styles' => 'styles.xml',
47+
'Manifest' => 'META-INF/manifest.xml',
48+
);
49+
foreach (array_keys($this->parts) as $partName) {
50+
$partClass = 'PhpOffice\\PhpWord\\Writer\\ODText\\Part\\' . $partName;
4651
if (class_exists($partClass)) {
4752
$partObject = new $partClass();
4853
$partObject->setParentWriter($this);
49-
$this->writerParts[$partName] = $partObject;
54+
$this->writerParts[strtolower($partName)] = $partObject;
5055
}
5156
}
5257

@@ -72,12 +77,12 @@ public function save($filename = null)
7277
$this->addFilesToPackage($objZip, $sectionMedia);
7378
}
7479

75-
// Add parts
76-
$objZip->addFromString('mimetype', $this->getWriterPart('mimetype')->writeMimetype());
77-
$objZip->addFromString('content.xml', $this->getWriterPart('content')->writeContent($this->phpWord));
78-
$objZip->addFromString('meta.xml', $this->getWriterPart('meta')->writeMeta($this->phpWord));
79-
$objZip->addFromString('styles.xml', $this->getWriterPart('styles')->writeStyles($this->phpWord));
80-
$objZip->addFromString('META-INF/manifest.xml', $this->getWriterPart('manifest')->writeManifest());
80+
// Write parts
81+
foreach ($this->parts as $partName => $fileName) {
82+
if ($fileName != '') {
83+
$objZip->addFromString($fileName, $this->getWriterPart($partName)->write());
84+
}
85+
}
8186

8287
// Close file
8388
if ($objZip->close() === false) {

src/PhpWord/Writer/ODText/Element/Element.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,10 @@ class Element
5959
/**
6060
* Create new instance
6161
*
62+
* @param \PhpOffice\PhpWord\Element\AbstractElement $element
6263
* @param bool $withoutP
6364
*/
64-
public function __construct(XMLWriter $xmlWriter, AbstractPart $parentWriter, AbstractElement $element, $withoutP = false)
65+
public function __construct(XMLWriter $xmlWriter, AbstractPart $parentWriter, $element, $withoutP = false)
6566
{
6667
$this->xmlWriter = $xmlWriter;
6768
$this->parentWriter = $parentWriter;

src/PhpWord/Writer/ODText/Element/Table.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ public function write()
5656
$elementWriter->write();
5757
}
5858
} else {
59-
$elementWriter = new ElementWriter($this->xmlWriter, $this->parentWriter, new TextBreakElement());
59+
$element = new TextBreakElement();
60+
$elementWriter = new ElementWriter($this->xmlWriter, $this->parentWriter, $element);
6061
$elementWriter->write();
6162
}
6263
$this->xmlWriter->endElement(); // table:table-cell

src/PhpWord/Writer/ODText/Part/Content.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,18 @@
2929
use PhpOffice\PhpWord\Writer\ODText\Element\Element as ElementWriter;
3030

3131
/**
32-
* ODText content part writer
32+
* ODText content part writer: content.xml
3333
*/
3434
class Content extends AbstractPart
3535
{
3636
/**
37-
* Write content file to XML format
37+
* Write part
3838
*
39-
* @param \PhpOffice\PhpWord\PhpWord $phpWord
40-
* @return string XML Output
39+
* @return string
4140
*/
42-
public function writeContent(PhpWord $phpWord = null)
41+
public function write()
4342
{
44-
if (is_null($phpWord)) {
45-
throw new Exception("No PhpWord assigned.");
46-
}
43+
$phpWord = $this->getParentWriter()->getPhpWord();
4744
$xmlWriter = $this->getXmlWriter();
4845

4946
$xmlWriter->startDocument('1.0', 'UTF-8');

src/PhpWord/Writer/ODText/Part/Manifest.php

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,49 +20,38 @@
2020
use PhpOffice\PhpWord\Media;
2121

2222
/**
23-
* ODText manifest part writer
23+
* ODText manifest part writer: META-INF/manifest.xml
2424
*/
2525
class Manifest extends AbstractPart
2626
{
2727
/**
28-
* Write Manifest file to XML format
28+
* Write part
2929
*
30-
* @return string XML Output
30+
* @return string
3131
*/
32-
public function writeManifest()
32+
public function write()
3333
{
34-
// Create XML writer
34+
$parts = array('content.xml', 'meta.xml', 'styles.xml');
3535
$xmlWriter = $this->getXmlWriter();
3636

37-
// XML header
3837
$xmlWriter->startDocument('1.0', 'UTF-8');
39-
40-
// manifest:manifest
4138
$xmlWriter->startElement('manifest:manifest');
4239
$xmlWriter->writeAttribute('manifest:version', '1.2');
4340
$xmlWriter->writeAttribute('xmlns:manifest', 'urn:oasis:names:tc:opendocument:xmlns:manifest:1.0');
4441

45-
// manifest:file-entry
4642
$xmlWriter->startElement('manifest:file-entry');
4743
$xmlWriter->writeAttribute('manifest:media-type', 'application/vnd.oasis.opendocument.text');
48-
$xmlWriter->writeAttribute('manifest:version', '1.2');
4944
$xmlWriter->writeAttribute('manifest:full-path', '/');
45+
$xmlWriter->writeAttribute('manifest:version', '1.2');
5046
$xmlWriter->endElement();
51-
// manifest:file-entry
52-
$xmlWriter->startElement('manifest:file-entry');
53-
$xmlWriter->writeAttribute('manifest:media-type', 'text/xml');
54-
$xmlWriter->writeAttribute('manifest:full-path', 'content.xml');
55-
$xmlWriter->endElement();
56-
// manifest:file-entry
57-
$xmlWriter->startElement('manifest:file-entry');
58-
$xmlWriter->writeAttribute('manifest:media-type', 'text/xml');
59-
$xmlWriter->writeAttribute('manifest:full-path', 'meta.xml');
60-
$xmlWriter->endElement();
61-
// manifest:file-entry
62-
$xmlWriter->startElement('manifest:file-entry');
63-
$xmlWriter->writeAttribute('manifest:media-type', 'text/xml');
64-
$xmlWriter->writeAttribute('manifest:full-path', 'styles.xml');
65-
$xmlWriter->endElement();
47+
48+
// Parts
49+
foreach ($parts as $part) {
50+
$xmlWriter->startElement('manifest:file-entry');
51+
$xmlWriter->writeAttribute('manifest:media-type', 'text/xml');
52+
$xmlWriter->writeAttribute('manifest:full-path', $part);
53+
$xmlWriter->endElement();
54+
}
6655

6756
// Media files
6857
$media = Media::getElements('section');

0 commit comments

Comments
 (0)