PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

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

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