Title.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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\ODText\Element;
  18. /**
  19. * Title element writer
  20. *
  21. * @since 0.11.0
  22. */
  23. class Title extends AbstractElement
  24. {
  25. /**
  26. * Write element
  27. */
  28. public function write()
  29. {
  30. $xmlWriter = $this->getXmlWriter();
  31. $element = $this->getElement();
  32. if (!$element instanceof \PhpOffice\PhpWord\Element\Title) {
  33. return;
  34. }
  35. $xmlWriter->startElement('text:h');
  36. $hdname = 'HD';
  37. $sect = $element->getParent();
  38. if ($sect instanceof \PhpOffice\PhpWord\Element\Section) {
  39. if (self::compareToFirstElement($element, $sect->getElements())) {
  40. $hdname = 'HE';
  41. }
  42. }
  43. $depth = $element->getDepth();
  44. $xmlWriter->writeAttribute('text:style-name', "$hdname$depth");
  45. $xmlWriter->writeAttribute('text:outline-level', $depth);
  46. $xmlWriter->startElement('text:span');
  47. if ($depth > 0) {
  48. $xmlWriter->writeAttribute('text:style-name', 'Heading_' . $depth);
  49. } else {
  50. $xmlWriter->writeAttribute('text:style-name', 'Title');
  51. }
  52. $text = $element->getText();
  53. if (is_string($text)) {
  54. $this->writeText($text);
  55. } elseif ($text instanceof \PhpOffice\PhpWord\Element\AbstractContainer) {
  56. $containerWriter = new Container($xmlWriter, $text);
  57. $containerWriter->write();
  58. }
  59. $xmlWriter->endElement(); // text:span
  60. $xmlWriter->endElement(); // text:h
  61. }
  62. /**
  63. * Test if element is same as first element in array
  64. *
  65. * @param \PhpOffice\PhpWord\Element\AbstractElement $elem
  66. *
  67. * @param \PhpOffice\PhpWord\Element\AbstractElement[] $elemarray
  68. *
  69. * @return bool
  70. */
  71. private static function compareToFirstElement($elem, $elemarray)
  72. {
  73. return $elem === $elemarray[0];
  74. }
  75. }