RichText.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\RichText;
  3. use PhpOffice\PhpSpreadsheet\Cell\Cell;
  4. use PhpOffice\PhpSpreadsheet\Cell\DataType;
  5. use PhpOffice\PhpSpreadsheet\Exception;
  6. use PhpOffice\PhpSpreadsheet\IComparable;
  7. class RichText implements IComparable
  8. {
  9. /**
  10. * Rich text elements.
  11. *
  12. * @var ITextElement[]
  13. */
  14. private $richTextElements;
  15. /**
  16. * Create a new RichText instance.
  17. *
  18. * @param Cell $pCell
  19. *
  20. * @throws Exception
  21. */
  22. public function __construct(Cell $pCell = null)
  23. {
  24. // Initialise variables
  25. $this->richTextElements = [];
  26. // Rich-Text string attached to cell?
  27. if ($pCell !== null) {
  28. // Add cell text and style
  29. if ($pCell->getValue() != '') {
  30. $objRun = new Run($pCell->getValue());
  31. $objRun->setFont(clone $pCell->getWorksheet()->getStyle($pCell->getCoordinate())->getFont());
  32. $this->addText($objRun);
  33. }
  34. // Set parent value
  35. $pCell->setValueExplicit($this, DataType::TYPE_STRING);
  36. }
  37. }
  38. /**
  39. * Add text.
  40. *
  41. * @param ITextElement $pText Rich text element
  42. *
  43. * @return RichText
  44. */
  45. public function addText(ITextElement $pText)
  46. {
  47. $this->richTextElements[] = $pText;
  48. return $this;
  49. }
  50. /**
  51. * Create text.
  52. *
  53. * @param string $pText Text
  54. *
  55. * @throws Exception
  56. *
  57. * @return TextElement
  58. */
  59. public function createText($pText)
  60. {
  61. $objText = new TextElement($pText);
  62. $this->addText($objText);
  63. return $objText;
  64. }
  65. /**
  66. * Create text run.
  67. *
  68. * @param string $pText Text
  69. *
  70. * @throws Exception
  71. *
  72. * @return Run
  73. */
  74. public function createTextRun($pText)
  75. {
  76. $objText = new Run($pText);
  77. $this->addText($objText);
  78. return $objText;
  79. }
  80. /**
  81. * Get plain text.
  82. *
  83. * @return string
  84. */
  85. public function getPlainText()
  86. {
  87. // Return value
  88. $returnValue = '';
  89. // Loop through all ITextElements
  90. foreach ($this->richTextElements as $text) {
  91. $returnValue .= $text->getText();
  92. }
  93. return $returnValue;
  94. }
  95. /**
  96. * Convert to string.
  97. *
  98. * @return string
  99. */
  100. public function __toString()
  101. {
  102. return $this->getPlainText();
  103. }
  104. /**
  105. * Get Rich Text elements.
  106. *
  107. * @return ITextElement[]
  108. */
  109. public function getRichTextElements()
  110. {
  111. return $this->richTextElements;
  112. }
  113. /**
  114. * Set Rich Text elements.
  115. *
  116. * @param ITextElement[] $textElements Array of elements
  117. *
  118. * @return RichText
  119. */
  120. public function setRichTextElements(array $textElements)
  121. {
  122. $this->richTextElements = $textElements;
  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. }