Borders.php 12 KB

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