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

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

#
PHP | 346 lines | 132 code | 30 blank | 184 comment | 18 complexity | c666394646edacc48468c1c7df6ba100 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_Style
  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_Border */
  28. require_once 'PHPExcel/Style/Border.php';
  29. /** PHPExcel_IComparable */
  30. require_once 'PHPExcel/IComparable.php';
  31. /**
  32. * PHPExcel_Style_Borders
  33. *
  34. * @category PHPExcel
  35. * @package PHPExcel_Style
  36. * @copyright Copyright (c) 2006 - 2007 PHPExcel (http://www.codeplex.com/PHPExcel)
  37. */
  38. class PHPExcel_Style_Borders implements PHPExcel_IComparable
  39. {
  40. /* Diagonal directions */
  41. const DIAGONAL_NONE = 0;
  42. const DIAGONAL_UP = 1;
  43. const DIAGONAL_DOWN = 2;
  44. /**
  45. * Left
  46. *
  47. * @var PHPExcel_Style_Border
  48. */
  49. private $_left;
  50. /**
  51. * Right
  52. *
  53. * @var PHPExcel_Style_Border
  54. */
  55. private $_right;
  56. /**
  57. * Top
  58. *
  59. * @var PHPExcel_Style_Border
  60. */
  61. private $_top;
  62. /**
  63. * Bottom
  64. *
  65. * @var PHPExcel_Style_Border
  66. */
  67. private $_bottom;
  68. /**
  69. * Diagonal
  70. *
  71. * @var PHPExcel_Style_Border
  72. */
  73. private $_diagonal;
  74. /**
  75. * Vertical
  76. *
  77. * @var PHPExcel_Style_Border
  78. */
  79. private $_vertical;
  80. /**
  81. * Horizontal
  82. *
  83. * @var PHPExcel_Style_Border
  84. */
  85. private $_horizontal;
  86. /**
  87. * DiagonalDirection
  88. *
  89. * @var int
  90. */
  91. private $_diagonalDirection;
  92. /**
  93. * Outline, defaults to true
  94. *
  95. * @var boolean
  96. */
  97. private $_outline;
  98. /**
  99. * Create a new PHPExcel_Style_Borders
  100. */
  101. public function __construct()
  102. {
  103. // Initialise values
  104. $this->_left = new PHPExcel_Style_Border();
  105. $this->_right = new PHPExcel_Style_Border();
  106. $this->_top = new PHPExcel_Style_Border();
  107. $this->_bottom = new PHPExcel_Style_Border();
  108. $this->_diagonal = new PHPExcel_Style_Border();
  109. $this->_vertical = new PHPExcel_Style_Border();
  110. $this->_horizontal = new PHPExcel_Style_Border();
  111. $this->_diagonalDirection = PHPExcel_Style_Borders::DIAGONAL_NONE;
  112. $this->_outline = true;
  113. }
  114. /**
  115. * Apply styles from array
  116. *
  117. * <code>
  118. * $objPHPExcel->getActiveSheet()->getStyle('B2')->getBorders()->applyFromArray(
  119. * array(
  120. * 'bottom' => array(
  121. * 'style' => PHPExcel_Style_Border::BORDER_DASHDOT,
  122. * 'color' => array(
  123. * 'rgb' => '808080'
  124. * )
  125. * ),
  126. * 'top' => array(
  127. * 'style' => PHPExcel_Style_Border::BORDER_DASHDOT,
  128. * 'color' => array(
  129. * 'rgb' => '808080'
  130. * )
  131. * )
  132. * )
  133. * );
  134. * </code>
  135. * <code>
  136. * $objPHPExcel->getActiveSheet()->getStyle('B2')->getBorders()->applyFromArray(
  137. * array(
  138. * 'allborders' => array(
  139. * 'style' => PHPExcel_Style_Border::BORDER_DASHDOT,
  140. * 'color' => array(
  141. * 'rgb' => '808080'
  142. * )
  143. * )
  144. * )
  145. * );
  146. * </code>
  147. *
  148. * @param array $pStyles Array containing style information
  149. * @throws Exception
  150. */
  151. public function applyFromArray($pStyles = null) {
  152. if (is_array($pStyles)) {
  153. if (array_key_exists('allborders', $pStyles)) {
  154. $this->getLeft()->applyFromArray($pStyles['allborders']);
  155. $this->getRight()->applyFromArray($pStyles['allborders']);
  156. $this->getTop()->applyFromArray($pStyles['allborders']);
  157. $this->getBottom()->applyFromArray($pStyles['allborders']);
  158. }
  159. if (array_key_exists('left', $pStyles)) {
  160. $this->getLeft()->applyFromArray($pStyles['left']);
  161. }
  162. if (array_key_exists('right', $pStyles)) {
  163. $this->getRight()->applyFromArray($pStyles['right']);
  164. }
  165. if (array_key_exists('top', $pStyles)) {
  166. $this->getTop()->applyFromArray($pStyles['top']);
  167. }
  168. if (array_key_exists('bottom', $pStyles)) {
  169. $this->getBottom()->applyFromArray($pStyles['bottom']);
  170. }
  171. if (array_key_exists('diagonal', $pStyles)) {
  172. $this->getDiagonal()->applyFromArray($pStyles['diagonal']);
  173. }
  174. if (array_key_exists('vertical', $pStyles)) {
  175. $this->getVertical()->applyFromArray($pStyles['vertical']);
  176. }
  177. if (array_key_exists('horizontal', $pStyles)) {
  178. $this->getHorizontal()->applyFromArray($pStyles['horizontal']);
  179. }
  180. if (array_key_exists('diagonaldirection', $pStyles)) {
  181. $this->setDiagonalDirection($pStyles['diagonaldirection']);
  182. }
  183. if (array_key_exists('outline', $pStyles)) {
  184. $this->setOutline($pStyles['outline']);
  185. }
  186. } else {
  187. throw new Exception("Invalid style array passed.");
  188. }
  189. }
  190. /**
  191. * Get Left
  192. *
  193. * @return PHPExcel_Style_Border
  194. */
  195. public function getLeft() {
  196. return $this->_left;
  197. }
  198. /**
  199. * Get Right
  200. *
  201. * @return PHPExcel_Style_Border
  202. */
  203. public function getRight() {
  204. return $this->_right;
  205. }
  206. /**
  207. * Get Top
  208. *
  209. * @return PHPExcel_Style_Border
  210. */
  211. public function getTop() {
  212. return $this->_top;
  213. }
  214. /**
  215. * Get Bottom
  216. *
  217. * @return PHPExcel_Style_Border
  218. */
  219. public function getBottom() {
  220. return $this->_bottom;
  221. }
  222. /**
  223. * Get Diagonal
  224. *
  225. * @return PHPExcel_Style_Border
  226. */
  227. public function getDiagonal() {
  228. return $this->_diagonal;
  229. }
  230. /**
  231. * Get Vertical
  232. *
  233. * @return PHPExcel_Style_Border
  234. */
  235. public function getVertical() {
  236. return $this->_vertical;
  237. }
  238. /**
  239. * Get Horizontal
  240. *
  241. * @return PHPExcel_Style_Border
  242. */
  243. public function getHorizontal() {
  244. return $this->_horizontal;
  245. }
  246. /**
  247. * Get DiagonalDirection
  248. *
  249. * @return int
  250. */
  251. public function getDiagonalDirection() {
  252. return $this->_diagonalDirection;
  253. }
  254. /**
  255. * Set DiagonalDirection
  256. *
  257. * @param int $pValue
  258. */
  259. public function setDiagonalDirection($pValue = PHPExcel_Style_Borders::DIAGONAL_NONE) {
  260. if ($pValue == '') {
  261. $pValue = PHPExcel_Style_Borders::DIAGONAL_NONE;
  262. }
  263. $this->_diagonalDirection = $pValue;
  264. }
  265. /**
  266. * Get Outline
  267. *
  268. * @return boolean
  269. */
  270. public function getOutline() {
  271. return $this->_outline;
  272. }
  273. /**
  274. * Set Outline
  275. *
  276. * @param boolean $pValue
  277. */
  278. public function setOutline($pValue = true) {
  279. if ($pValue == '') {
  280. $pValue = true;
  281. }
  282. $this->_outline = $pValue;
  283. }
  284. /**
  285. * Get hash code
  286. *
  287. * @return string Hash code
  288. */
  289. public function getHashCode() {
  290. return md5(
  291. $this->getLeft()->getHashCode()
  292. . $this->getRight()->getHashCode()
  293. . $this->getTop()->getHashCode()
  294. . $this->getBottom()->getHashCode()
  295. . $this->getDiagonal()->getHashCode()
  296. . $this->getVertical()->getHashCode()
  297. . $this->getHorizontal()->getHashCode()
  298. . $this->getDiagonalDirection()
  299. . $this->getOutline()
  300. . __CLASS__
  301. );
  302. }
  303. /**
  304. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  305. */
  306. public function __clone() {
  307. $vars = get_object_vars($this);
  308. foreach ($vars as $key => $value) {
  309. if (is_object($value)) {
  310. $this->$key = clone $value;
  311. } else {
  312. $this->$key = $value;
  313. }
  314. }
  315. }
  316. }