PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/branches/v1.5.5/Classes/PHPExcel/RichText.php

#
PHP | 235 lines | 89 code | 28 blank | 118 comment | 9 complexity | 81182ffb51dc65e1173de944dd973f8d MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.0, LGPL-2.1, GPL-3.0, LGPL-3.0
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2007 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
  23. * @copyright Copyright (c) 2006 - 2007 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/lgpl.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. /** PHPExcel_IComparable */
  28. require_once 'PHPExcel/IComparable.php';
  29. /** PHPExcel_Cell */
  30. require_once 'PHPExcel/Cell.php';
  31. /** PHPExcel_RichText_ITextElement */
  32. require_once 'PHPExcel/RichText/ITextElement.php';
  33. /** PHPExcel_RichText_TextElement */
  34. require_once 'PHPExcel/RichText/TextElement.php';
  35. /** PHPExcel_RichText_Run */
  36. require_once 'PHPExcel/RichText/Run.php';
  37. /** PHPExcel_Style_Font */
  38. require_once 'PHPExcel/Style/Font.php';
  39. /**
  40. * PHPExcel_RichText
  41. *
  42. * @category PHPExcel
  43. * @package PHPExcel
  44. * @copyright Copyright (c) 2006 - 2007 PHPExcel (http://www.codeplex.com/PHPExcel)
  45. */
  46. class PHPExcel_RichText implements PHPExcel_IComparable
  47. {
  48. /**
  49. * Rich text elements
  50. *
  51. * @var PHPExcel_RichText_ITextElement[]
  52. */
  53. private $_richTextElements;
  54. /**
  55. * Parent cell
  56. *
  57. * @var PHPExcel_Cell
  58. */
  59. private $_parent;
  60. /**
  61. * Create a new PHPExcel_RichText instance
  62. *
  63. * @param PHPExcel_Cell $pParent
  64. * @throws Exception
  65. */
  66. public function __construct(PHPExcel_Cell $pCell = null)
  67. {
  68. // Initialise variables
  69. $this->_richTextElements = array();
  70. // Set parent?
  71. if (!is_null($pCell)) {
  72. // Set parent cell
  73. $this->_parent = $pCell;
  74. // Add cell text and style
  75. if ($this->_parent->getValue() != "") {
  76. $objRun = new PHPExcel_RichText_Run($this->_parent->getValue());
  77. $objRun->setFont(clone $this->_parent->getParent()->getStyle($this->_parent->getCoordinate())->getFont());
  78. $this->addText($objRun);
  79. }
  80. // Set parent value
  81. $this->_parent->setValue($this);
  82. }
  83. }
  84. /**
  85. * Add text
  86. *
  87. * @param PHPExcel_RichText_ITextElement $pText Rich text element
  88. * @throws Exception
  89. */
  90. public function addText(PHPExcel_RichText_ITextElement $pText = null)
  91. {
  92. $this->_richTextElements[] = $pText;
  93. }
  94. /**
  95. * Create text
  96. *
  97. * @param string $pText Text
  98. * @return PHPExcel_RichText_TextElement
  99. * @throws Exception
  100. */
  101. public function createText($pText = '')
  102. {
  103. $objText = new PHPExcel_RichText_TextElement($pText);
  104. $this->addText($objText);
  105. return $objText;
  106. }
  107. /**
  108. * Create text run
  109. *
  110. * @param string $pText Text
  111. * @return PHPExcel_RichText_Run
  112. * @throws Exception
  113. */
  114. public function createTextRun($pText = '')
  115. {
  116. $objText = new PHPExcel_RichText_Run($pText);
  117. $this->addText($objText);
  118. return $objText;
  119. }
  120. /**
  121. * Get plain text
  122. *
  123. * @return string
  124. */
  125. public function getPlainText()
  126. {
  127. // Return value
  128. $returnValue = '';
  129. // Loop trough all PHPExcel_RichText_ITextElement
  130. foreach ($this->_richTextElements as $text) {
  131. $returnValue .= $text->getText();
  132. }
  133. // Return
  134. return $returnValue;
  135. }
  136. /**
  137. * Get Rich Text elements
  138. *
  139. * @return PHPExcel_RichText_ITextElement[]
  140. */
  141. public function getRichTextElements()
  142. {
  143. return $this->_richTextElements;
  144. }
  145. /**
  146. * Set Rich Text elements
  147. *
  148. * @param PHPExcel_RichText_ITextElement[] $pElements Array of elements
  149. * @throws Exception
  150. */
  151. public function setRichTextElements($pElements = null)
  152. {
  153. if (is_array($pElements)) {
  154. $this->_richTextElements = $pElements;
  155. } else {
  156. throw new Exception("Invalid PHPExcel_RichText_ITextElement[] array passed.");
  157. }
  158. }
  159. /**
  160. * Get parent
  161. *
  162. * @return PHPExcel_Cell
  163. */
  164. public function getParent() {
  165. return $this->_parent;
  166. }
  167. /**
  168. * Set parent
  169. *
  170. * @param PHPExcel_Cell $value
  171. */
  172. public function setParent(PHPExcel_Cell $value) {
  173. // Set parent
  174. $this->_parent = $value;
  175. // Set parent value
  176. $this->_parent->setValue($this);
  177. }
  178. /**
  179. * Get hash code
  180. *
  181. * @return string Hash code
  182. */
  183. public function getHashCode() {
  184. $hashElements = '';
  185. foreach ($this->_richTextElements as $element) {
  186. $hashElements .= $element->getHashCode();
  187. }
  188. return md5(
  189. $hashElements
  190. . __CLASS__
  191. );
  192. }
  193. /**
  194. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  195. */
  196. public function __clone() {
  197. $vars = get_object_vars($this);
  198. foreach ($vars as $key => $value) {
  199. if ($key == '_parent') continue;
  200. if (is_object($value)) {
  201. $this->$key = clone $value;
  202. } else {
  203. $this->$key = $value;
  204. }
  205. }
  206. }
  207. }