PageRenderTime 25ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/wi6857-memory/Classes/PHPExcel/Style/Border.php

#
PHP | 383 lines | 172 code | 43 blank | 168 comment | 17 complexity | 4ed9f663cdbb43b812b7fc880d515fdf 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 - 2009 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 - 2009 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_IComparable */
  30. require_once 'PHPExcel/IComparable.php';
  31. /**
  32. * PHPExcel_Style_Border
  33. *
  34. * @category PHPExcel
  35. * @package PHPExcel_Style
  36. * @copyright Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  37. */
  38. class PHPExcel_Style_Border implements PHPExcel_IComparable
  39. {
  40. /* Border style */
  41. const BORDER_NONE = 'none';
  42. const BORDER_DASHDOT = 'dashDot';
  43. const BORDER_DASHDOTDOT = 'dashDotDot';
  44. const BORDER_DASHED = 'dashed';
  45. const BORDER_DOTTED = 'dotted';
  46. const BORDER_DOUBLE = 'double';
  47. const BORDER_HAIR = 'hair';
  48. const BORDER_MEDIUM = 'medium';
  49. const BORDER_MEDIUMDASHDOT = 'mediumDashDot';
  50. const BORDER_MEDIUMDASHDOTDOT = 'mediumDashDotDot';
  51. const BORDER_MEDIUMDASHED = 'mediumDashed';
  52. const BORDER_SLANTDASHDOT = 'slantDashDot';
  53. const BORDER_THICK = 'thick';
  54. const BORDER_THIN = 'thin';
  55. /**
  56. * Border style
  57. *
  58. * @var string
  59. */
  60. private $_borderStyle;
  61. /**
  62. * Border color
  63. *
  64. * @var PHPExcel_Style_Color
  65. */
  66. private $_color;
  67. /**
  68. * Supervisor?
  69. *
  70. * @var boolean
  71. */
  72. private $_isSupervisor;
  73. /**
  74. * Parent. Only used for supervisor
  75. *
  76. * @var PHPExcel_Style_Borders
  77. */
  78. private $_parent;
  79. /**
  80. * Parent property name
  81. *
  82. * @var string
  83. */
  84. private $_parentPropertyName;
  85. /**
  86. * Create a new PHPExcel_Style_Border
  87. */
  88. public function __construct($isSupervisor = false)
  89. {
  90. // Supervisor?
  91. $this->_isSupervisor = $isSupervisor;
  92. // Initialise values
  93. $this->_borderStyle = PHPExcel_Style_Border::BORDER_NONE;
  94. $this->_color = new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_BLACK, $isSupervisor);
  95. // bind parent if we are a supervisor
  96. if ($isSupervisor) {
  97. $this->_color->bindParent($this, '_color');
  98. }
  99. }
  100. /**
  101. * Bind parent. Only used for supervisor
  102. *
  103. * @param PHPExcel_Style_Borders $parent
  104. * @param string $parentPropertyName
  105. */
  106. public function bindParent($parent, $parentPropertyName)
  107. {
  108. $this->_parent = $parent;
  109. $this->_parentPropertyName = $parentPropertyName;
  110. }
  111. /**
  112. * Is this a supervisor or a real style component?
  113. *
  114. * @return boolean
  115. */
  116. public function getIsSupervisor()
  117. {
  118. return $this->_isSupervisor;
  119. }
  120. /**
  121. * Get the shared style component for the currently active cell in currently active sheet.
  122. * Only used for style supervisor
  123. *
  124. * @return PHPExcel_Style_Border
  125. */
  126. public function getSharedComponent()
  127. {
  128. switch ($this->_parentPropertyName) {
  129. case '_bottom':
  130. return $this->_parent->getSharedComponent()->getBottom();
  131. break;
  132. case '_diagonal':
  133. return $this->_parent->getSharedComponent()->getDiagonal();
  134. break;
  135. case '_horizontal':
  136. return $this->_parent->getSharedComponent()->getHorizontal();
  137. break;
  138. case '_left':
  139. return $this->_parent->getSharedComponent()->getLeft();
  140. break;
  141. case '_right':
  142. return $this->_parent->getSharedComponent()->getRight();
  143. break;
  144. case '_top':
  145. return $this->_parent->getSharedComponent()->getTop();
  146. break;
  147. case '_vertical':
  148. return $this->_parent->getSharedComponent()->getVertical();
  149. break;
  150. }
  151. }
  152. /**
  153. * Get the currently active sheet. Only used for supervisor
  154. *
  155. * @return PHPExcel_Worksheet
  156. */
  157. public function getActiveSheet()
  158. {
  159. return $this->_parent->getActiveSheet();
  160. }
  161. /**
  162. * Get the currently active cell coordinate in currently active sheet.
  163. * Only used for supervisor
  164. *
  165. * @return string E.g. 'A1'
  166. */
  167. public function getSelectedCell()
  168. {
  169. return $this->getActiveSheet()->getSelectedCell();
  170. }
  171. /**
  172. * Build style array from subcomponents
  173. *
  174. * @param array $array
  175. * @return array
  176. */
  177. public function getStyleArray($array)
  178. {
  179. switch ($this->_parentPropertyName) {
  180. case '_bottom':
  181. $key = 'bottom';
  182. break;
  183. case '_diagonal':
  184. $key = 'diagonal';
  185. break;
  186. case '_horizontal':
  187. $key = 'horizontal';
  188. break;
  189. case '_left':
  190. $key = 'left';
  191. break;
  192. case '_right':
  193. $key = 'right';
  194. break;
  195. case '_top':
  196. $key = 'top';
  197. break;
  198. case '_vertical':
  199. $key = 'vertical';
  200. break;
  201. }
  202. return $this->_parent->getStyleArray(array($key => $array));
  203. }
  204. /**
  205. * Apply styles from array
  206. *
  207. * <code>
  208. * $objPHPExcel->getActiveSheet()->getStyle('B2')->getBorders()->getTop()->applyFromArray(
  209. * array(
  210. * 'style' => PHPExcel_Style_Border::BORDER_DASHDOT,
  211. * 'color' => array(
  212. * 'rgb' => '808080'
  213. * )
  214. * )
  215. * );
  216. * </code>
  217. *
  218. * @param array $pStyles Array containing style information
  219. * @throws Exception
  220. */
  221. public function applyFromArray($pStyles = null) {
  222. if (is_array($pStyles)) {
  223. if (array_key_exists('style', $pStyles)) {
  224. $this->setBorderStyle($pStyles['style']);
  225. }
  226. if (array_key_exists('color', $pStyles)) {
  227. $this->getColor()->applyFromArray($pStyles['color']);
  228. }
  229. } else {
  230. throw new Exception("Invalid style array passed.");
  231. }
  232. }
  233. /**
  234. * Get Border style
  235. *
  236. * @return string
  237. */
  238. public function getBorderStyle() {
  239. if ($this->_isSupervisor) {
  240. return $this->getSharedComponent()->getBorderStyle();
  241. }
  242. return $this->_borderStyle;
  243. }
  244. /**
  245. * Set Border style
  246. *
  247. * @param string $pValue
  248. */
  249. public function setBorderStyle($pValue = PHPExcel_Style_Border::BORDER_NONE) {
  250. if ($pValue == '') {
  251. $pValue = PHPExcel_Style_Border::BORDER_NONE;
  252. }
  253. if ($this->_isSupervisor) {
  254. $styleArray = $this->getStyleArray(array('style' => $pValue));
  255. $this->getActiveSheet()->duplicateStyleArray($styleArray, $this->getSelectedCell());
  256. } else {
  257. $this->_borderStyle = $pValue;
  258. }
  259. }
  260. /**
  261. * Get Border Color
  262. *
  263. * @return PHPExcel_Style_Color
  264. */
  265. public function getColor() {
  266. return $this->_color;
  267. }
  268. /**
  269. * Set Border Color
  270. *
  271. * @param PHPExcel_Style_Color $pValue
  272. * @throws Exception
  273. */
  274. public function setColor(PHPExcel_Style_Color $pValue = null) {
  275. // make sure parameter is a real color and not a supervisor
  276. $color = $pValue->getIsSupervisor() ? $pValue->getSharedComponent() : $pValue;
  277. if ($this->_isSupervisor) {
  278. $styleArray = $this->getColor()->getStyleArray(array('argb' => $color->getARGB()));
  279. $this->getActiveSheet()->duplicateStyleArray($styleArray, $this->getSelectedCell());
  280. } else {
  281. $this->_color = $color;
  282. }
  283. }
  284. /**
  285. * Get hash code
  286. *
  287. * @return string Hash code
  288. */
  289. public function getHashCode() {
  290. if ($this->_isSupervisor) {
  291. return $this->getSharedComponent()->getHashCode();
  292. }
  293. return md5(
  294. $this->_borderStyle
  295. . $this->_color->getHashCode()
  296. . __CLASS__
  297. );
  298. }
  299. /**
  300. * Hash index
  301. *
  302. * @var string
  303. */
  304. private $_hashIndex;
  305. /**
  306. * Get hash index
  307. *
  308. * Note that this index may vary during script execution! Only reliable moment is
  309. * while doing a write of a workbook and when changes are not allowed.
  310. *
  311. * @return string Hash index
  312. */
  313. public function getHashIndex() {
  314. return $this->_hashIndex;
  315. }
  316. /**
  317. * Set hash index
  318. *
  319. * Note that this index may vary during script execution! Only reliable moment is
  320. * while doing a write of a workbook and when changes are not allowed.
  321. *
  322. * @param string $value Hash index
  323. */
  324. public function setHashIndex($value) {
  325. $this->_hashIndex = $value;
  326. }
  327. /**
  328. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  329. */
  330. public function __clone() {
  331. $vars = get_object_vars($this);
  332. foreach ($vars as $key => $value) {
  333. if (is_object($value)) {
  334. $this->$key = clone $value;
  335. } else {
  336. $this->$key = $value;
  337. }
  338. }
  339. }
  340. }