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. * @param bool $isConditional Flag indicating if this is a conditional style or not
  84. * Leave this value at default unless you understand exactly what
  85. * its ramifications are
  86. */
  87. public function __construct($isSupervisor = false, $isConditional = false)
  88. {
  89. // Supervisor?
  90. parent::__construct($isSupervisor);
  91. // Initialise values
  92. $this->left = new Border($isSupervisor, $isConditional);
  93. $this->right = new Border($isSupervisor, $isConditional);
  94. $this->top = new Border($isSupervisor, $isConditional);
  95. $this->bottom = new Border($isSupervisor, $isConditional);
  96. $this->diagonal = new Border($isSupervisor, $isConditional);
  97. $this->diagonalDirection = self::DIAGONAL_NONE;
  98. // Specially for supervisor
  99. if ($isSupervisor) {
  100. // Initialize pseudo-borders
  101. $this->allBorders = new Border(true);
  102. $this->outline = new Border(true);
  103. $this->inside = new Border(true);
  104. $this->vertical = new Border(true);
  105. $this->horizontal = new Border(true);
  106. // bind parent if we are a supervisor
  107. $this->left->bindParent($this, 'left');
  108. $this->right->bindParent($this, 'right');
  109. $this->top->bindParent($this, 'top');
  110. $this->bottom->bindParent($this, 'bottom');
  111. $this->diagonal->bindParent($this, 'diagonal');
  112. $this->allBorders->bindParent($this, 'allBorders');
  113. $this->outline->bindParent($this, 'outline');
  114. $this->inside->bindParent($this, 'inside');
  115. $this->vertical->bindParent($this, 'vertical');
  116. $this->horizontal->bindParent($this, 'horizontal');
  117. }
  118. }
  119. /**
  120. * Get the shared style component for the currently active cell in currently active sheet.
  121. * Only used for style supervisor.
  122. *
  123. * @return Borders
  124. */
  125. public function getSharedComponent()
  126. {
  127. return $this->parent->getSharedComponent()->getBorders();
  128. }
  129. /**
  130. * Build style array from subcomponents.
  131. *
  132. * @param array $array
  133. *
  134. * @return array
  135. */
  136. public function getStyleArray($array)
  137. {
  138. return ['borders' => $array];
  139. }
  140. /**
  141. * Apply styles from array.
  142. * <code>
  143. * $spreadsheet->getActiveSheet()->getStyle('B2')->getBorders()->applyFromArray(
  144. * array(
  145. * 'bottom' => array(
  146. * 'borderStyle' => Border::BORDER_DASHDOT,
  147. * 'color' => array(
  148. * 'rgb' => '808080'
  149. * )
  150. * ),
  151. * 'top' => array(
  152. * 'borderStyle' => Border::BORDER_DASHDOT,
  153. * 'color' => array(
  154. * 'rgb' => '808080'
  155. * )
  156. * )
  157. * )
  158. * );
  159. * </code>
  160. * <code>
  161. * $spreadsheet->getActiveSheet()->getStyle('B2')->getBorders()->applyFromArray(
  162. * array(
  163. * 'allBorders' => array(
  164. * 'borderStyle' => Border::BORDER_DASHDOT,
  165. * 'color' => array(
  166. * 'rgb' => '808080'
  167. * )
  168. * )
  169. * )
  170. * );
  171. * </code>.
  172. *
  173. * @param array $pStyles Array containing style information
  174. *
  175. * @throws PhpSpreadsheetException
  176. *
  177. * @return Borders
  178. */
  179. public function applyFromArray(array $pStyles)
  180. {
  181. if ($this->isSupervisor) {
  182. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
  183. } else {
  184. if (isset($pStyles['left'])) {
  185. $this->getLeft()->applyFromArray($pStyles['left']);
  186. }
  187. if (isset($pStyles['right'])) {
  188. $this->getRight()->applyFromArray($pStyles['right']);
  189. }
  190. if (isset($pStyles['top'])) {
  191. $this->getTop()->applyFromArray($pStyles['top']);
  192. }
  193. if (isset($pStyles['bottom'])) {
  194. $this->getBottom()->applyFromArray($pStyles['bottom']);
  195. }
  196. if (isset($pStyles['diagonal'])) {
  197. $this->getDiagonal()->applyFromArray($pStyles['diagonal']);
  198. }
  199. if (isset($pStyles['diagonalDirection'])) {
  200. $this->setDiagonalDirection($pStyles['diagonalDirection']);
  201. }
  202. if (isset($pStyles['allBorders'])) {
  203. $this->getLeft()->applyFromArray($pStyles['allBorders']);
  204. $this->getRight()->applyFromArray($pStyles['allBorders']);
  205. $this->getTop()->applyFromArray($pStyles['allBorders']);
  206. $this->getBottom()->applyFromArray($pStyles['allBorders']);
  207. }
  208. }
  209. return $this;
  210. }
  211. /**
  212. * Get Left.
  213. *
  214. * @return Border
  215. */
  216. public function getLeft()
  217. {
  218. return $this->left;
  219. }
  220. /**
  221. * Get Right.
  222. *
  223. * @return Border
  224. */
  225. public function getRight()
  226. {
  227. return $this->right;
  228. }
  229. /**
  230. * Get Top.
  231. *
  232. * @return Border
  233. */
  234. public function getTop()
  235. {
  236. return $this->top;
  237. }
  238. /**
  239. * Get Bottom.
  240. *
  241. * @return Border
  242. */
  243. public function getBottom()
  244. {
  245. return $this->bottom;
  246. }
  247. /**
  248. * Get Diagonal.
  249. *
  250. * @return Border
  251. */
  252. public function getDiagonal()
  253. {
  254. return $this->diagonal;
  255. }
  256. /**
  257. * Get AllBorders (pseudo-border). Only applies to supervisor.
  258. *
  259. * @throws PhpSpreadsheetException
  260. *
  261. * @return Border
  262. */
  263. public function getAllBorders()
  264. {
  265. if (!$this->isSupervisor) {
  266. throw new PhpSpreadsheetException('Can only get pseudo-border for supervisor.');
  267. }
  268. return $this->allBorders;
  269. }
  270. /**
  271. * Get Outline (pseudo-border). Only applies to supervisor.
  272. *
  273. * @throws PhpSpreadsheetException
  274. *
  275. * @return Border
  276. */
  277. public function getOutline()
  278. {
  279. if (!$this->isSupervisor) {
  280. throw new PhpSpreadsheetException('Can only get pseudo-border for supervisor.');
  281. }
  282. return $this->outline;
  283. }
  284. /**
  285. * Get Inside (pseudo-border). Only applies to supervisor.
  286. *
  287. * @throws PhpSpreadsheetException
  288. *
  289. * @return Border
  290. */
  291. public function getInside()
  292. {
  293. if (!$this->isSupervisor) {
  294. throw new PhpSpreadsheetException('Can only get pseudo-border for supervisor.');
  295. }
  296. return $this->inside;
  297. }
  298. /**
  299. * Get Vertical (pseudo-border). Only applies to supervisor.
  300. *
  301. * @throws PhpSpreadsheetException
  302. *
  303. * @return Border
  304. */
  305. public function getVertical()
  306. {
  307. if (!$this->isSupervisor) {
  308. throw new PhpSpreadsheetException('Can only get pseudo-border for supervisor.');
  309. }
  310. return $this->vertical;
  311. }
  312. /**
  313. * Get Horizontal (pseudo-border). Only applies to supervisor.
  314. *
  315. * @throws PhpSpreadsheetException
  316. *
  317. * @return Border
  318. */
  319. public function getHorizontal()
  320. {
  321. if (!$this->isSupervisor) {
  322. throw new PhpSpreadsheetException('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 see self::DIAGONAL_*
  342. *
  343. * @return Borders
  344. */
  345. public function setDiagonalDirection($pValue)
  346. {
  347. if ($pValue == '') {
  348. $pValue = self::DIAGONAL_NONE;
  349. }
  350. if ($this->isSupervisor) {
  351. $styleArray = $this->getStyleArray(['diagonalDirection' => $pValue]);
  352. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  353. } else {
  354. $this->diagonalDirection = $pValue;
  355. }
  356. return $this;
  357. }
  358. /**
  359. * Get hash code.
  360. *
  361. * @return string Hash code
  362. */
  363. public function getHashCode()
  364. {
  365. if ($this->isSupervisor) {
  366. return $this->getSharedComponent()->getHashcode();
  367. }
  368. return md5(
  369. $this->getLeft()->getHashCode() .
  370. $this->getRight()->getHashCode() .
  371. $this->getTop()->getHashCode() .
  372. $this->getBottom()->getHashCode() .
  373. $this->getDiagonal()->getHashCode() .
  374. $this->getDiagonalDirection() .
  375. __CLASS__
  376. );
  377. }
  378. }