PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/phpmyadmin/libraries/PHPExcel/PHPExcel/Style/Border.php

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