Container.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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\Writer\HTML\Element;
  18. use PhpOffice\PhpWord\Element\AbstractContainer as ContainerElement;
  19. /**
  20. * Container element HTML writer
  21. *
  22. * @since 0.11.0
  23. */
  24. class Container extends AbstractElement
  25. {
  26. /**
  27. * Namespace; Can't use __NAMESPACE__ in inherited class (RTF)
  28. *
  29. * @var string
  30. */
  31. protected $namespace = 'PhpOffice\\PhpWord\\Writer\\HTML\\Element';
  32. /**
  33. * Write container
  34. *
  35. * @return string
  36. */
  37. public function write()
  38. {
  39. $container = $this->element;
  40. if (!$container instanceof ContainerElement) {
  41. return '';
  42. }
  43. $containerClass = substr(get_class($container), strrpos(get_class($container), '\\') + 1);
  44. $withoutP = in_array($containerClass, array('TextRun', 'Footnote', 'Endnote')) ? true : false;
  45. $content = '';
  46. $elements = $container->getElements();
  47. foreach ($elements as $element) {
  48. $elementClass = get_class($element);
  49. $writerClass = str_replace('PhpOffice\\PhpWord\\Element', $this->namespace, $elementClass);
  50. if (class_exists($writerClass)) {
  51. /** @var \PhpOffice\PhpWord\Writer\HTML\Element\AbstractElement $writer Type hint */
  52. $writer = new $writerClass($this->parentWriter, $element, $withoutP);
  53. $content .= $writer->write();
  54. }
  55. }
  56. return $content;
  57. }
  58. }