PageRenderTime 42ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/add-ons/PHPExcel/PHPExcel/RichText.php

https://github.com/jcplat/console-seolan
PHP | 306 lines | 116 code | 37 blank | 153 comment | 15 complexity | 27b746982c3617059d8f482c97e3d97d MD5 | raw file
Possible License(s): LGPL-2.0, LGPL-2.1, GPL-3.0, Apache-2.0, BSD-3-Clause
  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 1.7.1, 2009-11-02
  26. */
  27. /** PHPExcel root directory */
  28. if (!defined('PHPEXCEL_ROOT')) {
  29. /**
  30. * @ignore
  31. */
  32. define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../');
  33. }
  34. /** PHPExcel_IComparable */
  35. require_once PHPEXCEL_ROOT . 'PHPExcel/IComparable.php';
  36. /** PHPExcel_Cell */
  37. require_once PHPEXCEL_ROOT . 'PHPExcel/Cell.php';
  38. /** PHPExcel_Cell_DataType */
  39. require_once PHPEXCEL_ROOT . 'PHPExcel/Cell/DataType.php';
  40. /** PHPExcel_RichText_ITextElement */
  41. require_once PHPEXCEL_ROOT . 'PHPExcel/RichText/ITextElement.php';
  42. /** PHPExcel_RichText_TextElement */
  43. require_once PHPEXCEL_ROOT . 'PHPExcel/RichText/TextElement.php';
  44. /** PHPExcel_RichText_Run */
  45. require_once PHPEXCEL_ROOT . 'PHPExcel/RichText/Run.php';
  46. /** PHPExcel_Style_Font */
  47. require_once PHPEXCEL_ROOT . 'PHPExcel/Style/Font.php';
  48. /**
  49. * PHPExcel_RichText
  50. *
  51. * @category PHPExcel
  52. * @package PHPExcel_RichText
  53. * @copyright Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  54. */
  55. class PHPExcel_RichText implements PHPExcel_IComparable
  56. {
  57. /**
  58. * Rich text elements
  59. *
  60. * @var PHPExcel_RichText_ITextElement[]
  61. */
  62. private $_richTextElements;
  63. /**
  64. * Parent cell
  65. *
  66. * @var PHPExcel_Cell
  67. */
  68. private $_parent;
  69. /**
  70. * Create a new PHPExcel_RichText instance
  71. *
  72. * @param PHPExcel_Cell $pParent
  73. * @throws Exception
  74. */
  75. public function __construct(PHPExcel_Cell $pCell = null)
  76. {
  77. // Initialise variables
  78. $this->_richTextElements = array();
  79. // Set parent?
  80. if (!is_null($pCell)) {
  81. // Set parent cell
  82. $this->_parent = $pCell;
  83. // Add cell text and style
  84. if ($this->_parent->getValue() != "") {
  85. $objRun = new PHPExcel_RichText_Run($this->_parent->getValue());
  86. $objRun->setFont(clone $this->_parent->getParent()->getStyle($this->_parent->getCoordinate())->getFont());
  87. $this->addText($objRun);
  88. }
  89. // Set parent value
  90. $this->_parent->setValueExplicit($this, PHPExcel_Cell_DataType::TYPE_STRING);
  91. }
  92. }
  93. /**
  94. * Add text
  95. *
  96. * @param PHPExcel_RichText_ITextElement $pText Rich text element
  97. * @throws Exception
  98. * @return PHPExcel_RichText
  99. */
  100. public function addText(PHPExcel_RichText_ITextElement $pText = null)
  101. {
  102. $this->_richTextElements[] = $pText;
  103. return $this;
  104. }
  105. /**
  106. * Create text
  107. *
  108. * @param string $pText Text
  109. * @return PHPExcel_RichText_TextElement
  110. * @throws Exception
  111. */
  112. public function createText($pText = '')
  113. {
  114. $objText = new PHPExcel_RichText_TextElement($pText);
  115. $this->addText($objText);
  116. return $objText;
  117. }
  118. /**
  119. * Create text run
  120. *
  121. * @param string $pText Text
  122. * @return PHPExcel_RichText_Run
  123. * @throws Exception
  124. */
  125. public function createTextRun($pText = '')
  126. {
  127. $objText = new PHPExcel_RichText_Run($pText);
  128. $this->addText($objText);
  129. return $objText;
  130. }
  131. /**
  132. * Get plain text
  133. *
  134. * @return string
  135. */
  136. public function getPlainText()
  137. {
  138. // Return value
  139. $returnValue = '';
  140. // Loop through all PHPExcel_RichText_ITextElement
  141. foreach ($this->_richTextElements as $text) {
  142. $returnValue .= $text->getText();
  143. }
  144. // Return
  145. return $returnValue;
  146. }
  147. /**
  148. * Convert to string
  149. *
  150. * @return string
  151. */
  152. public function __toString() {
  153. return $this->getPlainText();
  154. }
  155. /**
  156. * Get Rich Text elements
  157. *
  158. * @return PHPExcel_RichText_ITextElement[]
  159. */
  160. public function getRichTextElements()
  161. {
  162. return $this->_richTextElements;
  163. }
  164. /**
  165. * Set Rich Text elements
  166. *
  167. * @param PHPExcel_RichText_ITextElement[] $pElements Array of elements
  168. * @throws Exception
  169. * @return PHPExcel_RichText
  170. */
  171. public function setRichTextElements($pElements = null)
  172. {
  173. if (is_array($pElements)) {
  174. $this->_richTextElements = $pElements;
  175. } else {
  176. throw new Exception("Invalid PHPExcel_RichText_ITextElement[] array passed.");
  177. }
  178. return $this;
  179. }
  180. /**
  181. * Get parent
  182. *
  183. * @return PHPExcel_Cell
  184. */
  185. public function getParent() {
  186. return $this->_parent;
  187. }
  188. /**
  189. * Set parent
  190. *
  191. * @param PHPExcel_Cell $value
  192. * @return PHPExcel_RichText
  193. */
  194. public function setParent(PHPExcel_Cell $value) {
  195. // Set parent
  196. $this->_parent = $value;
  197. // Set parent value
  198. $this->_parent->setValueExplicit($this, PHPExcel_Cell_DataType::TYPE_STRING);
  199. // Verify style information
  200. $sheet = $this->_parent->getParent();
  201. $cellFont = $sheet->getStyle($this->_parent->getCoordinate())->getFont()->getSharedComponent();
  202. foreach ($this->getRichTextElements() as $element) {
  203. if (!($element instanceof PHPExcel_RichText_Run)) continue;
  204. if ($element->getFont()->getHashCode() == $sheet->getDefaultStyle()->getFont()->getHashCode()) {
  205. if ($element->getFont()->getHashCode() != $cellFont->getHashCode()) {
  206. $element->setFont(clone $cellFont);
  207. }
  208. }
  209. }
  210. return $this;
  211. }
  212. /**
  213. * Get hash code
  214. *
  215. * @return string Hash code
  216. */
  217. public function getHashCode() {
  218. $hashElements = '';
  219. foreach ($this->_richTextElements as $element) {
  220. $hashElements .= $element->getHashCode();
  221. }
  222. return md5(
  223. $hashElements
  224. . __CLASS__
  225. );
  226. }
  227. /**
  228. * Hash index
  229. *
  230. * @var string
  231. */
  232. private $_hashIndex;
  233. /**
  234. * Get hash index
  235. *
  236. * Note that this index may vary during script execution! Only reliable moment is
  237. * while doing a write of a workbook and when changes are not allowed.
  238. *
  239. * @return string Hash index
  240. */
  241. public function getHashIndex() {
  242. return $this->_hashIndex;
  243. }
  244. /**
  245. * Set hash index
  246. *
  247. * Note that this index may vary during script execution! Only reliable moment is
  248. * while doing a write of a workbook and when changes are not allowed.
  249. *
  250. * @param string $value Hash index
  251. */
  252. public function setHashIndex($value) {
  253. $this->_hashIndex = $value;
  254. }
  255. /**
  256. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  257. */
  258. public function __clone() {
  259. $vars = get_object_vars($this);
  260. foreach ($vars as $key => $value) {
  261. if ($key == '_parent') continue;
  262. if (is_object($value)) {
  263. $this->$key = clone $value;
  264. } else {
  265. $this->$key = $value;
  266. }
  267. }
  268. }
  269. }