RichText.Class.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: phperstar
  5. * Date: 2020/8/11
  6. * Time: 4:13 PM
  7. */
  8. namespace Util\PHPExcel;
  9. class RichText
  10. {
  11. /**
  12. * Rich text elements
  13. *
  14. * @var PHPExcel_RichText_ITextElement[]
  15. */
  16. private $richTextElements;
  17. /**
  18. * Create a new PHPExcel_RichText instance
  19. *
  20. * @param PHPExcel_Cell $pCell
  21. * @throws PHPExcel_Exception
  22. */
  23. public function __construct(PHPExcel_Cell $pCell = null)
  24. {
  25. // Initialise variables
  26. $this->richTextElements = array();
  27. // Rich-Text string attached to cell?
  28. if ($pCell !== null) {
  29. // Add cell text and style
  30. if ($pCell->getValue() != "") {
  31. $objRun = new PHPExcel_RichText_Run($pCell->getValue());
  32. $objRun->setFont(clone $pCell->getParent()->getStyle($pCell->getCoordinate())->getFont());
  33. $this->addText($objRun);
  34. }
  35. // Set parent value
  36. $pCell->setValueExplicit($this, PHPExcel_Cell_DataType::TYPE_STRING);
  37. }
  38. }
  39. /**
  40. * Add text
  41. *
  42. * @param PHPExcel_RichText_ITextElement $pText Rich text element
  43. * @throws PHPExcel_Exception
  44. * @return PHPExcel_RichText
  45. */
  46. public function addText(PHPExcel_RichText_ITextElement $pText = null)
  47. {
  48. $this->richTextElements[] = $pText;
  49. return $this;
  50. }
  51. /**
  52. * Create text
  53. *
  54. * @param string $pText Text
  55. * @return PHPExcel_RichText_TextElement
  56. * @throws PHPExcel_Exception
  57. */
  58. public function createText($pText = '')
  59. {
  60. $objText = new PHPExcel_RichText_TextElement($pText);
  61. $this->addText($objText);
  62. return $objText;
  63. }
  64. /**
  65. * Create text run
  66. *
  67. * @param string $pText Text
  68. * @return PHPExcel_RichText_Run
  69. * @throws PHPExcel_Exception
  70. */
  71. public function createTextRun($pText = '')
  72. {
  73. $objText = new PHPExcel_RichText_Run($pText);
  74. $this->addText($objText);
  75. return $objText;
  76. }
  77. /**
  78. * Get plain text
  79. *
  80. * @return string
  81. */
  82. public function getPlainText()
  83. {
  84. // Return value
  85. $returnValue = '';
  86. // Loop through all PHPExcel_RichText_ITextElement
  87. foreach ($this->richTextElements as $text) {
  88. $returnValue .= $text->getText();
  89. }
  90. // Return
  91. return $returnValue;
  92. }
  93. /**
  94. * Convert to string
  95. *
  96. * @return string
  97. */
  98. public function __toString()
  99. {
  100. return $this->getPlainText();
  101. }
  102. /**
  103. * Get Rich Text elements
  104. *
  105. * @return PHPExcel_RichText_ITextElement[]
  106. */
  107. public function getRichTextElements()
  108. {
  109. return $this->richTextElements;
  110. }
  111. /**
  112. * Set Rich Text elements
  113. *
  114. * @param PHPExcel_RichText_ITextElement[] $pElements Array of elements
  115. * @throws PHPExcel_Exception
  116. * @return PHPExcel_RichText
  117. */
  118. public function setRichTextElements($pElements = null)
  119. {
  120. if (is_array($pElements)) {
  121. $this->richTextElements = $pElements;
  122. } else {
  123. throw new PHPExcel_Exception("Invalid PHPExcel_RichText_ITextElement[] array passed.");
  124. }
  125. return $this;
  126. }
  127. /**
  128. * Get hash code
  129. *
  130. * @return string Hash code
  131. */
  132. public function getHashCode()
  133. {
  134. $hashElements = '';
  135. foreach ($this->richTextElements as $element) {
  136. $hashElements .= $element->getHashCode();
  137. }
  138. return md5(
  139. $hashElements .
  140. __CLASS__
  141. );
  142. }
  143. /**
  144. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  145. */
  146. public function __clone()
  147. {
  148. $vars = get_object_vars($this);
  149. foreach ($vars as $key => $value) {
  150. if (is_object($value)) {
  151. $this->$key = clone $value;
  152. } else {
  153. $this->$key = $value;
  154. }
  155. }
  156. }
  157. }