diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a62003c..962479bd 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -73,6 +73,7 @@ This release marked heavy refactorings on internal code structure with the creat - General: Add some unit tests for Shared & Element (100%!) - @Progi1984 - Test: Add some samples and tests for image wrapping style - @brunocasado GH-59 - Refactor: Remove Style\Tabs +- Refactor: Apply composite pattern for writers ## 0.9.1 - 27 Mar 2014 diff --git a/src/PhpWord/Element/AbstractElement.php b/src/PhpWord/Element/AbstractElement.php index 152bdc98..66683980 100644 --- a/src/PhpWord/Element/AbstractElement.php +++ b/src/PhpWord/Element/AbstractElement.php @@ -13,7 +13,7 @@ use PhpOffice\PhpWord\Endnotes; use PhpOffice\PhpWord\Footnotes; use PhpOffice\PhpWord\Media; use PhpOffice\PhpWord\Style; -use PhpOffice\PhpWord\TOC; +use PhpOffice\PhpWord\TOC as Titles; use PhpOffice\PhpWord\Exception\InvalidObjectException; use PhpOffice\PhpWord\Shared\String; @@ -174,7 +174,7 @@ abstract class AbstractElement $text = String::toUTF8($text); $title = new Title($text, $depth, $style); $title->setDocPart($this->getDocPart(), $this->getDocPartId()); - $data = TOC::addTitle($text, $depth); + $data = Titles::addTitle($text, $depth); $anchor = $data[0]; $bookmarkId = $data[1]; $title->setAnchor($anchor); diff --git a/src/PhpWord/Writer/HTML.php b/src/PhpWord/Writer/HTML.php index ad1425af..0c997086 100644 --- a/src/PhpWord/Writer/HTML.php +++ b/src/PhpWord/Writer/HTML.php @@ -150,8 +150,7 @@ class HTML extends AbstractWriter implements WriterInterface $elements = $section->getElements(); foreach ($elements as $element) { if ($element instanceof AbstractElement) { - $elementWriter = new ElementWriter($element, false); - $elementWriter->setParentWriter($this); + $elementWriter = new ElementWriter($this, $element, false); $html .= $elementWriter->write(); } } @@ -176,8 +175,7 @@ class HTML extends AbstractWriter implements WriterInterface $collection = $collectionObject::getElements(); if (array_key_exists($noteTypeId, $collection)) { $element = $collection[$noteTypeId]; - $elmWriter = new TextRunWriter($element, true); - $elmWriter->setParentWriter($this); + $elmWriter = new TextRunWriter($this, $element, true); $content = "{$noteId}" . $elmWriter->write(); $html .= "

{$content}

" . PHP_EOL; } diff --git a/src/PhpWord/Writer/HTML/Element/Element.php b/src/PhpWord/Writer/HTML/Element/Element.php index c68d2743..434760f9 100644 --- a/src/PhpWord/Writer/HTML/Element/Element.php +++ b/src/PhpWord/Writer/HTML/Element/Element.php @@ -49,8 +49,9 @@ class Element * * @param bool $withoutP */ - public function __construct(AbstractElement $element, $withoutP = false) + public function __construct(HTML $parentWriter, AbstractElement $element, $withoutP = false) { + $this->parentWriter = $parentWriter; $this->element = $element; $this->withoutP = $withoutP; } @@ -66,36 +67,10 @@ class Element $elmName = str_replace('PhpOffice\\PhpWord\\Element\\', '', get_class($this->element)); $elmWriterClass = 'PhpOffice\\PhpWord\\Writer\\HTML\\Element\\' . $elmName; if (class_exists($elmWriterClass) === true) { - $elmWriter = new $elmWriterClass($this->element, $this->withoutP); - $elmWriter->setParentWriter($this->parentWriter); + $elmWriter = new $elmWriterClass($this->parentWriter, $this->element, $this->withoutP); $html = $elmWriter->write(); } return $html; } - - /** - * Set parent writer - * - * @param \PhpOffice\PhpWord\Writer\HTML $writer - */ - public function setParentWriter(HTML $writer) - { - $this->parentWriter = $writer; - } - - /** - * Get parent writer - * - * @return \PhpOffice\PhpWord\Writer\HTML - * @throws \PhpOffice\PhpWord\Exception\Exception - */ - public function getParentWriter() - { - if (!is_null($this->parentWriter)) { - return $this->parentWriter; - } else { - throw new Exception("No parent HTML Writer assigned."); - } - } } diff --git a/src/PhpWord/Writer/HTML/Element/Image.php b/src/PhpWord/Writer/HTML/Element/Image.php index 5c15f12f..28355e25 100644 --- a/src/PhpWord/Writer/HTML/Element/Image.php +++ b/src/PhpWord/Writer/HTML/Element/Image.php @@ -30,7 +30,7 @@ class Image extends Element if (!$this->element instanceof ImageElement) { return $html; } - if (!$this->getParentWriter()->isPdf()) { + if (!$this->parentWriter->isPdf()) { $imageData = $this->getBase64ImageData($this->element); if (!is_null($imageData)) { $styleWriter = new StyleWriter(); @@ -71,8 +71,8 @@ class Image extends Element $zip = new $zipClass(); if ($zip->open($zipFilename) !== false) { if ($zip->locateName($imageFilename)) { - $zip->extractTo($this->getParentWriter()->getTempDir(), $imageFilename); - $actualSource = $this->getParentWriter()->getTempDir() . DIRECTORY_SEPARATOR . $imageFilename; + $zip->extractTo($this->parentWriter->getTempDir(), $imageFilename); + $actualSource = $this->parentWriter->getTempDir() . DIRECTORY_SEPARATOR . $imageFilename; } } $zip->close(); diff --git a/src/PhpWord/Writer/HTML/Element/Note.php b/src/PhpWord/Writer/HTML/Element/Note.php index 92fab7df..cac84cbe 100644 --- a/src/PhpWord/Writer/HTML/Element/Note.php +++ b/src/PhpWord/Writer/HTML/Element/Note.php @@ -23,10 +23,10 @@ class Note extends Element */ public function write() { - $noteId = count($this->getParentWriter()->getNotes()) + 1; + $noteId = count($this->parentWriter->getNotes()) + 1; $prefix = ($this->element instanceof \PhpOffice\PhpWord\Element\Endnote) ? 'endnote' : 'footnote'; $noteMark = $prefix . '-' . $this->element->getRelationId(); - $this->getParentWriter()->addNote($noteId, $noteMark); + $this->parentWriter->addNote($noteId, $noteMark); $html = "{$noteId}"; return $html; diff --git a/src/PhpWord/Writer/HTML/Element/Table.php b/src/PhpWord/Writer/HTML/Element/Table.php index 36ba2dab..c0b3989c 100644 --- a/src/PhpWord/Writer/HTML/Element/Table.php +++ b/src/PhpWord/Writer/HTML/Element/Table.php @@ -39,13 +39,11 @@ class Table extends Element $html .= "<{$cellTag}>" . PHP_EOL; if (count($cellContents) > 0) { foreach ($cellContents as $content) { - $writer = new Element($content, false); - $writer->setParentWriter($this->parentWriter); + $writer = new Element($this->parentWriter, $content, false); $html .= $writer->write(); } } else { - $writer = new Element(new \PhpOffice\PhpWord\Element\TextBreak(), false); - $writer->setParentWriter($this->parentWriter); + $writer = new Element($this->parentWriter, new \PhpOffice\PhpWord\Element\TextBreak(), false); $html .= $writer->write(); } $html .= '' . PHP_EOL; diff --git a/src/PhpWord/Writer/HTML/Element/TextRun.php b/src/PhpWord/Writer/HTML/Element/TextRun.php index 05d40d89..a211854d 100644 --- a/src/PhpWord/Writer/HTML/Element/TextRun.php +++ b/src/PhpWord/Writer/HTML/Element/TextRun.php @@ -40,8 +40,7 @@ class TextRun extends Element $attribute = $pStyleIsObject ? 'style' : 'class'; $html .= "<{$tag} {$attribute}=\"{$pStyle}\">"; foreach ($elements as $element) { - $elementWriter = new Element($element, true); - $elementWriter->setParentWriter($this->parentWriter); + $elementWriter = new Element($this->parentWriter, $element, true); $html .= $elementWriter->write(); } $html .= "";