Border.php 8.1 KB

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