PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/PHPExcel_1.7.8-with_documentation-msoffice_format/Classes/PHPExcel/RichText.php

https://bitbucket.org/izubizarreta/https-bitbucket.org-bityvip
PHP | 196 lines | 78 code | 17 blank | 101 comment | 7 complexity | 8178a5d057eeca2e4c85f8e67c5fccd4 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.0, JSON, GPL-2.0, BSD-3-Clause, LGPL-2.1, MIT
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2012 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 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.8, 2012-10-12
  26. */
  27. /**
  28. * PHPExcel_RichText
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_RichText
  32. * @copyright Copyright (c) 2006 - 2012 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 $pParent
  46. * @throws 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 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 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 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. return $this->getPlainText();
  125. }
  126. /**
  127. * Get Rich Text elements
  128. *
  129. * @return PHPExcel_RichText_ITextElement[]
  130. */
  131. public function getRichTextElements()
  132. {
  133. return $this->_richTextElements;
  134. }
  135. /**
  136. * Set Rich Text elements
  137. *
  138. * @param PHPExcel_RichText_ITextElement[] $pElements Array of elements
  139. * @throws Exception
  140. * @return PHPExcel_RichText
  141. */
  142. public function setRichTextElements($pElements = null)
  143. {
  144. if (is_array($pElements)) {
  145. $this->_richTextElements = $pElements;
  146. } else {
  147. throw new Exception("Invalid PHPExcel_RichText_ITextElement[] array passed.");
  148. }
  149. return $this;
  150. }
  151. /**
  152. * Get hash code
  153. *
  154. * @return string Hash code
  155. */
  156. public function getHashCode() {
  157. $hashElements = '';
  158. foreach ($this->_richTextElements as $element) {
  159. $hashElements .= $element->getHashCode();
  160. }
  161. return md5(
  162. $hashElements
  163. . __CLASS__
  164. );
  165. }
  166. /**
  167. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  168. */
  169. public function __clone() {
  170. $vars = get_object_vars($this);
  171. foreach ($vars as $key => $value) {
  172. if (is_object($value)) {
  173. $this->$key = clone $value;
  174. } else {
  175. $this->$key = $value;
  176. }
  177. }
  178. }
  179. }