diff --git a/src/PhpWord/Reader/Word2007/Endnotes.php b/src/PhpWord/Reader/Word2007/Endnotes.php index 7c81a3df..02bf9e9e 100644 --- a/src/PhpWord/Reader/Word2007/Endnotes.php +++ b/src/PhpWord/Reader/Word2007/Endnotes.php @@ -20,12 +20,19 @@ namespace PhpOffice\PhpWord\Reader\Word2007; /** * Endnotes reader */ -class Endnotes extends Notes +class Endnotes extends Footnotes { /** - * Note type = endnotes + * Collection name * * @var string */ - protected $type = 'endnotes'; + protected $collection = 'endnotes'; + + /** + * Element name + * + * @var string + */ + protected $element = 'endnote'; } diff --git a/src/PhpWord/Reader/Word2007/Footnotes.php b/src/PhpWord/Reader/Word2007/Footnotes.php index d0c8b98c..7aadf6c0 100644 --- a/src/PhpWord/Reader/Word2007/Footnotes.php +++ b/src/PhpWord/Reader/Word2007/Footnotes.php @@ -17,15 +17,58 @@ namespace PhpOffice\PhpWord\Reader\Word2007; +use PhpOffice\PhpWord\PhpWord; +use PhpOffice\PhpWord\Shared\XMLReader; + /** * Footnotes reader */ -class Footnotes extends Notes +class Footnotes extends AbstractPart { /** - * Note type = footnotes + * Collection name footnotes|endnotes * * @var string */ - protected $type = 'footnotes'; + protected $collection = 'footnotes'; + + /** + * Element name footnote|endnote + * + * @var string + */ + protected $element = 'footnote'; + + /** + * Read (footnotes|endnotes).xml + * + * @param \PhpOffice\PhpWord\PhpWord $phpWord + */ + public function read(PhpWord &$phpWord) + { + $getMethod = "get{$this->collection}"; + $collection = $phpWord->$getMethod()->getItems(); + + $xmlReader = new XMLReader(); + $xmlReader->getDomFromZip($this->docFile, $this->xmlFile); + $nodes = $xmlReader->getElements('*'); + if ($nodes->length > 0) { + foreach ($nodes as $node) { + $id = $xmlReader->getAttribute('w:id', $node); + $type = $xmlReader->getAttribute('w:type', $node); + + // Avoid w:type "separator" and "continuationSeparator" + // Only look for or without w:type attribute + if (is_null($type) && array_key_exists($id, $collection)) { + $element = $collection[$id]; + $pNodes = $xmlReader->getElements('w:p/*', $node); + foreach ($pNodes as $pNode) { + $this->readRun($xmlReader, $pNode, $element, $this->collection); + } + $addMethod = "add{$this->element}"; + $phpWord->$addMethod($element); + } + } + } + } } diff --git a/src/PhpWord/Reader/Word2007/Notes.php b/src/PhpWord/Reader/Word2007/Notes.php deleted file mode 100644 index e3a7bba6..00000000 --- a/src/PhpWord/Reader/Word2007/Notes.php +++ /dev/null @@ -1,68 +0,0 @@ -type = ($this->type == 'endnotes') ? 'endnotes' : 'footnotes'; - $getMethod = 'get' . $this->type; - $collection = $phpWord->$getMethod()->getItems(); - - $xmlReader = new XMLReader(); - $xmlReader->getDomFromZip($this->docFile, $this->xmlFile); - $nodes = $xmlReader->getElements('*'); - if ($nodes->length > 0) { - foreach ($nodes as $node) { - $id = $xmlReader->getAttribute('w:id', $node); - $type = $xmlReader->getAttribute('w:type', $node); - - // Avoid w:type "separator" and "continuationSeparator" - // Only look for or without w:type attribute - if (is_null($type) && array_key_exists($id, $collection)) { - $element = $collection[$id]; - $pNodes = $xmlReader->getElements('w:p/*', $node); - foreach ($pNodes as $pNode) { - $this->readRun($xmlReader, $pNode, $element, $this->type); - } - $addMethod = 'add' . ($this->type == 'endnotes' ? 'endnote' : 'footnote'); - $phpWord->$addMethod($element); - } - } - } - } -} diff --git a/src/PhpWord/Writer/Word2007/Part/Footer.php b/src/PhpWord/Writer/Word2007/Part/Footer.php index eed495b0..dded8266 100644 --- a/src/PhpWord/Writer/Word2007/Part/Footer.php +++ b/src/PhpWord/Writer/Word2007/Part/Footer.php @@ -17,7 +17,7 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Part; -use PhpOffice\PhpWord\Element\Footer as FooterElement; +use PhpOffice\PhpWord\Element\AbstractContainer as Container; /** * Word2007 footer part writer @@ -25,16 +25,23 @@ use PhpOffice\PhpWord\Element\Footer as FooterElement; class Footer extends AbstractPart { /** - * Write word/footnotes.xml + * Root element name * - * @param \PhpOffice\PhpWord\Element\Footer $footer + * @var string */ - public function writeFooter(FooterElement $footer) + protected $rootElement = 'w:ftr'; + + /** + * Write word/footerx.xml + * + * @param \PhpOffice\PhpWord\Element\AbstractContainer $element + */ + public function writeFooter(Container $element) { $xmlWriter = $this->getXmlWriter(); $xmlWriter->startDocument('1.0', 'UTF-8', 'yes'); - $xmlWriter->startElement('w:ftr'); + $xmlWriter->startElement($this->rootElement); $xmlWriter->writeAttribute('xmlns:ve', 'http://schemas.openxmlformats.org/markup-compatibility/2006'); $xmlWriter->writeAttribute('xmlns:o', 'urn:schemas-microsoft-com:office:office'); $xmlWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships'); @@ -45,9 +52,9 @@ class Footer extends AbstractPart $xmlWriter->writeAttribute('xmlns:w', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'); $xmlWriter->writeAttribute('xmlns:wne', 'http://schemas.microsoft.com/office/word/2006/wordml'); - $this->writeContainerElements($xmlWriter, $footer); + $this->writeContainerElements($xmlWriter, $element); - $xmlWriter->endElement(); // w:ftr + $xmlWriter->endElement(); // $this->rootElement return $xmlWriter->getData(); } diff --git a/src/PhpWord/Writer/Word2007/Part/Header.php b/src/PhpWord/Writer/Word2007/Part/Header.php index 319b030e..2b515c47 100644 --- a/src/PhpWord/Writer/Word2007/Part/Header.php +++ b/src/PhpWord/Writer/Word2007/Part/Header.php @@ -17,38 +17,27 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Part; -use PhpOffice\PhpWord\Element\Header as HeaderElement; +use PhpOffice\PhpWord\Element\AbstractContainer as Container; /** * Word2007 header part writer */ -class Header extends AbstractPart +class Header extends Footer { + /** + * Root element name + * + * @var string + */ + protected $rootElement = 'w:hdr'; + /** * Write word/headerx.xml * - * @param \PhpOffice\PhpWord\Element\Header $header + * @param \PhpOffice\PhpWord\Element\AbstractContainer $element */ - public function writeHeader(HeaderElement $header) + public function writeHeader(Container $element) { - $xmlWriter = $this->getXmlWriter(); - - $xmlWriter->startDocument('1.0', 'UTF-8', 'yes'); - $xmlWriter->startElement('w:hdr'); - $xmlWriter->writeAttribute('xmlns:ve', 'http://schemas.openxmlformats.org/markup-compatibility/2006'); - $xmlWriter->writeAttribute('xmlns:o', 'urn:schemas-microsoft-com:office:office'); - $xmlWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships'); - $xmlWriter->writeAttribute('xmlns:m', 'http://schemas.openxmlformats.org/officeDocument/2006/math'); - $xmlWriter->writeAttribute('xmlns:v', 'urn:schemas-microsoft-com:vml'); - $xmlWriter->writeAttribute('xmlns:wp', 'http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing'); - $xmlWriter->writeAttribute('xmlns:w10', 'urn:schemas-microsoft-com:office:word'); - $xmlWriter->writeAttribute('xmlns:w', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'); - $xmlWriter->writeAttribute('xmlns:wne', 'http://schemas.microsoft.com/office/word/2006/wordml'); - - $this->writeContainerElements($xmlWriter, $header); - - $xmlWriter->endElement(); // w:hdr - - return $xmlWriter->getData(); + return $this->writeFooter($element); } }