RichText.php 4.2 KB

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