Border.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. /**
  3. * PHPExcel_Style_Border
  4. *
  5. * Copyright (c) 2006 - 2015 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 - 2015 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. class PHPExcel_Style_Border extends PHPExcel_Style_Supervisor implements PHPExcel_IComparable
  28. {
  29. /* Border style */
  30. const BORDER_NONE = 'none';
  31. const BORDER_DASHDOT = 'dashDot';
  32. const BORDER_DASHDOTDOT = 'dashDotDot';
  33. const BORDER_DASHED = 'dashed';
  34. const BORDER_DOTTED = 'dotted';
  35. const BORDER_DOUBLE = 'double';
  36. const BORDER_HAIR = 'hair';
  37. const BORDER_MEDIUM = 'medium';
  38. const BORDER_MEDIUMDASHDOT = 'mediumDashDot';
  39. const BORDER_MEDIUMDASHDOTDOT = 'mediumDashDotDot';
  40. const BORDER_MEDIUMDASHED = 'mediumDashed';
  41. const BORDER_SLANTDASHDOT = 'slantDashDot';
  42. const BORDER_THICK = 'thick';
  43. const BORDER_THIN = 'thin';
  44. /**
  45. * Border style
  46. *
  47. * @var string
  48. */
  49. protected $borderStyle = PHPExcel_Style_Border::BORDER_NONE;
  50. /**
  51. * Border color
  52. *
  53. * @var PHPExcel_Style_Color
  54. */
  55. protected $color;
  56. /**
  57. * Parent property name
  58. *
  59. * @var string
  60. */
  61. protected $parentPropertyName;
  62. /**
  63. * Create a new PHPExcel_Style_Border
  64. *
  65. * @param boolean $isSupervisor Flag indicating if this is a supervisor or not
  66. * Leave this value at default unless you understand exactly what
  67. * its ramifications are
  68. * @param boolean $isConditional Flag indicating if this is a conditional style or not
  69. * Leave this value at default unless you understand exactly what
  70. * its ramifications are
  71. */
  72. public function __construct($isSupervisor = false, $isConditional = false)
  73. {
  74. // Supervisor?
  75. parent::__construct($isSupervisor);
  76. // Initialise values
  77. $this->color = new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_BLACK, $isSupervisor);
  78. // bind parent if we are a supervisor
  79. if ($isSupervisor) {
  80. $this->color->bindParent($this, 'color');
  81. }
  82. }
  83. /**
  84. * Bind parent. Only used for supervisor
  85. *
  86. * @param PHPExcel_Style_Borders $parent
  87. * @param string $parentPropertyName
  88. * @return PHPExcel_Style_Border
  89. */
  90. public function bindParent($parent, $parentPropertyName = null)
  91. {
  92. $this->parent = $parent;
  93. $this->parentPropertyName = $parentPropertyName;
  94. return $this;
  95. }
  96. /**
  97. * Get the shared style component for the currently active cell in currently active sheet.
  98. * Only used for style supervisor
  99. *
  100. * @return PHPExcel_Style_Border
  101. * @throws PHPExcel_Exception
  102. */
  103. public function getSharedComponent()
  104. {
  105. switch ($this->parentPropertyName) {
  106. case 'allBorders':
  107. case 'horizontal':
  108. case 'inside':
  109. case 'outline':
  110. case 'vertical':
  111. throw new PHPExcel_Exception('Cannot get shared component for a pseudo-border.');
  112. break;
  113. case 'bottom':
  114. return $this->parent->getSharedComponent()->getBottom();
  115. case 'diagonal':
  116. return $this->parent->getSharedComponent()->getDiagonal();
  117. case 'left':
  118. return $this->parent->getSharedComponent()->getLeft();
  119. case 'right':
  120. return $this->parent->getSharedComponent()->getRight();
  121. case 'top':
  122. return $this->parent->getSharedComponent()->getTop();
  123. }
  124. }
  125. /**
  126. * Build style array from subcomponents
  127. *
  128. * @param array $array
  129. * @return array
  130. */
  131. public function getStyleArray($array)
  132. {
  133. switch ($this->parentPropertyName) {
  134. case 'allBorders':
  135. case 'bottom':
  136. case 'diagonal':
  137. case 'horizontal':
  138. case 'inside':
  139. case 'left':
  140. case 'outline':
  141. case 'right':
  142. case 'top':
  143. case 'vertical':
  144. $key = strtolower('vertical');
  145. break;
  146. }
  147. return $this->parent->getStyleArray(array($key => $array));
  148. }
  149. /**
  150. * Apply styles from array
  151. *
  152. * <code>
  153. * $objPHPExcel->getActiveSheet()->getStyle('B2')->getBorders()->getTop()->applyFromArray(
  154. * array(
  155. * 'style' => PHPExcel_Style_Border::BORDER_DASHDOT,
  156. * 'color' => array(
  157. * 'rgb' => '808080'
  158. * )
  159. * )
  160. * );
  161. * </code>
  162. *
  163. * @param array $pStyles Array containing style information
  164. * @throws PHPExcel_Exception
  165. * @return PHPExcel_Style_Border
  166. */
  167. public function applyFromArray($pStyles = null)
  168. {
  169. if (is_array($pStyles)) {
  170. if ($this->isSupervisor) {
  171. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
  172. } else {
  173. if (isset($pStyles['style'])) {
  174. $this->setBorderStyle($pStyles['style']);
  175. }
  176. if (isset($pStyles['color'])) {
  177. $this->getColor()->applyFromArray($pStyles['color']);
  178. }
  179. }
  180. } else {
  181. throw new PHPExcel_Exception("Invalid style array passed.");
  182. }
  183. return $this;
  184. }
  185. /**
  186. * Get Border style
  187. *
  188. * @return string
  189. */
  190. public function getBorderStyle()
  191. {
  192. if ($this->isSupervisor) {
  193. return $this->getSharedComponent()->getBorderStyle();
  194. }
  195. return $this->borderStyle;
  196. }
  197. /**
  198. * Set Border style
  199. *
  200. * @param string|boolean $pValue
  201. * When passing a boolean, FALSE equates PHPExcel_Style_Border::BORDER_NONE
  202. * and TRUE to PHPExcel_Style_Border::BORDER_MEDIUM
  203. * @return PHPExcel_Style_Border
  204. */
  205. public function setBorderStyle($pValue = PHPExcel_Style_Border::BORDER_NONE)
  206. {
  207. if (empty($pValue)) {
  208. $pValue = PHPExcel_Style_Border::BORDER_NONE;
  209. } elseif (is_bool($pValue) && $pValue) {
  210. $pValue = PHPExcel_Style_Border::BORDER_MEDIUM;
  211. }
  212. if ($this->isSupervisor) {
  213. $styleArray = $this->getStyleArray(array('style' => $pValue));
  214. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  215. } else {
  216. $this->borderStyle = $pValue;
  217. }
  218. return $this;
  219. }
  220. /**
  221. * Get Border Color
  222. *
  223. * @return PHPExcel_Style_Color
  224. */
  225. public function getColor()
  226. {
  227. return $this->color;
  228. }
  229. /**
  230. * Set Border Color
  231. *
  232. * @param PHPExcel_Style_Color $pValue
  233. * @throws PHPExcel_Exception
  234. * @return PHPExcel_Style_Border
  235. */
  236. public function setColor(PHPExcel_Style_Color $pValue = null)
  237. {
  238. // make sure parameter is a real color and not a supervisor
  239. $color = $pValue->getIsSupervisor() ? $pValue->getSharedComponent() : $pValue;
  240. if ($this->isSupervisor) {
  241. $styleArray = $this->getColor()->getStyleArray(array('argb' => $color->getARGB()));
  242. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  243. } else {
  244. $this->color = $color;
  245. }
  246. return $this;
  247. }
  248. /**
  249. * Get hash code
  250. *
  251. * @return string Hash code
  252. */
  253. public function getHashCode()
  254. {
  255. if ($this->isSupervisor) {
  256. return $this->getSharedComponent()->getHashCode();
  257. }
  258. return md5(
  259. $this->borderStyle .
  260. $this->color->getHashCode() .
  261. __CLASS__
  262. );
  263. }
  264. }