Skip to content

Commit 2567a22

Browse files
committed
Update changelog and fine tune changes
1 parent 2be4cbf commit 2567a22

File tree

8 files changed

+16
-45
lines changed

8 files changed

+16
-45
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ This release marked heavy refactorings on internal code structure with the creat
7373
- General: Add some unit tests for Shared & Element (100%!) - @Progi1984
7474
- Test: Add some samples and tests for image wrapping style - @brunocasado GH-59
7575
- Refactor: Remove Style\Tabs
76+
- Refactor: Apply composite pattern for writers
7677

7778
## 0.9.1 - 27 Mar 2014
7879

src/PhpWord/Element/AbstractElement.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use PhpOffice\PhpWord\Footnotes;
1414
use PhpOffice\PhpWord\Media;
1515
use PhpOffice\PhpWord\Style;
16-
use PhpOffice\PhpWord\TOC;
16+
use PhpOffice\PhpWord\TOC as Titles;
1717
use PhpOffice\PhpWord\Exception\InvalidObjectException;
1818
use PhpOffice\PhpWord\Shared\String;
1919

@@ -174,7 +174,7 @@ public function addTitle($text, $depth = 1)
174174
$text = String::toUTF8($text);
175175
$title = new Title($text, $depth, $style);
176176
$title->setDocPart($this->getDocPart(), $this->getDocPartId());
177-
$data = TOC::addTitle($text, $depth);
177+
$data = Titles::addTitle($text, $depth);
178178
$anchor = $data[0];
179179
$bookmarkId = $data[1];
180180
$title->setAnchor($anchor);

src/PhpWord/Writer/HTML.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,7 @@ private function writeHTMLBody()
150150
$elements = $section->getElements();
151151
foreach ($elements as $element) {
152152
if ($element instanceof AbstractElement) {
153-
$elementWriter = new ElementWriter($element, false);
154-
$elementWriter->setParentWriter($this);
153+
$elementWriter = new ElementWriter($this, $element, false);
155154
$html .= $elementWriter->write();
156155
}
157156
}
@@ -176,8 +175,7 @@ private function writeNotes()
176175
$collection = $collectionObject::getElements();
177176
if (array_key_exists($noteTypeId, $collection)) {
178177
$element = $collection[$noteTypeId];
179-
$elmWriter = new TextRunWriter($element, true);
180-
$elmWriter->setParentWriter($this);
178+
$elmWriter = new TextRunWriter($this, $element, true);
181179
$content = "<a href=\"#{$noteMark}\" class=\"NoteRef\"><sup>{$noteId}</sup></a>" . $elmWriter->write();
182180
$html .= "<p><a name=\"{$noteAnchor}\" />{$content}</p>" . PHP_EOL;
183181
}

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

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ class Element
4949
*
5050
* @param bool $withoutP
5151
*/
52-
public function __construct(AbstractElement $element, $withoutP = false)
52+
public function __construct(HTML $parentWriter, AbstractElement $element, $withoutP = false)
5353
{
54+
$this->parentWriter = $parentWriter;
5455
$this->element = $element;
5556
$this->withoutP = $withoutP;
5657
}
@@ -66,36 +67,10 @@ public function write()
6667
$elmName = str_replace('PhpOffice\\PhpWord\\Element\\', '', get_class($this->element));
6768
$elmWriterClass = 'PhpOffice\\PhpWord\\Writer\\HTML\\Element\\' . $elmName;
6869
if (class_exists($elmWriterClass) === true) {
69-
$elmWriter = new $elmWriterClass($this->element, $this->withoutP);
70-
$elmWriter->setParentWriter($this->parentWriter);
70+
$elmWriter = new $elmWriterClass($this->parentWriter, $this->element, $this->withoutP);
7171
$html = $elmWriter->write();
7272
}
7373

7474
return $html;
7575
}
76-
77-
/**
78-
* Set parent writer
79-
*
80-
* @param \PhpOffice\PhpWord\Writer\HTML $writer
81-
*/
82-
public function setParentWriter(HTML $writer)
83-
{
84-
$this->parentWriter = $writer;
85-
}
86-
87-
/**
88-
* Get parent writer
89-
*
90-
* @return \PhpOffice\PhpWord\Writer\HTML
91-
* @throws \PhpOffice\PhpWord\Exception\Exception
92-
*/
93-
public function getParentWriter()
94-
{
95-
if (!is_null($this->parentWriter)) {
96-
return $this->parentWriter;
97-
} else {
98-
throw new Exception("No parent HTML Writer assigned.");
99-
}
100-
}
10176
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function write()
3030
if (!$this->element instanceof ImageElement) {
3131
return $html;
3232
}
33-
if (!$this->getParentWriter()->isPdf()) {
33+
if (!$this->parentWriter->isPdf()) {
3434
$imageData = $this->getBase64ImageData($this->element);
3535
if (!is_null($imageData)) {
3636
$styleWriter = new StyleWriter();
@@ -71,8 +71,8 @@ private function getBase64ImageData(ImageElement $element)
7171
$zip = new $zipClass();
7272
if ($zip->open($zipFilename) !== false) {
7373
if ($zip->locateName($imageFilename)) {
74-
$zip->extractTo($this->getParentWriter()->getTempDir(), $imageFilename);
75-
$actualSource = $this->getParentWriter()->getTempDir() . DIRECTORY_SEPARATOR . $imageFilename;
74+
$zip->extractTo($this->parentWriter->getTempDir(), $imageFilename);
75+
$actualSource = $this->parentWriter->getTempDir() . DIRECTORY_SEPARATOR . $imageFilename;
7676
}
7777
}
7878
$zip->close();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ class Note extends Element
2323
*/
2424
public function write()
2525
{
26-
$noteId = count($this->getParentWriter()->getNotes()) + 1;
26+
$noteId = count($this->parentWriter->getNotes()) + 1;
2727
$prefix = ($this->element instanceof \PhpOffice\PhpWord\Element\Endnote) ? 'endnote' : 'footnote';
2828
$noteMark = $prefix . '-' . $this->element->getRelationId();
29-
$this->getParentWriter()->addNote($noteId, $noteMark);
29+
$this->parentWriter->addNote($noteId, $noteMark);
3030
$html = "<a name=\"{$noteMark}\"><a href=\"#note-{$noteId}\" class=\"NoteRef\"><sup>{$noteId}</sup></a>";
3131

3232
return $html;

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,11 @@ public function write()
3939
$html .= "<{$cellTag}>" . PHP_EOL;
4040
if (count($cellContents) > 0) {
4141
foreach ($cellContents as $content) {
42-
$writer = new Element($content, false);
43-
$writer->setParentWriter($this->parentWriter);
42+
$writer = new Element($this->parentWriter, $content, false);
4443
$html .= $writer->write();
4544
}
4645
} else {
47-
$writer = new Element(new \PhpOffice\PhpWord\Element\TextBreak(), false);
48-
$writer->setParentWriter($this->parentWriter);
46+
$writer = new Element($this->parentWriter, new \PhpOffice\PhpWord\Element\TextBreak(), false);
4947
$html .= $writer->write();
5048
}
5149
$html .= '</td>' . PHP_EOL;

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ public function write()
4040
$attribute = $pStyleIsObject ? 'style' : 'class';
4141
$html .= "<{$tag} {$attribute}=\"{$pStyle}\">";
4242
foreach ($elements as $element) {
43-
$elementWriter = new Element($element, true);
44-
$elementWriter->setParentWriter($this->parentWriter);
43+
$elementWriter = new Element($this->parentWriter, $element, true);
4544
$html .= $elementWriter->write();
4645
}
4746
$html .= "</{$tag}>";

0 commit comments

Comments
 (0)