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

/branches/v1.6.1/Classes/PHPExcel/Style.php

#
PHP | 300 lines | 106 code | 30 blank | 164 comment | 11 complexity | 1f8c786eb460e6abf7c4823a22972ad4 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 - 2008 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 - 2008 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_Cell
  50. * @copyright Copyright (c) 2006 - 2008 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. /**Protection
  91. *
  92. * @var PHPExcel_Style_Protection
  93. */
  94. private $_protection;
  95. /**
  96. * Create a new PHPExcel_Style
  97. */
  98. public function __construct()
  99. {
  100. // Initialise values
  101. $this->_fill = new PHPExcel_Style_Fill();
  102. $this->_font = new PHPExcel_Style_Font();
  103. $this->_borders = new PHPExcel_Style_Borders();
  104. $this->_alignment = new PHPExcel_Style_Alignment();
  105. $this->_numberFormat = new PHPExcel_Style_NumberFormat();
  106. $this->_conditionalStyles = array();
  107. $this->_protection = new PHPExcel_Style_Protection();
  108. }
  109. /**
  110. * Apply styles from array
  111. *
  112. * <code>
  113. * $objPHPExcel->getActiveSheet()->getStyle('B2')->applyFromArray(
  114. * array(
  115. * 'font' => array(
  116. * 'name' => 'Arial',
  117. * 'bold' => true,
  118. * 'italic' => false,
  119. * 'underline' => PHPExcel_Style_Font::UNDERLINE_DOUBLE,
  120. * 'strike' => false,
  121. * 'color' => array(
  122. * 'rgb' => '808080'
  123. * )
  124. * ),
  125. * 'borders' => array(
  126. * 'bottom' => array(
  127. * 'style' => PHPExcel_Style_Border::BORDER_DASHDOT,
  128. * 'color' => array(
  129. * 'rgb' => '808080'
  130. * )
  131. * ),
  132. * 'top' => array(
  133. * 'style' => PHPExcel_Style_Border::BORDER_DASHDOT,
  134. * 'color' => array(
  135. * 'rgb' => '808080'
  136. * )
  137. * )
  138. * )
  139. * )
  140. * );
  141. * </code>
  142. *
  143. * @param array $pStyles Array containing style information
  144. * @throws Exception
  145. */
  146. public function applyFromArray($pStyles = null) {
  147. if (is_array($pStyles)) {
  148. if (array_key_exists('fill', $pStyles)) {
  149. $this->getFill()->applyFromArray($pStyles['fill']);
  150. }
  151. if (array_key_exists('font', $pStyles)) {
  152. $this->getFont()->applyFromArray($pStyles['font']);
  153. }
  154. if (array_key_exists('borders', $pStyles)) {
  155. $this->getBorders()->applyFromArray($pStyles['borders']);
  156. }
  157. if (array_key_exists('alignment', $pStyles)) {
  158. $this->getAlignment()->applyFromArray($pStyles['alignment']);
  159. }
  160. if (array_key_exists('numberformat', $pStyles)) {
  161. $this->getNumberFormat()->applyFromArray($pStyles['numberformat']);
  162. }
  163. if (array_key_exists('protection', $pStyles)) {
  164. $this->getProtection()->applyFromArray($pStyles['protection']);
  165. }
  166. } else {
  167. throw new Exception("Invalid style array passed.");
  168. }
  169. }
  170. /**
  171. * Get Fill
  172. *
  173. * @return PHPExcel_Style_Fill
  174. */
  175. public function getFill() {
  176. return $this->_fill;
  177. }
  178. /**
  179. * Get Font
  180. *
  181. * @return PHPExcel_Style_Font
  182. */
  183. public function getFont() {
  184. return $this->_font;
  185. }
  186. /**
  187. * Get Borders
  188. *
  189. * @return PHPExcel_Style_Borders
  190. */
  191. public function getBorders() {
  192. return $this->_borders;
  193. }
  194. /**
  195. * Get Alignment
  196. *
  197. * @return PHPExcel_Style_Alignment
  198. */
  199. public function getAlignment() {
  200. return $this->_alignment;
  201. }
  202. /**
  203. * Get Number Format
  204. *
  205. * @return PHPExcel_Style_NumberFormat
  206. */
  207. public function getNumberFormat() {
  208. return $this->_numberFormat;
  209. }
  210. /**
  211. * Get Conditional Styles
  212. *
  213. * @return PHPExcel_Style_Conditional[]
  214. */
  215. public function getConditionalStyles() {
  216. return $this->_conditionalStyles;
  217. }
  218. /**
  219. * Set Conditional Styles
  220. *
  221. * @param PHPExcel_Style_Conditional[] $pValue Array of condtional styles
  222. */
  223. public function setConditionalStyles($pValue = null) {
  224. if (is_array($pValue)) {
  225. $this->_conditionalStyles = $pValue;
  226. }
  227. }
  228. /**
  229. * Get Protection
  230. *
  231. * @return PHPExcel_Style_Protection
  232. */
  233. public function getProtection() {
  234. return $this->_protection;
  235. }
  236. /**
  237. * Get hash code
  238. *
  239. * @return string Hash code
  240. */
  241. public function getHashCode() {
  242. $hashConditionals = '';
  243. foreach ($this->_conditionalStyles as $conditional) {
  244. $hashConditionals .= $conditional->getHashCode();
  245. }
  246. return md5(
  247. $this->_fill->getHashCode()
  248. . $this->_font->getHashCode()
  249. . $this->_borders->getHashCode()
  250. . $this->_alignment->getHashCode()
  251. . $this->_numberFormat->getHashCode()
  252. . $hashConditionals
  253. . $this->_protection->getHashCode()
  254. . __CLASS__
  255. );
  256. }
  257. /**
  258. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  259. */
  260. public function __clone() {
  261. $vars = get_object_vars($this);
  262. foreach ($vars as $key => $value) {
  263. if (is_object($value)) {
  264. $this->$key = clone $value;
  265. } else {
  266. $this->$key = $value;
  267. }
  268. }
  269. }
  270. }