Refactoring: Extends Endnotes from Footnotes & Heder from Footer

This commit is contained in:
Ivan Lanin 2014-05-05 23:30:15 +07:00
parent b2a0e08db9
commit 704cc2fe04
5 changed files with 82 additions and 104 deletions

View File

@ -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';
}

View File

@ -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 <footnote> or <endnote> 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);
}
}
}
}
}

View File

@ -1,68 +0,0 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Reader\Word2007;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\XMLReader;
/**
* Notes reader
*/
class Notes extends AbstractPart
{
/**
* Note type footnotes|endnotes
*
* @var string
*/
protected $type = 'footnotes';
/**
* Read (footnotes|endnotes).xml
*
* @param \PhpOffice\PhpWord\PhpWord $phpWord
*/
public function read(PhpWord &$phpWord)
{
$this->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 <footnote> or <endnote> 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);
}
}
}
}
}

View File

@ -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();
}

View File

@ -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);
}
}