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

/branches/wi6857-memory/Classes/PHPExcel/Style.php

#
PHP | 471 lines | 170 code | 49 blank | 252 comment | 14 complexity | 70dedf3227799d8fa628d28137b879b3 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_Style
  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_Style_Color */
  28. require_once 'PHPExcel/Style/Color.php';
  29. /** PHPExcel_Style_Font */
  30. require_once 'PHPExcel/Style/Font.php';
  31. /** PHPExcel_Style_Fill */
  32. require_once 'PHPExcel/Style/Fill.php';
  33. /** PHPExcel_Style_Borders */
  34. require_once 'PHPExcel/Style/Borders.php';
  35. /** PHPExcel_Style_Alignment */
  36. require_once 'PHPExcel/Style/Alignment.php';
  37. /** PHPExcel_Style_NumberFormat */
  38. require_once 'PHPExcel/Style/NumberFormat.php';
  39. /** PHPExcel_Style_Conditional */
  40. require_once 'PHPExcel/Style/Conditional.php';
  41. /** PHPExcel_Style_Protection */
  42. require_once 'PHPExcel/Style/Protection.php';
  43. /** PHPExcel_IComparable */
  44. require_once 'PHPExcel/IComparable.php';
  45. /**
  46. * PHPExcel_Style
  47. *
  48. * @category PHPExcel
  49. * @package PHPExcel_Style
  50. * @copyright Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  51. */
  52. class PHPExcel_Style implements PHPExcel_IComparable
  53. {
  54. /**
  55. * Font
  56. *
  57. * @var PHPExcel_Style_Font
  58. */
  59. private $_font;
  60. /**
  61. * Fill
  62. *
  63. * @var PHPExcel_Style_Fill
  64. */
  65. private $_fill;
  66. /**
  67. * Borders
  68. *
  69. * @var PHPExcel_Style_Borders
  70. */
  71. private $_borders;
  72. /**
  73. * Alignment
  74. *
  75. * @var PHPExcel_Style_Alignment
  76. */
  77. private $_alignment;
  78. /**
  79. * Number Format
  80. *
  81. * @var PHPExcel_Style_NumberFormat
  82. */
  83. private $_numberFormat;
  84. /**
  85. * Conditional styles
  86. *
  87. * @var PHPExcel_Style_Conditional[]
  88. */
  89. private $_conditionalStyles;
  90. /**
  91. * Protection
  92. *
  93. * @var PHPExcel_Style_Protection
  94. */
  95. private $_protection;
  96. /**
  97. * Style supervisor?
  98. *
  99. * @var boolean
  100. */
  101. private $_isSupervisor;
  102. /**
  103. * Parent. Only used for style supervisor
  104. *
  105. * @var PHPExcel
  106. */
  107. private $_parent;
  108. /**
  109. * Index of style in collection. Only used for real style.
  110. *
  111. * @var int
  112. */
  113. private $_index;
  114. /**
  115. * Create a new PHPExcel_Style
  116. *
  117. * @param boolean $isSupervisor
  118. */
  119. public function __construct($isSupervisor = false)
  120. {
  121. // Supervisor?
  122. $this->_isSupervisor = $isSupervisor;
  123. // Initialise values
  124. $this->_conditionalStyles = array();
  125. $this->_font = new PHPExcel_Style_Font($isSupervisor);
  126. $this->_fill = new PHPExcel_Style_Fill($isSupervisor);
  127. $this->_borders = new PHPExcel_Style_Borders($isSupervisor);
  128. $this->_alignment = new PHPExcel_Style_Alignment($isSupervisor);
  129. $this->_numberFormat = new PHPExcel_Style_NumberFormat($isSupervisor);
  130. $this->_protection = new PHPExcel_Style_Protection($isSupervisor);
  131. // bind parent if we are a supervisor
  132. if ($isSupervisor) {
  133. $this->_font->bindParent($this);
  134. $this->_fill->bindParent($this);
  135. $this->_borders->bindParent($this);
  136. $this->_alignment->bindParent($this);
  137. $this->_numberFormat->bindParent($this);
  138. $this->_protection->bindParent($this);
  139. }
  140. }
  141. /**
  142. * Bind parent. Only used for supervisor
  143. *
  144. * @param PHPExcel $parent
  145. */
  146. public function bindParent($parent)
  147. {
  148. $this->_parent = $parent;
  149. }
  150. /**
  151. * Is this a supervisor or a real style component?
  152. *
  153. * @return boolean
  154. */
  155. public function getIsSupervisor()
  156. {
  157. return $this->_isSupervisor;
  158. }
  159. /**
  160. * Get the shared style component for the currently active cell in currently active sheet.
  161. * Only used for style supervisor
  162. *
  163. * @return PHPExcel_Style
  164. */
  165. public function getSharedComponent()
  166. {
  167. $activeSheet = $this->getActiveSheet();
  168. $selectedCell = $this->getSelectedCell(); // e.g. 'A1'
  169. if ($activeSheet->cellExists($selectedCell)) {
  170. $cell = $activeSheet->getCell($selectedCell);
  171. $xfIndex = $cell->getXfIndex();
  172. } else {
  173. $xfIndex = 0;
  174. }
  175. $activeStyle = $this->_parent->getCellXfByIndex($xfIndex);
  176. return $activeStyle;
  177. }
  178. /**
  179. * Get the currently active sheet. Only used for supervisor
  180. *
  181. * @return PHPExcel_Worksheet
  182. */
  183. public function getActiveSheet()
  184. {
  185. return $this->_parent->getActiveSheet();
  186. }
  187. /**
  188. * Get the currently active cell coordinate in currently active sheet.
  189. * Only used for supervisor
  190. *
  191. * @return string E.g. 'A1'
  192. */
  193. public function getSelectedCell()
  194. {
  195. return $this->_parent->getActiveSheet()->getSelectedCell();
  196. }
  197. /**
  198. * Get parent. Only used for style supervisor
  199. *
  200. * @return PHPExcel
  201. */
  202. public function getParent()
  203. {
  204. return $this->_parent;
  205. }
  206. /**
  207. * Apply styles from array
  208. *
  209. * <code>
  210. * $objPHPExcel->getActiveSheet()->getStyle('B2')->applyFromArray(
  211. * array(
  212. * 'font' => array(
  213. * 'name' => 'Arial',
  214. * 'bold' => true,
  215. * 'italic' => false,
  216. * 'underline' => PHPExcel_Style_Font::UNDERLINE_DOUBLE,
  217. * 'strike' => false,
  218. * 'color' => array(
  219. * 'rgb' => '808080'
  220. * )
  221. * ),
  222. * 'borders' => array(
  223. * 'bottom' => array(
  224. * 'style' => PHPExcel_Style_Border::BORDER_DASHDOT,
  225. * 'color' => array(
  226. * 'rgb' => '808080'
  227. * )
  228. * ),
  229. * 'top' => array(
  230. * 'style' => PHPExcel_Style_Border::BORDER_DASHDOT,
  231. * 'color' => array(
  232. * 'rgb' => '808080'
  233. * )
  234. * )
  235. * )
  236. * )
  237. * );
  238. * </code>
  239. *
  240. * @param array $pStyles Array containing style information
  241. * @throws Exception
  242. */
  243. public function applyFromArray($pStyles = null) {
  244. if (is_array($pStyles)) {
  245. if (array_key_exists('fill', $pStyles)) {
  246. $this->getFill()->applyFromArray($pStyles['fill']);
  247. }
  248. if (array_key_exists('font', $pStyles)) {
  249. $this->getFont()->applyFromArray($pStyles['font']);
  250. }
  251. if (array_key_exists('borders', $pStyles)) {
  252. $this->getBorders()->applyFromArray($pStyles['borders']);
  253. }
  254. if (array_key_exists('alignment', $pStyles)) {
  255. $this->getAlignment()->applyFromArray($pStyles['alignment']);
  256. }
  257. if (array_key_exists('numberformat', $pStyles)) {
  258. $this->getNumberFormat()->applyFromArray($pStyles['numberformat']);
  259. }
  260. if (array_key_exists('protection', $pStyles)) {
  261. $this->getProtection()->applyFromArray($pStyles['protection']);
  262. }
  263. } else {
  264. throw new Exception("Invalid style array passed.");
  265. }
  266. }
  267. /**
  268. * Get Fill
  269. *
  270. * @return PHPExcel_Style_Fill
  271. */
  272. public function getFill() {
  273. return $this->_fill;
  274. }
  275. /**
  276. * Get Font
  277. *
  278. * @return PHPExcel_Style_Font
  279. */
  280. public function getFont() {
  281. return $this->_font;
  282. }
  283. /**
  284. * Set font
  285. *
  286. * @param PHPExcel_Style_Font $font
  287. */
  288. public function setFont(PHPExcel_Style_Font $font)
  289. {
  290. $this->_font = $font;
  291. }
  292. /**
  293. * Get Borders
  294. *
  295. * @return PHPExcel_Style_Borders
  296. */
  297. public function getBorders() {
  298. return $this->_borders;
  299. }
  300. /**
  301. * Get Alignment
  302. *
  303. * @return PHPExcel_Style_Alignment
  304. */
  305. public function getAlignment() {
  306. return $this->_alignment;
  307. }
  308. /**
  309. * Get Number Format
  310. *
  311. * @return PHPExcel_Style_NumberFormat
  312. */
  313. public function getNumberFormat() {
  314. return $this->_numberFormat;
  315. }
  316. /**
  317. * Get Conditional Styles. Only used on supervisor.
  318. *
  319. * @return PHPExcel_Style_Conditional[]
  320. */
  321. public function getConditionalStyles() {
  322. return $this->getActiveSheet()->getConditionalStyles($this->getSelectedCell());
  323. }
  324. /**
  325. * Set Conditional Styles. Only used on supervisor.
  326. *
  327. * @param PHPExcel_Style_Conditional[] $pValue Array of condtional styles
  328. */
  329. public function setConditionalStyles($pValue = null) {
  330. if (is_array($pValue)) {
  331. $this->getActiveSheet()->setConditionalStyles($this->getSelectedCell(), $pValue);
  332. }
  333. }
  334. /**
  335. * Get Protection
  336. *
  337. * @return PHPExcel_Style_Protection
  338. */
  339. public function getProtection() {
  340. return $this->_protection;
  341. }
  342. /**
  343. * Get hash code
  344. *
  345. * @return string Hash code
  346. */
  347. public function getHashCode() {
  348. $hashConditionals = '';
  349. foreach ($this->_conditionalStyles as $conditional) {
  350. $hashConditionals .= $conditional->getHashCode();
  351. }
  352. return md5(
  353. $this->getFill()->getHashCode()
  354. . $this->getFont()->getHashCode()
  355. . $this->getBorders()->getHashCode()
  356. . $this->getAlignment()->getHashCode()
  357. . $this->getNumberFormat()->getHashCode()
  358. . $hashConditionals
  359. . $this->getProtection()->getHashCode()
  360. . __CLASS__
  361. );
  362. }
  363. /**
  364. * Hash index
  365. *
  366. * @var string
  367. */
  368. private $_hashIndex;
  369. /**
  370. * Get hash index
  371. *
  372. * Note that this index may vary during script execution! Only reliable moment is
  373. * while doing a write of a workbook and when changes are not allowed.
  374. *
  375. * @return string Hash index
  376. */
  377. public function getHashIndex() {
  378. return $this->_hashIndex;
  379. }
  380. /**
  381. * Set hash index
  382. *
  383. * Note that this index may vary during script execution! Only reliable moment is
  384. * while doing a write of a workbook and when changes are not allowed.
  385. *
  386. * @param string $value Hash index
  387. */
  388. public function setHashIndex($value) {
  389. $this->_hashIndex = $value;
  390. }
  391. /**
  392. * Get own index in style collection
  393. *
  394. * @return int
  395. */
  396. public function getIndex()
  397. {
  398. return $this->_index;
  399. }
  400. /**
  401. * Set own index in style collection
  402. *
  403. * @param int $pValue
  404. */
  405. public function setIndex($pValue)
  406. {
  407. $this->_index = $pValue;
  408. }
  409. /**
  410. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  411. */
  412. public function __clone() {
  413. $vars = get_object_vars($this);
  414. foreach ($vars as $key => $value) {
  415. if (is_object($value)) {
  416. $this->$key = clone $value;
  417. } else {
  418. $this->$key = $value;
  419. }
  420. }
  421. }
  422. }