Skip to content

Commit 704cc2f

Browse files
committed
Refactoring: Extends Endnotes from Footnotes & Heder from Footer
1 parent b2a0e08 commit 704cc2f

File tree

5 files changed

+82
-104
lines changed

5 files changed

+82
-104
lines changed

src/PhpWord/Reader/Word2007/Endnotes.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,19 @@
2020
/**
2121
* Endnotes reader
2222
*/
23-
class Endnotes extends Notes
23+
class Endnotes extends Footnotes
2424
{
2525
/**
26-
* Note type = endnotes
26+
* Collection name
2727
*
2828
* @var string
2929
*/
30-
protected $type = 'endnotes';
30+
protected $collection = 'endnotes';
31+
32+
/**
33+
* Element name
34+
*
35+
* @var string
36+
*/
37+
protected $element = 'endnote';
3138
}

src/PhpWord/Reader/Word2007/Footnotes.php

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,58 @@
1717

1818
namespace PhpOffice\PhpWord\Reader\Word2007;
1919

20+
use PhpOffice\PhpWord\PhpWord;
21+
use PhpOffice\PhpWord\Shared\XMLReader;
22+
2023
/**
2124
* Footnotes reader
2225
*/
23-
class Footnotes extends Notes
26+
class Footnotes extends AbstractPart
2427
{
2528
/**
26-
* Note type = footnotes
29+
* Collection name footnotes|endnotes
30+
*
31+
* @var string
32+
*/
33+
protected $collection = 'footnotes';
34+
35+
/**
36+
* Element name footnote|endnote
2737
*
2838
* @var string
2939
*/
30-
protected $type = 'footnotes';
40+
protected $element = 'footnote';
41+
42+
/**
43+
* Read (footnotes|endnotes).xml
44+
*
45+
* @param \PhpOffice\PhpWord\PhpWord $phpWord
46+
*/
47+
public function read(PhpWord &$phpWord)
48+
{
49+
$getMethod = "get{$this->collection}";
50+
$collection = $phpWord->$getMethod()->getItems();
51+
52+
$xmlReader = new XMLReader();
53+
$xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
54+
$nodes = $xmlReader->getElements('*');
55+
if ($nodes->length > 0) {
56+
foreach ($nodes as $node) {
57+
$id = $xmlReader->getAttribute('w:id', $node);
58+
$type = $xmlReader->getAttribute('w:type', $node);
59+
60+
// Avoid w:type "separator" and "continuationSeparator"
61+
// Only look for <footnote> or <endnote> without w:type attribute
62+
if (is_null($type) && array_key_exists($id, $collection)) {
63+
$element = $collection[$id];
64+
$pNodes = $xmlReader->getElements('w:p/*', $node);
65+
foreach ($pNodes as $pNode) {
66+
$this->readRun($xmlReader, $pNode, $element, $this->collection);
67+
}
68+
$addMethod = "add{$this->element}";
69+
$phpWord->$addMethod($element);
70+
}
71+
}
72+
}
73+
}
3174
}

src/PhpWord/Reader/Word2007/Notes.php

Lines changed: 0 additions & 68 deletions
This file was deleted.

src/PhpWord/Writer/Word2007/Part/Footer.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,31 @@
1717

1818
namespace PhpOffice\PhpWord\Writer\Word2007\Part;
1919

20-
use PhpOffice\PhpWord\Element\Footer as FooterElement;
20+
use PhpOffice\PhpWord\Element\AbstractContainer as Container;
2121

2222
/**
2323
* Word2007 footer part writer
2424
*/
2525
class Footer extends AbstractPart
2626
{
2727
/**
28-
* Write word/footnotes.xml
28+
* Root element name
2929
*
30-
* @param \PhpOffice\PhpWord\Element\Footer $footer
30+
* @var string
3131
*/
32-
public function writeFooter(FooterElement $footer)
32+
protected $rootElement = 'w:ftr';
33+
34+
/**
35+
* Write word/footerx.xml
36+
*
37+
* @param \PhpOffice\PhpWord\Element\AbstractContainer $element
38+
*/
39+
public function writeFooter(Container $element)
3340
{
3441
$xmlWriter = $this->getXmlWriter();
3542

3643
$xmlWriter->startDocument('1.0', 'UTF-8', 'yes');
37-
$xmlWriter->startElement('w:ftr');
44+
$xmlWriter->startElement($this->rootElement);
3845
$xmlWriter->writeAttribute('xmlns:ve', 'http://schemas.openxmlformats.org/markup-compatibility/2006');
3946
$xmlWriter->writeAttribute('xmlns:o', 'urn:schemas-microsoft-com:office:office');
4047
$xmlWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
@@ -45,9 +52,9 @@ public function writeFooter(FooterElement $footer)
4552
$xmlWriter->writeAttribute('xmlns:w', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main');
4653
$xmlWriter->writeAttribute('xmlns:wne', 'http://schemas.microsoft.com/office/word/2006/wordml');
4754

48-
$this->writeContainerElements($xmlWriter, $footer);
55+
$this->writeContainerElements($xmlWriter, $element);
4956

50-
$xmlWriter->endElement(); // w:ftr
57+
$xmlWriter->endElement(); // $this->rootElement
5158

5259
return $xmlWriter->getData();
5360
}

src/PhpWord/Writer/Word2007/Part/Header.php

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,38 +17,27 @@
1717

1818
namespace PhpOffice\PhpWord\Writer\Word2007\Part;
1919

20-
use PhpOffice\PhpWord\Element\Header as HeaderElement;
20+
use PhpOffice\PhpWord\Element\AbstractContainer as Container;
2121

2222
/**
2323
* Word2007 header part writer
2424
*/
25-
class Header extends AbstractPart
25+
class Header extends Footer
2626
{
27+
/**
28+
* Root element name
29+
*
30+
* @var string
31+
*/
32+
protected $rootElement = 'w:hdr';
33+
2734
/**
2835
* Write word/headerx.xml
2936
*
30-
* @param \PhpOffice\PhpWord\Element\Header $header
37+
* @param \PhpOffice\PhpWord\Element\AbstractContainer $element
3138
*/
32-
public function writeHeader(HeaderElement $header)
39+
public function writeHeader(Container $element)
3340
{
34-
$xmlWriter = $this->getXmlWriter();
35-
36-
$xmlWriter->startDocument('1.0', 'UTF-8', 'yes');
37-
$xmlWriter->startElement('w:hdr');
38-
$xmlWriter->writeAttribute('xmlns:ve', 'http://schemas.openxmlformats.org/markup-compatibility/2006');
39-
$xmlWriter->writeAttribute('xmlns:o', 'urn:schemas-microsoft-com:office:office');
40-
$xmlWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
41-
$xmlWriter->writeAttribute('xmlns:m', 'http://schemas.openxmlformats.org/officeDocument/2006/math');
42-
$xmlWriter->writeAttribute('xmlns:v', 'urn:schemas-microsoft-com:vml');
43-
$xmlWriter->writeAttribute('xmlns:wp', 'http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing');
44-
$xmlWriter->writeAttribute('xmlns:w10', 'urn:schemas-microsoft-com:office:word');
45-
$xmlWriter->writeAttribute('xmlns:w', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main');
46-
$xmlWriter->writeAttribute('xmlns:wne', 'http://schemas.microsoft.com/office/word/2006/wordml');
47-
48-
$this->writeContainerElements($xmlWriter, $header);
49-
50-
$xmlWriter->endElement(); // w:hdr
51-
52-
return $xmlWriter->getData();
41+
return $this->writeFooter($element);
5342
}
5443
}

0 commit comments

Comments
 (0)