PageRenderTime 59ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/PHPExcel_1.7.8-with_documentation-msoffice_format/PHPExcel_1.7.8-with_documentation-msoffice_format/Classes/PHPExcel/Style/Border.php

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