Sin descripción

RichText.php 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2014 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 - 2014 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. /**
  28. * PHPExcel_RichText
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_RichText
  32. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_RichText implements PHPExcel_IComparable
  35. {
  36. /**
  37. * Rich text elements
  38. *
  39. * @var PHPExcel_RichText_ITextElement[]
  40. */
  41. private $_richTextElements;
  42. /**
  43. * Create a new PHPExcel_RichText instance
  44. *
  45. * @param PHPExcel_Cell $pCell
  46. * @throws PHPExcel_Exception
  47. */
  48. public function __construct(PHPExcel_Cell $pCell = null)
  49. {
  50. // Initialise variables
  51. $this->_richTextElements = array();
  52. // Rich-Text string attached to cell?
  53. if ($pCell !== NULL) {
  54. // Add cell text and style
  55. if ($pCell->getValue() != "") {
  56. $objRun = new PHPExcel_RichText_Run($pCell->getValue());
  57. $objRun->setFont(clone $pCell->getParent()->getStyle($pCell->getCoordinate())->getFont());
  58. $this->addText($objRun);
  59. }
  60. // Set parent value
  61. $pCell->setValueExplicit($this, PHPExcel_Cell_DataType::TYPE_STRING);
  62. }
  63. }
  64. /**
  65. * Add text
  66. *
  67. * @param PHPExcel_RichText_ITextElement $pText Rich text element
  68. * @throws PHPExcel_Exception
  69. * @return PHPExcel_RichText
  70. */
  71. public function addText(PHPExcel_RichText_ITextElement $pText = null)
  72. {
  73. $this->_richTextElements[] = $pText;
  74. return $this;
  75. }
  76. /**
  77. * Create text
  78. *
  79. * @param string $pText Text
  80. * @return PHPExcel_RichText_TextElement
  81. * @throws PHPExcel_Exception
  82. */
  83. public function createText($pText = '')
  84. {
  85. $objText = new PHPExcel_RichText_TextElement($pText);
  86. $this->addText($objText);
  87. return $objText;
  88. }
  89. /**
  90. * Create text run
  91. *
  92. * @param string $pText Text
  93. * @return PHPExcel_RichText_Run
  94. * @throws PHPExcel_Exception
  95. */
  96. public function createTextRun($pText = '')
  97. {
  98. $objText = new PHPExcel_RichText_Run($pText);
  99. $this->addText($objText);
  100. return $objText;
  101. }
  102. /**
  103. * Get plain text
  104. *
  105. * @return string
  106. */
  107. public function getPlainText()
  108. {
  109. // Return value
  110. $returnValue = '';
  111. // Loop through all PHPExcel_RichText_ITextElement
  112. foreach ($this->_richTextElements as $text) {
  113. $returnValue .= $text->getText();
  114. }
  115. // Return
  116. return $returnValue;
  117. }
  118. /**
  119. * Convert to string
  120. *
  121. * @return string
  122. */
  123. public function __toString()
  124. {
  125. return $this->getPlainText();
  126. }
  127. /**
  128. * Get Rich Text elements
  129. *
  130. * @return PHPExcel_RichText_ITextElement[]
  131. */
  132. public function getRichTextElements()
  133. {
  134. return $this->_richTextElements;
  135. }
  136. /**
  137. * Set Rich Text elements
  138. *
  139. * @param PHPExcel_RichText_ITextElement[] $pElements Array of elements
  140. * @throws PHPExcel_Exception
  141. * @return PHPExcel_RichText
  142. */
  143. public function setRichTextElements($pElements = null)
  144. {
  145. if (is_array($pElements)) {
  146. $this->_richTextElements = $pElements;
  147. } else {
  148. throw new PHPExcel_Exception("Invalid PHPExcel_RichText_ITextElement[] array passed.");
  149. }
  150. return $this;
  151. }
  152. /**
  153. * Get hash code
  154. *
  155. * @return string Hash code
  156. */
  157. public function getHashCode()
  158. {
  159. $hashElements = '';
  160. foreach ($this->_richTextElements as $element) {
  161. $hashElements .= $element->getHashCode();
  162. }
  163. return md5(
  164. $hashElements
  165. . __CLASS__
  166. );
  167. }
  168. /**
  169. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  170. */
  171. public function __clone()
  172. {
  173. $vars = get_object_vars($this);
  174. foreach ($vars as $key => $value) {
  175. if (is_object($value)) {
  176. $this->$key = clone $value;
  177. } else {
  178. $this->$key = $value;
  179. }
  180. }
  181. }
  182. }