Borders.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. class PHPExcel_Style_Borders extends PHPExcel_Style_Supervisor implements PHPExcel_IComparable
  8. {
  9. /* Diagonal directions */
  10. const DIAGONAL_NONE = 0;
  11. const DIAGONAL_UP = 1;
  12. const DIAGONAL_DOWN = 2;
  13. const DIAGONAL_BOTH = 3;
  14. /**
  15. * Left
  16. *
  17. * @var PHPExcel_Style_Border
  18. */
  19. protected $left;
  20. /**
  21. * Right
  22. *
  23. * @var PHPExcel_Style_Border
  24. */
  25. protected $right;
  26. /**
  27. * Top
  28. *
  29. * @var PHPExcel_Style_Border
  30. */
  31. protected $top;
  32. /**
  33. * Bottom
  34. *
  35. * @var PHPExcel_Style_Border
  36. */
  37. protected $bottom;
  38. /**
  39. * Diagonal
  40. *
  41. * @var PHPExcel_Style_Border
  42. */
  43. protected $diagonal;
  44. /**
  45. * DiagonalDirection
  46. *
  47. * @var int
  48. */
  49. protected $diagonalDirection;
  50. /**
  51. * All borders psedo-border. Only applies to supervisor.
  52. *
  53. * @var PHPExcel_Style_Border
  54. */
  55. protected $allBorders;
  56. /**
  57. * Outline psedo-border. Only applies to supervisor.
  58. *
  59. * @var PHPExcel_Style_Border
  60. */
  61. protected $outline;
  62. /**
  63. * Inside psedo-border. Only applies to supervisor.
  64. *
  65. * @var PHPExcel_Style_Border
  66. */
  67. protected $inside;
  68. /**
  69. * Vertical pseudo-border. Only applies to supervisor.
  70. *
  71. * @var PHPExcel_Style_Border
  72. */
  73. protected $vertical;
  74. /**
  75. * Horizontal pseudo-border. Only applies to supervisor.
  76. *
  77. * @var PHPExcel_Style_Border
  78. */
  79. protected $horizontal;
  80. /**
  81. * Create a new PHPExcel_Style_Borders
  82. *
  83. * @param boolean $isSupervisor Flag indicating if this is a supervisor or not
  84. * Leave this value at default unless you understand exactly what
  85. * its ramifications are
  86. * @param boolean $isConditional Flag indicating if this is a conditional style or not
  87. * Leave this value at default unless you understand exactly what
  88. * its ramifications are
  89. */
  90. public function __construct($isSupervisor = false, $isConditional = false)
  91. {
  92. // Supervisor?
  93. parent::__construct($isSupervisor);
  94. // Initialise values
  95. $this->left = new PHPExcel_Style_Border($isSupervisor, $isConditional);
  96. $this->right = new PHPExcel_Style_Border($isSupervisor, $isConditional);
  97. $this->top = new PHPExcel_Style_Border($isSupervisor, $isConditional);
  98. $this->bottom = new PHPExcel_Style_Border($isSupervisor, $isConditional);
  99. $this->diagonal = new PHPExcel_Style_Border($isSupervisor, $isConditional);
  100. $this->diagonalDirection = PHPExcel_Style_Borders::DIAGONAL_NONE;
  101. // Specially for supervisor
  102. if ($isSupervisor) {
  103. // Initialize pseudo-borders
  104. $this->allBorders = new PHPExcel_Style_Border(true);
  105. $this->outline = new PHPExcel_Style_Border(true);
  106. $this->inside = new PHPExcel_Style_Border(true);
  107. $this->vertical = new PHPExcel_Style_Border(true);
  108. $this->horizontal = new PHPExcel_Style_Border(true);
  109. // bind parent if we are a supervisor
  110. $this->left->bindParent($this, 'left');
  111. $this->right->bindParent($this, 'right');
  112. $this->top->bindParent($this, 'top');
  113. $this->bottom->bindParent($this, 'bottom');
  114. $this->diagonal->bindParent($this, 'diagonal');
  115. $this->allBorders->bindParent($this, 'allBorders');
  116. $this->outline->bindParent($this, 'outline');
  117. $this->inside->bindParent($this, 'inside');
  118. $this->vertical->bindParent($this, 'vertical');
  119. $this->horizontal->bindParent($this, 'horizontal');
  120. }
  121. }
  122. /**
  123. * Get the shared style component for the currently active cell in currently active sheet.
  124. * Only used for style supervisor
  125. *
  126. * @return PHPExcel_Style_Borders
  127. */
  128. public function getSharedComponent()
  129. {
  130. return $this->parent->getSharedComponent()->getBorders();
  131. }
  132. /**
  133. * Build style array from subcomponents
  134. *
  135. * @param array $array
  136. * @return array
  137. */
  138. public function getStyleArray($array)
  139. {
  140. return array('borders' => $array);
  141. }
  142. /**
  143. * Apply styles from array
  144. *
  145. * <code>
  146. * $objPHPExcel->getActiveSheet()->getStyle('B2')->getBorders()->applyFromArray(
  147. * array(
  148. * 'bottom' => array(
  149. * 'style' => PHPExcel_Style_Border::BORDER_DASHDOT,
  150. * 'color' => array(
  151. * 'rgb' => '808080'
  152. * )
  153. * ),
  154. * 'top' => array(
  155. * 'style' => PHPExcel_Style_Border::BORDER_DASHDOT,
  156. * 'color' => array(
  157. * 'rgb' => '808080'
  158. * )
  159. * )
  160. * )
  161. * );
  162. * </code>
  163. * <code>
  164. * $objPHPExcel->getActiveSheet()->getStyle('B2')->getBorders()->applyFromArray(
  165. * array(
  166. * 'allborders' => array(
  167. * 'style' => PHPExcel_Style_Border::BORDER_DASHDOT,
  168. * 'color' => array(
  169. * 'rgb' => '808080'
  170. * )
  171. * )
  172. * )
  173. * );
  174. * </code>
  175. *
  176. * @param array $pStyles Array containing style information
  177. * @throws PHPExcel_Exception
  178. * @return PHPExcel_Style_Borders
  179. */
  180. public function applyFromArray($pStyles = null)
  181. {
  182. if (is_array($pStyles)) {
  183. if ($this->isSupervisor) {
  184. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
  185. } else {
  186. if (array_key_exists('left', $pStyles)) {
  187. $this->getLeft()->applyFromArray($pStyles['left']);
  188. }
  189. if (array_key_exists('right', $pStyles)) {
  190. $this->getRight()->applyFromArray($pStyles['right']);
  191. }
  192. if (array_key_exists('top', $pStyles)) {
  193. $this->getTop()->applyFromArray($pStyles['top']);
  194. }
  195. if (array_key_exists('bottom', $pStyles)) {
  196. $this->getBottom()->applyFromArray($pStyles['bottom']);
  197. }
  198. if (array_key_exists('diagonal', $pStyles)) {
  199. $this->getDiagonal()->applyFromArray($pStyles['diagonal']);
  200. }
  201. if (array_key_exists('diagonaldirection', $pStyles)) {
  202. $this->setDiagonalDirection($pStyles['diagonaldirection']);
  203. }
  204. if (array_key_exists('allborders', $pStyles)) {
  205. $this->getLeft()->applyFromArray($pStyles['allborders']);
  206. $this->getRight()->applyFromArray($pStyles['allborders']);
  207. $this->getTop()->applyFromArray($pStyles['allborders']);
  208. $this->getBottom()->applyFromArray($pStyles['allborders']);
  209. }
  210. }
  211. } else {
  212. throw new PHPExcel_Exception("Invalid style array passed.");
  213. }
  214. return $this;
  215. }
  216. /**
  217. * Get Left
  218. *
  219. * @return PHPExcel_Style_Border
  220. */
  221. public function getLeft()
  222. {
  223. return $this->left;
  224. }
  225. /**
  226. * Get Right
  227. *
  228. * @return PHPExcel_Style_Border
  229. */
  230. public function getRight()
  231. {
  232. return $this->right;
  233. }
  234. /**
  235. * Get Top
  236. *
  237. * @return PHPExcel_Style_Border
  238. */
  239. public function getTop()
  240. {
  241. return $this->top;
  242. }
  243. /**
  244. * Get Bottom
  245. *
  246. * @return PHPExcel_Style_Border
  247. */
  248. public function getBottom()
  249. {
  250. return $this->bottom;
  251. }
  252. /**
  253. * Get Diagonal
  254. *
  255. * @return PHPExcel_Style_Border
  256. */
  257. public function getDiagonal()
  258. {
  259. return $this->diagonal;
  260. }
  261. /**
  262. * Get AllBorders (pseudo-border). Only applies to supervisor.
  263. *
  264. * @return PHPExcel_Style_Border
  265. * @throws PHPExcel_Exception
  266. */
  267. public function getAllBorders()
  268. {
  269. if (!$this->isSupervisor) {
  270. throw new PHPExcel_Exception('Can only get pseudo-border for supervisor.');
  271. }
  272. return $this->allBorders;
  273. }
  274. /**
  275. * Get Outline (pseudo-border). Only applies to supervisor.
  276. *
  277. * @return boolean
  278. * @throws PHPExcel_Exception
  279. */
  280. public function getOutline()
  281. {
  282. if (!$this->isSupervisor) {
  283. throw new PHPExcel_Exception('Can only get pseudo-border for supervisor.');
  284. }
  285. return $this->outline;
  286. }
  287. /**
  288. * Get Inside (pseudo-border). Only applies to supervisor.
  289. *
  290. * @return boolean
  291. * @throws PHPExcel_Exception
  292. */
  293. public function getInside()
  294. {
  295. if (!$this->isSupervisor) {
  296. throw new PHPExcel_Exception('Can only get pseudo-border for supervisor.');
  297. }
  298. return $this->inside;
  299. }
  300. /**
  301. * Get Vertical (pseudo-border). Only applies to supervisor.
  302. *
  303. * @return PHPExcel_Style_Border
  304. * @throws PHPExcel_Exception
  305. */
  306. public function getVertical()
  307. {
  308. if (!$this->isSupervisor) {
  309. throw new PHPExcel_Exception('Can only get pseudo-border for supervisor.');
  310. }
  311. return $this->vertical;
  312. }
  313. /**
  314. * Get Horizontal (pseudo-border). Only applies to supervisor.
  315. *
  316. * @return PHPExcel_Style_Border
  317. * @throws PHPExcel_Exception
  318. */
  319. public function getHorizontal()
  320. {
  321. if (!$this->isSupervisor) {
  322. throw new PHPExcel_Exception('Can only get pseudo-border for supervisor.');
  323. }
  324. return $this->horizontal;
  325. }
  326. /**
  327. * Get DiagonalDirection
  328. *
  329. * @return int
  330. */
  331. public function getDiagonalDirection()
  332. {
  333. if ($this->isSupervisor) {
  334. return $this->getSharedComponent()->getDiagonalDirection();
  335. }
  336. return $this->diagonalDirection;
  337. }
  338. /**
  339. * Set DiagonalDirection
  340. *
  341. * @param int $pValue
  342. * @return PHPExcel_Style_Borders
  343. */
  344. public function setDiagonalDirection($pValue = PHPExcel_Style_Borders::DIAGONAL_NONE)
  345. {
  346. if ($pValue == '') {
  347. $pValue = PHPExcel_Style_Borders::DIAGONAL_NONE;
  348. }
  349. if ($this->isSupervisor) {
  350. $styleArray = $this->getStyleArray(array('diagonaldirection' => $pValue));
  351. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  352. } else {
  353. $this->diagonalDirection = $pValue;
  354. }
  355. return $this;
  356. }
  357. /**
  358. * Get hash code
  359. *
  360. * @return string Hash code
  361. */
  362. public function getHashCode()
  363. {
  364. if ($this->isSupervisor) {
  365. return $this->getSharedComponent()->getHashcode();
  366. }
  367. return md5(
  368. $this->getLeft()->getHashCode() .
  369. $this->getRight()->getHashCode() .
  370. $this->getTop()->getHashCode() .
  371. $this->getBottom()->getHashCode() .
  372. $this->getDiagonal()->getHashCode() .
  373. $this->getDiagonalDirection() .
  374. __CLASS__
  375. );
  376. }
  377. }