Borders.php 10 KB

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