PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/third_party/codeplex/PHPExcel/Style/Border.php

https://github.com/testlinkjp/testlink-japanese-localization
PHP | 381 lines | 184 code | 41 blank | 156 comment | 21 complexity | 92fe0aa8e03d2728b67c33ed88adb511 MD5 | raw file
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2011 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 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.6, 2011-02-27
  26. */
  27. /**
  28. * PHPExcel_Style_Border
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Style
  32. * @copyright Copyright (c) 2006 - 2011 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 = PHPExcel_Style_Border::BORDER_NONE;
  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->_color = new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_BLACK, $isSupervisor);
  90. // bind parent if we are a supervisor
  91. if ($isSupervisor) {
  92. $this->_color->bindParent($this, '_color');
  93. }
  94. }
  95. /**
  96. * Bind parent. Only used for supervisor
  97. *
  98. * @param PHPExcel_Style_Borders $parent
  99. * @param string $parentPropertyName
  100. * @return PHPExcel_Style_Border
  101. */
  102. public function bindParent($parent, $parentPropertyName)
  103. {
  104. $this->_parent = $parent;
  105. $this->_parentPropertyName = $parentPropertyName;
  106. return $this;
  107. }
  108. /**
  109. * Is this a supervisor or a real style component?
  110. *
  111. * @return boolean
  112. */
  113. public function getIsSupervisor()
  114. {
  115. return $this->_isSupervisor;
  116. }
  117. /**
  118. * Get the shared style component for the currently active cell in currently active sheet.
  119. * Only used for style supervisor
  120. *
  121. * @return PHPExcel_Style_Border
  122. * @throws Exception
  123. */
  124. public function getSharedComponent()
  125. {
  126. switch ($this->_parentPropertyName) {
  127. case '_allBorders':
  128. case '_horizontal':
  129. case '_inside':
  130. case '_outline':
  131. case '_vertical':
  132. throw new Exception('Cannot get shared component for a pseudo-border.');
  133. break;
  134. case '_bottom':
  135. return $this->_parent->getSharedComponent()->getBottom();
  136. break;
  137. case '_diagonal':
  138. return $this->_parent->getSharedComponent()->getDiagonal();
  139. break;
  140. case '_left':
  141. return $this->_parent->getSharedComponent()->getLeft();
  142. break;
  143. case '_right':
  144. return $this->_parent->getSharedComponent()->getRight();
  145. break;
  146. case '_top':
  147. return $this->_parent->getSharedComponent()->getTop();
  148. break;
  149. }
  150. }
  151. /**
  152. * Get the currently active sheet. Only used for supervisor
  153. *
  154. * @return PHPExcel_Worksheet
  155. */
  156. public function getActiveSheet()
  157. {
  158. return $this->_parent->getActiveSheet();
  159. }
  160. /**
  161. * Get the currently active cell coordinate in currently active sheet.
  162. * Only used for supervisor
  163. *
  164. * @return string E.g. 'A1'
  165. */
  166. public function getSelectedCells()
  167. {
  168. return $this->getActiveSheet()->getSelectedCells();
  169. }
  170. /**
  171. * Get the currently active cell coordinate in currently active sheet.
  172. * Only used for supervisor
  173. *
  174. * @return string E.g. 'A1'
  175. */
  176. public function getActiveCell()
  177. {
  178. return $this->getActiveSheet()->getActiveCell();
  179. }
  180. /**
  181. * Build style array from subcomponents
  182. *
  183. * @param array $array
  184. * @return array
  185. */
  186. public function getStyleArray($array)
  187. {
  188. switch ($this->_parentPropertyName) {
  189. case '_allBorders':
  190. $key = 'allborders';
  191. break;
  192. case '_bottom':
  193. $key = 'bottom';
  194. break;
  195. case '_diagonal':
  196. $key = 'diagonal';
  197. break;
  198. case '_horizontal':
  199. $key = 'horizontal';
  200. break;
  201. case '_inside':
  202. $key = 'inside';
  203. break;
  204. case '_left':
  205. $key = 'left';
  206. break;
  207. case '_outline':
  208. $key = 'outline';
  209. break;
  210. case '_right':
  211. $key = 'right';
  212. break;
  213. case '_top':
  214. $key = 'top';
  215. break;
  216. case '_vertical':
  217. $key = 'vertical';
  218. break;
  219. }
  220. return $this->_parent->getStyleArray(array($key => $array));
  221. }
  222. /**
  223. * Apply styles from array
  224. *
  225. * <code>
  226. * $objPHPExcel->getActiveSheet()->getStyle('B2')->getBorders()->getTop()->applyFromArray(
  227. * array(
  228. * 'style' => PHPExcel_Style_Border::BORDER_DASHDOT,
  229. * 'color' => array(
  230. * 'rgb' => '808080'
  231. * )
  232. * )
  233. * );
  234. * </code>
  235. *
  236. * @param array $pStyles Array containing style information
  237. * @throws Exception
  238. * @return PHPExcel_Style_Border
  239. */
  240. public function applyFromArray($pStyles = null) {
  241. if (is_array($pStyles)) {
  242. if ($this->_isSupervisor) {
  243. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
  244. } else {
  245. if (array_key_exists('style', $pStyles)) {
  246. $this->setBorderStyle($pStyles['style']);
  247. }
  248. if (array_key_exists('color', $pStyles)) {
  249. $this->getColor()->applyFromArray($pStyles['color']);
  250. }
  251. }
  252. } else {
  253. throw new Exception("Invalid style array passed.");
  254. }
  255. return $this;
  256. }
  257. /**
  258. * Get Border style
  259. *
  260. * @return string
  261. */
  262. public function getBorderStyle() {
  263. if ($this->_isSupervisor) {
  264. return $this->getSharedComponent()->getBorderStyle();
  265. }
  266. return $this->_borderStyle;
  267. }
  268. /**
  269. * Set Border style
  270. *
  271. * @param string $pValue
  272. * @return PHPExcel_Style_Border
  273. */
  274. public function setBorderStyle($pValue = PHPExcel_Style_Border::BORDER_NONE) {
  275. if ($pValue == '') {
  276. $pValue = PHPExcel_Style_Border::BORDER_NONE;
  277. }
  278. if ($this->_isSupervisor) {
  279. $styleArray = $this->getStyleArray(array('style' => $pValue));
  280. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  281. } else {
  282. $this->_borderStyle = $pValue;
  283. }
  284. return $this;
  285. }
  286. /**
  287. * Get Border Color
  288. *
  289. * @return PHPExcel_Style_Color
  290. */
  291. public function getColor() {
  292. return $this->_color;
  293. }
  294. /**
  295. * Set Border Color
  296. *
  297. * @param PHPExcel_Style_Color $pValue
  298. * @throws Exception
  299. * @return PHPExcel_Style_Border
  300. */
  301. public function setColor(PHPExcel_Style_Color $pValue = null) {
  302. // make sure parameter is a real color and not a supervisor
  303. $color = $pValue->getIsSupervisor() ? $pValue->getSharedComponent() : $pValue;
  304. if ($this->_isSupervisor) {
  305. $styleArray = $this->getColor()->getStyleArray(array('argb' => $color->getARGB()));
  306. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  307. } else {
  308. $this->_color = $color;
  309. }
  310. return $this;
  311. }
  312. /**
  313. * Get hash code
  314. *
  315. * @return string Hash code
  316. */
  317. public function getHashCode() {
  318. if ($this->_isSupervisor) {
  319. return $this->getSharedComponent()->getHashCode();
  320. }
  321. return md5(
  322. $this->_borderStyle
  323. . $this->_color->getHashCode()
  324. . __CLASS__
  325. );
  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)) && ($key != '_parent')) {
  334. $this->$key = clone $value;
  335. } else {
  336. $this->$key = $value;
  337. }
  338. }
  339. }
  340. }