ODText.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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;
  18. use PhpOffice\PhpWord\PhpWord;
  19. use PhpOffice\PhpWord\Shared\XMLReader;
  20. /**
  21. * Reader for ODText
  22. *
  23. * @since 0.10.0
  24. */
  25. class ODText extends AbstractReader implements ReaderInterface
  26. {
  27. /**
  28. * Loads PhpWord from file
  29. *
  30. * @param string $docFile
  31. * @return \PhpOffice\PhpWord\PhpWord
  32. */
  33. public function load($docFile)
  34. {
  35. $phpWord = new PhpWord();
  36. $relationships = $this->readRelationships($docFile);
  37. $readerParts = array(
  38. 'content.xml' => 'Content',
  39. 'meta.xml' => 'Meta',
  40. );
  41. foreach ($readerParts as $xmlFile => $partName) {
  42. $this->readPart($phpWord, $relationships, $partName, $docFile, $xmlFile);
  43. }
  44. return $phpWord;
  45. }
  46. /**
  47. * Read document part.
  48. *
  49. * @param \PhpOffice\PhpWord\PhpWord $phpWord
  50. * @param array $relationships
  51. * @param string $partName
  52. * @param string $docFile
  53. * @param string $xmlFile
  54. */
  55. private function readPart(PhpWord $phpWord, $relationships, $partName, $docFile, $xmlFile)
  56. {
  57. $partClass = "PhpOffice\\PhpWord\\Reader\\ODText\\{$partName}";
  58. if (class_exists($partClass)) {
  59. /** @var \PhpOffice\PhpWord\Reader\ODText\AbstractPart $part Type hint */
  60. $part = new $partClass($docFile, $xmlFile);
  61. $part->setRels($relationships);
  62. $part->read($phpWord);
  63. }
  64. }
  65. /**
  66. * Read all relationship files
  67. *
  68. * @param string $docFile
  69. * @return array
  70. */
  71. private function readRelationships($docFile)
  72. {
  73. $rels = array();
  74. $xmlFile = 'META-INF/manifest.xml';
  75. $xmlReader = new XMLReader();
  76. $xmlReader->getDomFromZip($docFile, $xmlFile);
  77. $nodes = $xmlReader->getElements('manifest:file-entry');
  78. foreach ($nodes as $node) {
  79. $type = $xmlReader->getAttribute('manifest:media-type', $node);
  80. $target = $xmlReader->getAttribute('manifest:full-path', $node);
  81. $rels[] = array('type' => $type, 'target' => $target);
  82. }
  83. return $rels;
  84. }
  85. }