Footnotes.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * This file is part of PHPWord - A pure PHP library for reading and writing
  4. * word processing documents.
  5. *
  6. * PHPWord is free software distributed under the terms of the GNU Lesser
  7. * General Public License version 3 as published by the Free Software Foundation.
  8. *
  9. * For the full copyright and license information, please read the LICENSE
  10. * file that was distributed with this source code. For the full list of
  11. * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
  12. *
  13. * @link https://github.com/PHPOffice/PHPWord
  14. * @copyright 2010-2014 PHPWord contributors
  15. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  16. */
  17. namespace PhpOffice\PhpWord\Reader\Word2007;
  18. use PhpOffice\PhpWord\PhpWord;
  19. use PhpOffice\PhpWord\Shared\XMLReader;
  20. /**
  21. * Footnotes reader
  22. *
  23. * @since 0.10.0
  24. */
  25. class Footnotes extends AbstractPart
  26. {
  27. /**
  28. * Collection name footnotes|endnotes
  29. *
  30. * @var string
  31. */
  32. protected $collection = 'footnotes';
  33. /**
  34. * Element name footnote|endnote
  35. *
  36. * @var string
  37. */
  38. protected $element = 'footnote';
  39. /**
  40. * Read (footnotes|endnotes).xml.
  41. *
  42. * @param \PhpOffice\PhpWord\PhpWord $phpWord
  43. * @return void
  44. */
  45. public function read(PhpWord $phpWord)
  46. {
  47. $getMethod = "get{$this->collection}";
  48. $collection = $phpWord->$getMethod()->getItems();
  49. $xmlReader = new XMLReader();
  50. $xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
  51. $nodes = $xmlReader->getElements('*');
  52. if ($nodes->length > 0) {
  53. foreach ($nodes as $node) {
  54. $id = $xmlReader->getAttribute('w:id', $node);
  55. $type = $xmlReader->getAttribute('w:type', $node);
  56. // Avoid w:type "separator" and "continuationSeparator"
  57. // Only look for <footnote> or <endnote> without w:type attribute
  58. if (is_null($type) && isset($collection[$id])) {
  59. $element = $collection[$id];
  60. $pNodes = $xmlReader->getElements('w:p/*', $node);
  61. foreach ($pNodes as $pNode) {
  62. $this->readRun($xmlReader, $pNode, $element, $this->collection);
  63. }
  64. $addMethod = "add{$this->element}";
  65. $phpWord->$addMethod($element);
  66. }
  67. }
  68. }
  69. }
  70. }