Text.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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\Element;
  18. use PhpOffice\PhpWord\Shared\Text as SharedText;
  19. use PhpOffice\PhpWord\Style\Font;
  20. use PhpOffice\PhpWord\Style\Paragraph;
  21. /**
  22. * Text element
  23. */
  24. class Text extends AbstractElement
  25. {
  26. /**
  27. * Text content
  28. *
  29. * @var string
  30. */
  31. protected $text;
  32. /**
  33. * Text style
  34. *
  35. * @var string|\PhpOffice\PhpWord\Style\Font
  36. */
  37. protected $fontStyle;
  38. /**
  39. * Paragraph style
  40. *
  41. * @var string|\PhpOffice\PhpWord\Style\Paragraph
  42. */
  43. protected $paragraphStyle;
  44. /**
  45. * Create a new Text Element
  46. *
  47. * @param string $text
  48. * @param mixed $fontStyle
  49. * @param mixed $paragraphStyle
  50. */
  51. public function __construct($text = null, $fontStyle = null, $paragraphStyle = null)
  52. {
  53. $this->setText($text);
  54. $paragraphStyle = $this->setParagraphStyle($paragraphStyle);
  55. $this->setFontStyle($fontStyle, $paragraphStyle);
  56. }
  57. /**
  58. * Set Text style
  59. *
  60. * @param string|array|\PhpOffice\PhpWord\Style\Font $style
  61. * @param string|array|\PhpOffice\PhpWord\Style\Paragraph $paragraphStyle
  62. * @return string|\PhpOffice\PhpWord\Style\Font
  63. */
  64. public function setFontStyle($style = null, $paragraphStyle = null)
  65. {
  66. if ($style instanceof Font) {
  67. $this->fontStyle = $style;
  68. $this->setParagraphStyle($paragraphStyle);
  69. } elseif (is_array($style)) {
  70. $this->fontStyle = new Font('text', $paragraphStyle);
  71. $this->fontStyle->setStyleByArray($style);
  72. } elseif (null === $style) {
  73. $this->fontStyle = new Font('text', $paragraphStyle);
  74. } else {
  75. $this->fontStyle = $style;
  76. $this->setParagraphStyle($paragraphStyle);
  77. }
  78. return $this->fontStyle;
  79. }
  80. /**
  81. * Get Text style
  82. *
  83. * @return string|\PhpOffice\PhpWord\Style\Font
  84. */
  85. public function getFontStyle()
  86. {
  87. return $this->fontStyle;
  88. }
  89. /**
  90. * Set Paragraph style
  91. *
  92. * @param string|array|\PhpOffice\PhpWord\Style\Paragraph $style
  93. * @return string|\PhpOffice\PhpWord\Style\Paragraph
  94. */
  95. public function setParagraphStyle($style = null)
  96. {
  97. if (is_array($style)) {
  98. $this->paragraphStyle = new Paragraph();
  99. $this->paragraphStyle->setStyleByArray($style);
  100. } elseif ($style instanceof Paragraph) {
  101. $this->paragraphStyle = $style;
  102. } elseif (null === $style) {
  103. $this->paragraphStyle = new Paragraph();
  104. } else {
  105. $this->paragraphStyle = $style;
  106. }
  107. return $this->paragraphStyle;
  108. }
  109. /**
  110. * Get Paragraph style
  111. *
  112. * @return string|\PhpOffice\PhpWord\Style\Paragraph
  113. */
  114. public function getParagraphStyle()
  115. {
  116. return $this->paragraphStyle;
  117. }
  118. /**
  119. * Set text content
  120. *
  121. * @param string $text
  122. * @return self
  123. */
  124. public function setText($text)
  125. {
  126. $this->text = SharedText::toUTF8($text);
  127. return $this;
  128. }
  129. /**
  130. * Get Text content
  131. *
  132. * @return string
  133. */
  134. public function getText()
  135. {
  136. return $this->text;
  137. }
  138. }