PageRenderTime 57ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
PHP | 280 lines | 106 code | 34 blank | 140 comment | 14 complexity | 7391d077a8d4f68faa05135c5bfc0ea2 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 - 2009 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 - 2009 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. /** 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_RichText
  44. * @copyright Copyright (c) 2006 - 2009 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. // Verify style information
  178. $sheet = $this->_parent->getParent();
  179. $cellFont = $sheet->getStyle($this->_parent->getCoordinate())->getFont();
  180. foreach ($this->getRichTextElements() as $element) {
  181. if (!($element instanceof PHPExcel_RichText_Run)) continue;
  182. if ($element->getFont()->getHashCode() == $sheet->getDefaultStyle()->getFont()->getHashCode()) {
  183. if ($element->getFont()->getHashCode() != $cellFont->getHashCode()) {
  184. $element->setFont(clone $cellFont);
  185. }
  186. }
  187. }
  188. }
  189. /**
  190. * Get hash code
  191. *
  192. * @return string Hash code
  193. */
  194. public function getHashCode() {
  195. $hashElements = '';
  196. foreach ($this->_richTextElements as $element) {
  197. $hashElements .= $element->getHashCode();
  198. }
  199. return md5(
  200. $hashElements
  201. . __CLASS__
  202. );
  203. }
  204. /**
  205. * Hash index
  206. *
  207. * @var string
  208. */
  209. private $_hashIndex;
  210. /**
  211. * Get hash index
  212. *
  213. * Note that this index may vary during script execution! Only reliable moment is
  214. * while doing a write of a workbook and when changes are not allowed.
  215. *
  216. * @return string Hash index
  217. */
  218. public function getHashIndex() {
  219. return $this->_hashIndex;
  220. }
  221. /**
  222. * Set hash index
  223. *
  224. * Note that this index may vary during script execution! Only reliable moment is
  225. * while doing a write of a workbook and when changes are not allowed.
  226. *
  227. * @param string $value Hash index
  228. */
  229. public function setHashIndex($value) {
  230. $this->_hashIndex = $value;
  231. }
  232. /**
  233. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  234. */
  235. public function __clone() {
  236. $vars = get_object_vars($this);
  237. foreach ($vars as $key => $value) {
  238. if ($key == '_parent') continue;
  239. if (is_object($value)) {
  240. $this->$key = clone $value;
  241. } else {
  242. $this->$key = $value;
  243. }
  244. }
  245. }
  246. }