RichText.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * PHPExcel_RichText
  4. *
  5. * Copyright (c) 2006 - 2015 PHPExcel
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPExcel
  22. * @package PHPExcel_RichText
  23. * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. class PHPExcel_RichText implements PHPExcel_IComparable
  28. {
  29. /**
  30. * Rich text elements
  31. *
  32. * @var PHPExcel_RichText_ITextElement[]
  33. */
  34. private $richTextElements;
  35. /**
  36. * Create a new PHPExcel_RichText instance
  37. *
  38. * @param PHPExcel_Cell $pCell
  39. * @throws PHPExcel_Exception
  40. */
  41. public function __construct(PHPExcel_Cell $pCell = null)
  42. {
  43. // Initialise variables
  44. $this->richTextElements = array();
  45. // Rich-Text string attached to cell?
  46. if ($pCell !== null) {
  47. // Add cell text and style
  48. if ($pCell->getValue() != "") {
  49. $objRun = new PHPExcel_RichText_Run($pCell->getValue());
  50. $objRun->setFont(clone $pCell->getParent()->getStyle($pCell->getCoordinate())->getFont());
  51. $this->addText($objRun);
  52. }
  53. // Set parent value
  54. $pCell->setValueExplicit($this, PHPExcel_Cell_DataType::TYPE_STRING);
  55. }
  56. }
  57. /**
  58. * Add text
  59. *
  60. * @param PHPExcel_RichText_ITextElement $pText Rich text element
  61. * @throws PHPExcel_Exception
  62. * @return PHPExcel_RichText
  63. */
  64. public function addText(PHPExcel_RichText_ITextElement $pText = null)
  65. {
  66. $this->richTextElements[] = $pText;
  67. return $this;
  68. }
  69. /**
  70. * Create text
  71. *
  72. * @param string $pText Text
  73. * @return PHPExcel_RichText_TextElement
  74. * @throws PHPExcel_Exception
  75. */
  76. public function createText($pText = '')
  77. {
  78. $objText = new PHPExcel_RichText_TextElement($pText);
  79. $this->addText($objText);
  80. return $objText;
  81. }
  82. /**
  83. * Create text run
  84. *
  85. * @param string $pText Text
  86. * @return PHPExcel_RichText_Run
  87. * @throws PHPExcel_Exception
  88. */
  89. public function createTextRun($pText = '')
  90. {
  91. $objText = new PHPExcel_RichText_Run($pText);
  92. $this->addText($objText);
  93. return $objText;
  94. }
  95. /**
  96. * Get plain text
  97. *
  98. * @return string
  99. */
  100. public function getPlainText()
  101. {
  102. // Return value
  103. $returnValue = '';
  104. // Loop through all PHPExcel_RichText_ITextElement
  105. foreach ($this->richTextElements as $text) {
  106. $returnValue .= $text->getText();
  107. }
  108. // Return
  109. return $returnValue;
  110. }
  111. /**
  112. * Convert to string
  113. *
  114. * @return string
  115. */
  116. public function __toString()
  117. {
  118. return $this->getPlainText();
  119. }
  120. /**
  121. * Get Rich Text elements
  122. *
  123. * @return PHPExcel_RichText_ITextElement[]
  124. */
  125. public function getRichTextElements()
  126. {
  127. return $this->richTextElements;
  128. }
  129. /**
  130. * Set Rich Text elements
  131. *
  132. * @param PHPExcel_RichText_ITextElement[] $pElements Array of elements
  133. * @throws PHPExcel_Exception
  134. * @return PHPExcel_RichText
  135. */
  136. public function setRichTextElements($pElements = null)
  137. {
  138. if (is_array($pElements)) {
  139. $this->richTextElements = $pElements;
  140. } else {
  141. throw new PHPExcel_Exception("Invalid PHPExcel_RichText_ITextElement[] array passed.");
  142. }
  143. return $this;
  144. }
  145. /**
  146. * Get hash code
  147. *
  148. * @return string Hash code
  149. */
  150. public function getHashCode()
  151. {
  152. $hashElements = '';
  153. foreach ($this->richTextElements as $element) {
  154. $hashElements .= $element->getHashCode();
  155. }
  156. return md5(
  157. $hashElements .
  158. __CLASS__
  159. );
  160. }
  161. /**
  162. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  163. */
  164. public function __clone()
  165. {
  166. $vars = get_object_vars($this);
  167. foreach ($vars as $key => $value) {
  168. if (is_object($value)) {
  169. $this->$key = clone $value;
  170. } else {
  171. $this->$key = $value;
  172. }
  173. }
  174. }
  175. }