Border.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2013 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 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.9, 2013-06-02
  26. */
  27. /**
  28. * PHPExcel_Style_Border
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Style
  32. * @copyright Copyright (c) 2006 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_Style_Border extends PHPExcel_Style_Supervisor 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. protected $_borderStyle = PHPExcel_Style_Border::BORDER_NONE;
  57. /**
  58. * Border color
  59. *
  60. * @var PHPExcel_Style_Color
  61. */
  62. protected $_color;
  63. /**
  64. * Parent property name
  65. *
  66. * @var string
  67. */
  68. protected $_parentPropertyName;
  69. /**
  70. * Create a new PHPExcel_Style_Border
  71. *
  72. * @param boolean $isSupervisor Flag indicating if this is a supervisor or not
  73. * Leave this value at default unless you understand exactly what
  74. * its ramifications are
  75. * @param boolean $isConditional Flag indicating if this is a conditional style or not
  76. * Leave this value at default unless you understand exactly what
  77. * its ramifications are
  78. */
  79. public function __construct($isSupervisor = FALSE, $isConditional = FALSE)
  80. {
  81. // Supervisor?
  82. parent::__construct($isSupervisor);
  83. // Initialise values
  84. $this->_color = new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_BLACK, $isSupervisor);
  85. // bind parent if we are a supervisor
  86. if ($isSupervisor) {
  87. $this->_color->bindParent($this, '_color');
  88. }
  89. }
  90. /**
  91. * Bind parent. Only used for supervisor
  92. *
  93. * @param PHPExcel_Style_Borders $parent
  94. * @param string $parentPropertyName
  95. * @return PHPExcel_Style_Border
  96. */
  97. public function bindParent($parent, $parentPropertyName=NULL)
  98. {
  99. $this->_parent = $parent;
  100. $this->_parentPropertyName = $parentPropertyName;
  101. return $this;
  102. }
  103. /**
  104. * Get the shared style component for the currently active cell in currently active sheet.
  105. * Only used for style supervisor
  106. *
  107. * @return PHPExcel_Style_Border
  108. * @throws PHPExcel_Exception
  109. */
  110. public function getSharedComponent()
  111. {
  112. switch ($this->_parentPropertyName) {
  113. case '_allBorders':
  114. case '_horizontal':
  115. case '_inside':
  116. case '_outline':
  117. case '_vertical':
  118. throw new PHPExcel_Exception('Cannot get shared component for a pseudo-border.');
  119. break;
  120. case '_bottom':
  121. return $this->_parent->getSharedComponent()->getBottom(); break;
  122. case '_diagonal':
  123. return $this->_parent->getSharedComponent()->getDiagonal(); break;
  124. case '_left':
  125. return $this->_parent->getSharedComponent()->getLeft(); break;
  126. case '_right':
  127. return $this->_parent->getSharedComponent()->getRight(); break;
  128. case '_top':
  129. return $this->_parent->getSharedComponent()->getTop(); break;
  130. }
  131. }
  132. /**
  133. * Build style array from subcomponents
  134. *
  135. * @param array $array
  136. * @return array
  137. */
  138. public function getStyleArray($array)
  139. {
  140. switch ($this->_parentPropertyName) {
  141. case '_allBorders':
  142. $key = 'allborders'; break;
  143. case '_bottom':
  144. $key = 'bottom'; break;
  145. case '_diagonal':
  146. $key = 'diagonal'; break;
  147. case '_horizontal':
  148. $key = 'horizontal'; break;
  149. case '_inside':
  150. $key = 'inside'; break;
  151. case '_left':
  152. $key = 'left'; break;
  153. case '_outline':
  154. $key = 'outline'; break;
  155. case '_right':
  156. $key = 'right'; break;
  157. case '_top':
  158. $key = 'top'; break;
  159. case '_vertical':
  160. $key = 'vertical'; break;
  161. }
  162. return $this->_parent->getStyleArray(array($key => $array));
  163. }
  164. /**
  165. * Apply styles from array
  166. *
  167. * <code>
  168. * $objPHPExcel->getActiveSheet()->getStyle('B2')->getBorders()->getTop()->applyFromArray(
  169. * array(
  170. * 'style' => PHPExcel_Style_Border::BORDER_DASHDOT,
  171. * 'color' => array(
  172. * 'rgb' => '808080'
  173. * )
  174. * )
  175. * );
  176. * </code>
  177. *
  178. * @param array $pStyles Array containing style information
  179. * @throws PHPExcel_Exception
  180. * @return PHPExcel_Style_Border
  181. */
  182. public function applyFromArray($pStyles = null) {
  183. if (is_array($pStyles)) {
  184. if ($this->_isSupervisor) {
  185. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
  186. } else {
  187. if (isset($pStyles['style'])) {
  188. $this->setBorderStyle($pStyles['style']);
  189. }
  190. if (isset($pStyles['color'])) {
  191. $this->getColor()->applyFromArray($pStyles['color']);
  192. }
  193. }
  194. } else {
  195. throw new PHPExcel_Exception("Invalid style array passed.");
  196. }
  197. return $this;
  198. }
  199. /**
  200. * Get Border style
  201. *
  202. * @return string
  203. */
  204. public function getBorderStyle() {
  205. if ($this->_isSupervisor) {
  206. return $this->getSharedComponent()->getBorderStyle();
  207. }
  208. return $this->_borderStyle;
  209. }
  210. /**
  211. * Set Border style
  212. *
  213. * @param string|boolean $pValue
  214. * When passing a boolean, FALSE equates PHPExcel_Style_Border::BORDER_NONE
  215. * and TRUE to PHPExcel_Style_Border::BORDER_MEDIUM
  216. * @return PHPExcel_Style_Border
  217. */
  218. public function setBorderStyle($pValue = PHPExcel_Style_Border::BORDER_NONE) {
  219. if (empty($pValue)) {
  220. $pValue = PHPExcel_Style_Border::BORDER_NONE;
  221. } elseif(is_bool($pValue) && $pValue) {
  222. $pValue = PHPExcel_Style_Border::BORDER_MEDIUM;
  223. }
  224. if ($this->_isSupervisor) {
  225. $styleArray = $this->getStyleArray(array('style' => $pValue));
  226. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  227. } else {
  228. $this->_borderStyle = $pValue;
  229. }
  230. return $this;
  231. }
  232. /**
  233. * Get Border Color
  234. *
  235. * @return PHPExcel_Style_Color
  236. */
  237. public function getColor() {
  238. return $this->_color;
  239. }
  240. /**
  241. * Set Border Color
  242. *
  243. * @param PHPExcel_Style_Color $pValue
  244. * @throws PHPExcel_Exception
  245. * @return PHPExcel_Style_Border
  246. */
  247. public function setColor(PHPExcel_Style_Color $pValue = null) {
  248. // make sure parameter is a real color and not a supervisor
  249. $color = $pValue->getIsSupervisor() ? $pValue->getSharedComponent() : $pValue;
  250. if ($this->_isSupervisor) {
  251. $styleArray = $this->getColor()->getStyleArray(array('argb' => $color->getARGB()));
  252. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  253. } else {
  254. $this->_color = $color;
  255. }
  256. return $this;
  257. }
  258. /**
  259. * Get hash code
  260. *
  261. * @return string Hash code
  262. */
  263. public function getHashCode() {
  264. if ($this->_isSupervisor) {
  265. return $this->getSharedComponent()->getHashCode();
  266. }
  267. return md5(
  268. $this->_borderStyle
  269. . $this->_color->getHashCode()
  270. . __CLASS__
  271. );
  272. }
  273. }