PageRenderTime 22ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
PHP | 277 lines | 96 code | 27 blank | 154 comment | 10 complexity | 58e8b2f889aa483534b72e5d25ee4b57 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_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_IComparable */
  42. require_once 'PHPExcel/IComparable.php';
  43. /**
  44. * PHPExcel_Style
  45. *
  46. * @category PHPExcel
  47. * @package PHPExcel_Cell
  48. * @copyright Copyright (c) 2006 - 2007 PHPExcel (http://www.codeplex.com/PHPExcel)
  49. */
  50. class PHPExcel_Style implements PHPExcel_IComparable
  51. {
  52. /**
  53. * Font
  54. *
  55. * @var PHPExcel_Style_Font
  56. */
  57. private $_font;
  58. /**
  59. * Fill
  60. *
  61. * @var PHPExcel_Style_Fill
  62. */
  63. private $_fill;
  64. /**
  65. * Borders
  66. *
  67. * @var PHPExcel_Style_Borders
  68. */
  69. private $_borders;
  70. /**
  71. * Alignment
  72. *
  73. * @var PHPExcel_Style_Alignment
  74. */
  75. private $_alignment;
  76. /**
  77. * Number Format
  78. *
  79. * @var PHPExcel_Style_NumberFormat
  80. */
  81. private $_numberFormat;
  82. /**
  83. * Conditional styles
  84. *
  85. * @var PHPExcel_Style_Conditional[]
  86. */
  87. private $_conditionalStyles;
  88. /**
  89. * Create a new PHPExcel_Style
  90. */
  91. public function __construct()
  92. {
  93. // Initialise values
  94. $this->_fill = new PHPExcel_Style_Fill();
  95. $this->_font = new PHPExcel_Style_Font();
  96. $this->_borders = new PHPExcel_Style_Borders();
  97. $this->_alignment = new PHPExcel_Style_Alignment();
  98. $this->_numberFormat = new PHPExcel_Style_NumberFormat();
  99. $this->_conditionalStyles = array();
  100. }
  101. /**
  102. * Apply styles from array
  103. *
  104. * <code>
  105. * $objPHPExcel->getActiveSheet()->getStyle('B2')->applyFromArray(
  106. * array(
  107. * 'font' => array(
  108. * 'name' => 'Arial',
  109. * 'bold' => true,
  110. * 'italic' => false,
  111. * 'underline' => PHPExcel_Style_Font::UNDERLINE_DOUBLE,
  112. * 'strike' => false,
  113. * 'color' => array(
  114. * 'rgb' => '808080'
  115. * )
  116. * ),
  117. * 'borders' => array(
  118. * 'bottom' => array(
  119. * 'style' => PHPExcel_Style_Border::BORDER_DASHDOT,
  120. * 'color' => array(
  121. * 'rgb' => '808080'
  122. * )
  123. * ),
  124. * 'top' => array(
  125. * 'style' => PHPExcel_Style_Border::BORDER_DASHDOT,
  126. * 'color' => array(
  127. * 'rgb' => '808080'
  128. * )
  129. * )
  130. * )
  131. * )
  132. * );
  133. * </code>
  134. *
  135. * @param array $pStyles Array containing style information
  136. * @throws Exception
  137. */
  138. public function applyFromArray($pStyles = null) {
  139. if (is_array($pStyles)) {
  140. if (array_key_exists('fill', $pStyles)) {
  141. $this->getFill()->applyFromArray($pStyles['fill']);
  142. }
  143. if (array_key_exists('font', $pStyles)) {
  144. $this->getFont()->applyFromArray($pStyles['font']);
  145. }
  146. if (array_key_exists('borders', $pStyles)) {
  147. $this->getBorders()->applyFromArray($pStyles['borders']);
  148. }
  149. if (array_key_exists('alignment', $pStyles)) {
  150. $this->getAlignment()->applyFromArray($pStyles['alignment']);
  151. }
  152. if (array_key_exists('numberformat', $pStyles)) {
  153. $this->getNumberFormat()->applyFromArray($pStyles['numberformat']);
  154. }
  155. } else {
  156. throw new Exception("Invalid style array passed.");
  157. }
  158. }
  159. /**
  160. * Get Fill
  161. *
  162. * @return PHPExcel_Style_Fill
  163. */
  164. public function getFill() {
  165. return $this->_fill;
  166. }
  167. /**
  168. * Get Font
  169. *
  170. * @return PHPExcel_Style_Font
  171. */
  172. public function getFont() {
  173. return $this->_font;
  174. }
  175. /**
  176. * Get Borders
  177. *
  178. * @return PHPExcel_Style_Borders
  179. */
  180. public function getBorders() {
  181. return $this->_borders;
  182. }
  183. /**
  184. * Get Alignment
  185. *
  186. * @return PHPExcel_Style_Alignment
  187. */
  188. public function getAlignment() {
  189. return $this->_alignment;
  190. }
  191. /**
  192. * Get Number Format
  193. *
  194. * @return PHPExcel_Style_NumberFormat
  195. */
  196. public function getNumberFormat() {
  197. return $this->_numberFormat;
  198. }
  199. /**
  200. * Get Conditional Styles
  201. *
  202. * @return PHPExcel_Style_Conditional[]
  203. */
  204. public function getConditionalStyles() {
  205. return $this->_conditionalStyles;
  206. }
  207. /**
  208. * Set Conditional Styles
  209. *
  210. * @param PHPExcel_Style_Conditional[] $pValue Array of condtional styles
  211. */
  212. public function setConditionalStyles($pValue = null) {
  213. if (is_array($pValue)) {
  214. $this->_conditionalStyles = $pValue;
  215. }
  216. }
  217. /**
  218. * Get hash code
  219. *
  220. * @return string Hash code
  221. */
  222. public function getHashCode() {
  223. $hashConditionals = '';
  224. foreach ($this->_conditionalStyles as $conditional) {
  225. $hashConditionals .= $conditional->getHashCode();
  226. }
  227. return md5(
  228. $this->_fill->getHashCode()
  229. . $this->_font->getHashCode()
  230. . $this->_borders->getHashCode()
  231. . $this->_alignment->getHashCode()
  232. . $this->_numberFormat->getHashCode()
  233. . $hashConditionals
  234. . __CLASS__
  235. );
  236. }
  237. /**
  238. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  239. */
  240. public function __clone() {
  241. $vars = get_object_vars($this);
  242. foreach ($vars as $key => $value) {
  243. if (is_object($value)) {
  244. $this->$key = clone $value;
  245. } else {
  246. $this->$key = $value;
  247. }
  248. }
  249. }
  250. }