Content.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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\ODText;
  18. use PhpOffice\PhpWord\PhpWord;
  19. use PhpOffice\PhpWord\Shared\XMLReader;
  20. /**
  21. * Content reader
  22. *
  23. * @since 0.10.0
  24. */
  25. class Content extends AbstractPart
  26. {
  27. /**
  28. * Read content.xml.
  29. *
  30. * @param \PhpOffice\PhpWord\PhpWord $phpWord
  31. * @return void
  32. */
  33. public function read(PhpWord $phpWord)
  34. {
  35. $xmlReader = new XMLReader();
  36. $xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
  37. $nodes = $xmlReader->getElements('office:body/office:text/*');
  38. if ($nodes->length > 0) {
  39. $section = $phpWord->addSection();
  40. foreach ($nodes as $node) {
  41. // $styleName = $xmlReader->getAttribute('text:style-name', $node);
  42. switch ($node->nodeName) {
  43. case 'text:h': // Heading
  44. $depth = $xmlReader->getAttribute('text:outline-level', $node);
  45. $section->addTitle($node->nodeValue, $depth);
  46. break;
  47. case 'text:p': // Paragraph
  48. $section->addText($node->nodeValue);
  49. break;
  50. case 'text:list': // List
  51. $listItems = $xmlReader->getElements('text:list-item/text:p', $node);
  52. foreach ($listItems as $listItem) {
  53. // $listStyleName = $xmlReader->getAttribute('text:style-name', $listItem);
  54. $section->addListItem($listItem->nodeValue, 0);
  55. }
  56. break;
  57. }
  58. }
  59. }
  60. }
  61. }