説明なし

Container.php 3.4KB

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. * @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\Writer\Word2007\Element;
  18. use PhpOffice\PhpWord\Element\AbstractContainer as ContainerElement;
  19. use PhpOffice\PhpWord\Element\AbstractElement as Element;
  20. use PhpOffice\PhpWord\Element\TextBreak as TextBreakElement;
  21. use PhpOffice\PhpWord\Shared\XMLWriter;
  22. /**
  23. * Container element writer (section, textrun, header, footnote, cell, etc.)
  24. *
  25. * @since 0.11.0
  26. */
  27. class Container extends AbstractElement
  28. {
  29. /**
  30. * Namespace; Can't use __NAMESPACE__ in inherited class (ODText)
  31. *
  32. * @var string
  33. */
  34. protected $namespace = 'PhpOffice\\PhpWord\\Writer\\Word2007\\Element';
  35. /**
  36. * Write element.
  37. *
  38. * @return void
  39. */
  40. public function write()
  41. {
  42. $container = $this->getElement();
  43. if (!$container instanceof ContainerElement) {
  44. return;
  45. }
  46. $containerClass = substr(get_class($container), strrpos(get_class($container), '\\') + 1);
  47. $withoutP = in_array($containerClass, array('TextRun', 'Footnote', 'Endnote', 'ListItemRun')) ? true : false;
  48. $xmlWriter = $this->getXmlWriter();
  49. // Loop through elements
  50. $elements = $container->getElements();
  51. $elementClass = '';
  52. foreach ($elements as $element) {
  53. $elementClass = $this->writeElement($xmlWriter, $element, $withoutP);
  54. }
  55. // Special case for Cell: They have to contain a w:p element at the end.
  56. // The $elementClass contains the last element name. If it's empty string
  57. // or Table, the last element is not w:p
  58. $writeLastTextBreak = ($containerClass == 'Cell') && ($elementClass == '' || $elementClass == 'Table');
  59. if ($writeLastTextBreak) {
  60. $writerClass = $this->namespace . '\\TextBreak';
  61. /** @var \PhpOffice\PhpWord\Writer\Word2007\Element\AbstractElement $writer Type hint */
  62. $writer = new $writerClass($xmlWriter, new TextBreakElement(), $withoutP);
  63. $writer->write();
  64. }
  65. }
  66. /**
  67. * Write individual element
  68. *
  69. * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
  70. * @param \PhpOffice\PhpWord\Element\AbstractElement $element
  71. * @param bool $withoutP
  72. * @return string
  73. */
  74. private function writeElement(XMLWriter $xmlWriter, Element $element, $withoutP)
  75. {
  76. $elementClass = substr(get_class($element), strrpos(get_class($element), '\\') + 1);
  77. $writerClass = $this->namespace . '\\' . $elementClass;
  78. if (class_exists($writerClass)) {
  79. /** @var \PhpOffice\PhpWord\Writer\Word2007\Element\AbstractElement $writer Type hint */
  80. $writer = new $writerClass($xmlWriter, $element, $withoutP);
  81. $writer->write();
  82. }
  83. return $elementClass;
  84. }
  85. }