Borders.php 11 KB

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