Footnotes.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. * @see https://github.com/PHPOffice/PHPWord
  14. * @copyright 2010-2018 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. */
  44. public function read(PhpWord $phpWord)
  45. {
  46. $xmlReader = new XMLReader();
  47. $xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
  48. $nodes = $xmlReader->getElements('*');
  49. if ($nodes->length > 0) {
  50. foreach ($nodes as $node) {
  51. $id = $xmlReader->getAttribute('w:id', $node);
  52. $type = $xmlReader->getAttribute('w:type', $node);
  53. // Avoid w:type "separator" and "continuationSeparator"
  54. // Only look for <footnote> or <endnote> without w:type attribute, or with w:type = normal
  55. if ((is_null($type) || $type === 'normal')) {
  56. $element = $this->getElement($phpWord, $id);
  57. if ($element !== null) {
  58. $pNodes = $xmlReader->getElements('w:p/*', $node);
  59. foreach ($pNodes as $pNode) {
  60. $this->readRun($xmlReader, $pNode, $element, $this->collection);
  61. }
  62. $addMethod = "add{$this->element}";
  63. $phpWord->$addMethod($element);
  64. }
  65. }
  66. }
  67. }
  68. }
  69. /**
  70. * Searches for the element with the given relationId
  71. *
  72. * @param PhpWord $phpWord
  73. * @param int $relationId
  74. * @return \PhpOffice\PhpWord\Element\AbstractContainer|null
  75. */
  76. private function getElement(PhpWord $phpWord, $relationId)
  77. {
  78. $getMethod = "get{$this->collection}";
  79. $collection = $phpWord->$getMethod()->getItems();
  80. //not found by key, looping to search by relationId
  81. foreach ($collection as $collectionElement) {
  82. if ($collectionElement->getRelationId() == $relationId) {
  83. return $collectionElement;
  84. }
  85. }
  86. return null;
  87. }
  88. }