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

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

#
PHP | 414 lines | 166 code | 52 blank | 196 comment | 17 complexity | 92f9049f292ec5a5396f34247082915e 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. /**
  91. * Protection
  92. *
  93. * @var PHPExcel_Style_Protection
  94. */
  95. private $_protection;
  96. /**
  97. * Create a new PHPExcel_Style
  98. */
  99. public function __construct()
  100. {
  101. // Initialise values
  102. /**
  103. * The following properties are late bound. Binding is initiated by property classes when they are modified.
  104. *
  105. * _font
  106. * _fill
  107. * _borders
  108. * _alignment
  109. * _numberFormat
  110. * _protection
  111. *
  112. */
  113. $this->_conditionalStyles = array();
  114. }
  115. /**
  116. * Property Complete Bind
  117. *
  118. * Complete the binding process a child property object started
  119. *
  120. * @param $propertyObject
  121. * @param $propertyName Name of this property in the parent object
  122. * @throws Exception
  123. */
  124. public function propertyCompleteBind($propertyObject, $propertyName) {
  125. switch($propertyName) {
  126. case "_font":
  127. $this->_font = $propertyObject;
  128. break;
  129. case "_fill":
  130. $this->_fill = $propertyObject;
  131. break;
  132. case "_borders":
  133. $this->_borders = $propertyObject;
  134. break;
  135. case "_alignment":
  136. $this->_alignment = $propertyObject;
  137. break;
  138. case "_numberFormat":
  139. $this->_numberFormat = $propertyObject;
  140. break;
  141. case "_protection":
  142. $this->_protection = $propertyObject;
  143. break;
  144. default:
  145. throw new Exception("Invalid property passed.");
  146. }
  147. }
  148. /**
  149. * Property Is Bound
  150. *
  151. * Determines if a child property is bound to this one
  152. *
  153. * @param $propertyName Name of this property in the parent object
  154. *
  155. * @return boolean
  156. *
  157. * @throws Exception
  158. */
  159. public function propertyIsBound($propertyName) {
  160. switch($propertyName) {
  161. case "_font":
  162. return isset($this->_font);
  163. case "_fill":
  164. return isset($this->_fill);
  165. case "_borders":
  166. return isset($this->_borders);
  167. case "_alignment":
  168. return isset($this->_alignment);
  169. case "_numberFormat":
  170. return isset($this->_numberFormat);
  171. case "_protection":
  172. return isset($this->_protection);
  173. default:
  174. throw new Exception("Invalid property passed.");
  175. }
  176. }
  177. /**
  178. * Apply styles from array
  179. *
  180. * <code>
  181. * $objPHPExcel->getActiveSheet()->getStyle('B2')->applyFromArray(
  182. * array(
  183. * 'font' => array(
  184. * 'name' => 'Arial',
  185. * 'bold' => true,
  186. * 'italic' => false,
  187. * 'underline' => PHPExcel_Style_Font::UNDERLINE_DOUBLE,
  188. * 'strike' => false,
  189. * 'color' => array(
  190. * 'rgb' => '808080'
  191. * )
  192. * ),
  193. * 'borders' => array(
  194. * 'bottom' => array(
  195. * 'style' => PHPExcel_Style_Border::BORDER_DASHDOT,
  196. * 'color' => array(
  197. * 'rgb' => '808080'
  198. * )
  199. * ),
  200. * 'top' => array(
  201. * 'style' => PHPExcel_Style_Border::BORDER_DASHDOT,
  202. * 'color' => array(
  203. * 'rgb' => '808080'
  204. * )
  205. * )
  206. * )
  207. * )
  208. * );
  209. * </code>
  210. *
  211. * @param array $pStyles Array containing style information
  212. * @throws Exception
  213. */
  214. public function applyFromArray($pStyles = null) {
  215. if (is_array($pStyles)) {
  216. if (array_key_exists('fill', $pStyles)) {
  217. $this->getFill()->applyFromArray($pStyles['fill']);
  218. }
  219. if (array_key_exists('font', $pStyles)) {
  220. $this->getFont()->applyFromArray($pStyles['font']);
  221. }
  222. if (array_key_exists('borders', $pStyles)) {
  223. $this->getBorders()->applyFromArray($pStyles['borders']);
  224. }
  225. if (array_key_exists('alignment', $pStyles)) {
  226. $this->getAlignment()->applyFromArray($pStyles['alignment']);
  227. }
  228. if (array_key_exists('numberformat', $pStyles)) {
  229. $this->getNumberFormat()->applyFromArray($pStyles['numberformat']);
  230. }
  231. if (array_key_exists('protection', $pStyles)) {
  232. $this->getProtection()->applyFromArray($pStyles['protection']);
  233. }
  234. } else {
  235. throw new Exception("Invalid style array passed.");
  236. }
  237. }
  238. /**
  239. * Get Fill
  240. *
  241. * @return PHPExcel_Style_Fill
  242. */
  243. public function getFill() {
  244. if(isset($this->_fill))
  245. return $this->_fill;
  246. $property = new PHPExcel_Style_Fill();
  247. $property->propertyPrepareBind($this, "_fill");
  248. return $property;
  249. }
  250. /**
  251. * Get Font
  252. *
  253. * @return PHPExcel_Style_Font
  254. */
  255. public function getFont() {
  256. if(isset($this->_font))
  257. return $this->_font;
  258. $property = new PHPExcel_Style_Font();
  259. $property->propertyPrepareBind($this, "_font");
  260. return $property;
  261. }
  262. /**
  263. * Get Borders
  264. *
  265. * @return PHPExcel_Style_Borders
  266. */
  267. public function getBorders() {
  268. if(isset($this->_borders))
  269. return $this->_borders;
  270. $property = new PHPExcel_Style_Borders();
  271. $property->propertyPrepareBind($this, "_borders");
  272. return $property;
  273. }
  274. /**
  275. * Get Alignment
  276. *
  277. * @return PHPExcel_Style_Alignment
  278. */
  279. public function getAlignment() {
  280. if(isset($this->_alignment))
  281. return $this->_alignment;
  282. $property = new PHPExcel_Style_Alignment();
  283. $property->propertyPrepareBind($this, "_alignment");
  284. return $property;
  285. }
  286. /**
  287. * Get Number Format
  288. *
  289. * @return PHPExcel_Style_NumberFormat
  290. */
  291. public function getNumberFormat() {
  292. if(isset($this->_numberFormat))
  293. return $this->_numberFormat;
  294. $property = new PHPExcel_Style_NumberFormat();
  295. $property->propertyPrepareBind($this, "_numberFormat");
  296. return $property;
  297. }
  298. /**
  299. * Get Conditional Styles
  300. *
  301. * @return PHPExcel_Style_Conditional[]
  302. */
  303. public function getConditionalStyles() {
  304. return $this->_conditionalStyles;
  305. }
  306. /**
  307. * Set Conditional Styles
  308. *
  309. * @param PHPExcel_Style_Conditional[] $pValue Array of condtional styles
  310. */
  311. public function setConditionalStyles($pValue = null) {
  312. if (is_array($pValue)) {
  313. $this->_conditionalStyles = $pValue;
  314. }
  315. }
  316. /**
  317. * Get Protection
  318. *
  319. * @return PHPExcel_Style_Protection
  320. */
  321. public function getProtection() {
  322. if(isset($this->_protection))
  323. return $this->_protection;
  324. $property = new PHPExcel_Style_Protection();
  325. $property->propertyPrepareBind($this, "_protection");
  326. return $property;
  327. }
  328. /**
  329. * Get hash code
  330. *
  331. * @return string Hash code
  332. */
  333. public function getHashCode() {
  334. $hashConditionals = '';
  335. foreach ($this->_conditionalStyles as $conditional) {
  336. $hashConditionals .= $conditional->getHashCode();
  337. }
  338. return md5(
  339. $this->getFill()->getHashCode()
  340. . $this->getFont()->getHashCode()
  341. . $this->getBorders()->getHashCode()
  342. . $this->getAlignment()->getHashCode()
  343. . $this->getNumberFormat()->getHashCode()
  344. . $hashConditionals
  345. . $this->getProtection()->getHashCode()
  346. . __CLASS__
  347. );
  348. }
  349. /**
  350. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  351. */
  352. public function __clone() {
  353. $vars = get_object_vars($this);
  354. foreach ($vars as $key => $value) {
  355. if (is_object($value)) {
  356. $this->$key = clone $value;
  357. } else {
  358. $this->$key = $value;
  359. }
  360. }
  361. }
  362. }