Nenhuma descrição

AbstractElement.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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\AbstractElement as Element;
  19. use PhpOffice\PhpWord\Shared\String;
  20. use PhpOffice\PhpWord\Shared\XMLWriter;
  21. /**
  22. * Abstract element writer
  23. *
  24. * @since 0.11.0
  25. */
  26. abstract class AbstractElement
  27. {
  28. /**
  29. * XML writer
  30. *
  31. * @var \PhpOffice\PhpWord\Shared\XMLWriter
  32. */
  33. private $xmlWriter;
  34. /**
  35. * Element
  36. *
  37. * @var \PhpOffice\PhpWord\Element\AbstractElement
  38. */
  39. private $element;
  40. /**
  41. * Without paragraph
  42. *
  43. * @var bool
  44. */
  45. protected $withoutP = false;
  46. /**
  47. * Write element
  48. */
  49. abstract public function write();
  50. /**
  51. * Create new instance
  52. *
  53. * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
  54. * @param \PhpOffice\PhpWord\Element\AbstractElement $element
  55. * @param bool $withoutP
  56. */
  57. public function __construct(XMLWriter $xmlWriter, Element $element, $withoutP = false)
  58. {
  59. $this->xmlWriter = $xmlWriter;
  60. $this->element = $element;
  61. $this->withoutP = $withoutP;
  62. }
  63. /**
  64. * Get XML Writer
  65. *
  66. * @return \PhpOffice\PhpWord\Shared\XMLWriter
  67. */
  68. protected function getXmlWriter()
  69. {
  70. return $this->xmlWriter;
  71. }
  72. /**
  73. * Get element
  74. *
  75. * @return \PhpOffice\PhpWord\Element\AbstractElement
  76. */
  77. protected function getElement()
  78. {
  79. return $this->element;
  80. }
  81. /**
  82. * Start w:p DOM element.
  83. *
  84. * @uses \PhpOffice\PhpWord\Writer\Word2007\Element\PageBreak::write()
  85. * @return void
  86. */
  87. protected function startElementP()
  88. {
  89. if (!$this->withoutP) {
  90. $this->xmlWriter->startElement('w:p');
  91. // Paragraph style
  92. if (method_exists($this->element, 'getParagraphStyle')) {
  93. $this->writeParagraphStyle();
  94. }
  95. }
  96. }
  97. /**
  98. * End w:p DOM element.
  99. *
  100. * @return void
  101. */
  102. protected function endElementP()
  103. {
  104. if (!$this->withoutP) {
  105. $this->xmlWriter->endElement(); // w:p
  106. }
  107. }
  108. /**
  109. * Write ending.
  110. *
  111. * @return void
  112. */
  113. protected function writeParagraphStyle()
  114. {
  115. $this->writeTextStyle('Paragraph');
  116. }
  117. /**
  118. * Write ending.
  119. *
  120. * @return void
  121. */
  122. protected function writeFontStyle()
  123. {
  124. $this->writeTextStyle('Font');
  125. }
  126. /**
  127. * Write text style.
  128. *
  129. * @param string $styleType Font|Paragraph
  130. * @return void
  131. */
  132. private function writeTextStyle($styleType)
  133. {
  134. $method = "get{$styleType}Style";
  135. $class = "PhpOffice\\PhpWord\\Writer\\Word2007\\Style\\{$styleType}";
  136. $styleObject = $this->element->$method();
  137. $styleWriter = new $class($this->xmlWriter, $styleObject);
  138. if (method_exists($styleWriter, 'setIsInline')) {
  139. $styleWriter->setIsInline(true);
  140. }
  141. /** @var \PhpOffice\PhpWord\Writer\Word2007\Style\AbstractStyle $styleWriter */
  142. $styleWriter->write();
  143. }
  144. /**
  145. * Convert text to valid format
  146. *
  147. * @param string $text
  148. * @return string
  149. */
  150. protected function getText($text)
  151. {
  152. return String::controlCharacterPHP2OOXML($text);
  153. }
  154. }