PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/PHPExcel_1.7.8-with_documentation-msoffice_format/Classes/PHPExcel/Style/Borders.php

https://bitbucket.org/izubizarreta/https-bitbucket.org-bityvip
PHP | 512 lines | 200 code | 42 blank | 270 comment | 27 complexity | 86bc4c3b26572b57e243747d20aa383b MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.0, JSON, GPL-2.0, BSD-3-Clause, LGPL-2.1, MIT
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2012 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 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.8, 2012-10-12
  26. */
  27. /**
  28. * PHPExcel_Style_Borders
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Style
  32. * @copyright Copyright (c) 2006 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_Style_Borders 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. private $_left;
  47. /**
  48. * Right
  49. *
  50. * @var PHPExcel_Style_Border
  51. */
  52. private $_right;
  53. /**
  54. * Top
  55. *
  56. * @var PHPExcel_Style_Border
  57. */
  58. private $_top;
  59. /**
  60. * Bottom
  61. *
  62. * @var PHPExcel_Style_Border
  63. */
  64. private $_bottom;
  65. /**
  66. * Diagonal
  67. *
  68. * @var PHPExcel_Style_Border
  69. */
  70. private $_diagonal;
  71. /**
  72. * DiagonalDirection
  73. *
  74. * @var int
  75. */
  76. private $_diagonalDirection;
  77. /**
  78. * All borders psedo-border. Only applies to supervisor.
  79. *
  80. * @var PHPExcel_Style_Border
  81. */
  82. private $_allBorders;
  83. /**
  84. * Outline psedo-border. Only applies to supervisor.
  85. *
  86. * @var PHPExcel_Style_Border
  87. */
  88. private $_outline;
  89. /**
  90. * Inside psedo-border. Only applies to supervisor.
  91. *
  92. * @var PHPExcel_Style_Border
  93. */
  94. private $_inside;
  95. /**
  96. * Vertical pseudo-border. Only applies to supervisor.
  97. *
  98. * @var PHPExcel_Style_Border
  99. */
  100. private $_vertical;
  101. /**
  102. * Horizontal pseudo-border. Only applies to supervisor.
  103. *
  104. * @var PHPExcel_Style_Border
  105. */
  106. private $_horizontal;
  107. /**
  108. * Parent Borders
  109. *
  110. * @var _parentPropertyName string
  111. */
  112. private $_parentPropertyName;
  113. /**
  114. * Supervisor?
  115. *
  116. * @var boolean
  117. */
  118. private $_isSupervisor;
  119. /**
  120. * Parent. Only used for supervisor
  121. *
  122. * @var PHPExcel_Style
  123. */
  124. private $_parent;
  125. /**
  126. * Create a new PHPExcel_Style_Borders
  127. *
  128. * @param boolean $isSupervisor Flag indicating if this is a supervisor or not
  129. * Leave this value at default unless you understand exactly what
  130. * its ramifications are
  131. * @param boolean $isConditional Flag indicating if this is a conditional style or not
  132. * Leave this value at default unless you understand exactly what
  133. * its ramifications are
  134. */
  135. public function __construct($isSupervisor = false, $isConditional = false)
  136. {
  137. // Supervisor?
  138. $this->_isSupervisor = $isSupervisor;
  139. // Initialise values
  140. $this->_left = new PHPExcel_Style_Border($isSupervisor, $isConditional);
  141. $this->_right = new PHPExcel_Style_Border($isSupervisor, $isConditional);
  142. $this->_top = new PHPExcel_Style_Border($isSupervisor, $isConditional);
  143. $this->_bottom = new PHPExcel_Style_Border($isSupervisor, $isConditional);
  144. $this->_diagonal = new PHPExcel_Style_Border($isSupervisor, $isConditional);
  145. $this->_diagonalDirection = PHPExcel_Style_Borders::DIAGONAL_NONE;
  146. // Specially for supervisor
  147. if ($isSupervisor) {
  148. // Initialize pseudo-borders
  149. $this->_allBorders = new PHPExcel_Style_Border(true);
  150. $this->_outline = new PHPExcel_Style_Border(true);
  151. $this->_inside = new PHPExcel_Style_Border(true);
  152. $this->_vertical = new PHPExcel_Style_Border(true);
  153. $this->_horizontal = new PHPExcel_Style_Border(true);
  154. // bind parent if we are a supervisor
  155. $this->_left->bindParent($this, '_left');
  156. $this->_right->bindParent($this, '_right');
  157. $this->_top->bindParent($this, '_top');
  158. $this->_bottom->bindParent($this, '_bottom');
  159. $this->_diagonal->bindParent($this, '_diagonal');
  160. $this->_allBorders->bindParent($this, '_allBorders');
  161. $this->_outline->bindParent($this, '_outline');
  162. $this->_inside->bindParent($this, '_inside');
  163. $this->_vertical->bindParent($this, '_vertical');
  164. $this->_horizontal->bindParent($this, '_horizontal');
  165. }
  166. }
  167. /**
  168. * Bind parent. Only used for supervisor
  169. *
  170. * @param PHPExcel_Style $parent
  171. * @return PHPExcel_Style_Borders
  172. */
  173. public function bindParent($parent)
  174. {
  175. $this->_parent = $parent;
  176. return $this;
  177. }
  178. /**
  179. * Is this a supervisor or a real style component?
  180. *
  181. * @return boolean
  182. */
  183. public function getIsSupervisor()
  184. {
  185. return $this->_isSupervisor;
  186. }
  187. /**
  188. * Get the shared style component for the currently active cell in currently active sheet.
  189. * Only used for style supervisor
  190. *
  191. * @return PHPExcel_Style_Borders
  192. */
  193. public function getSharedComponent()
  194. {
  195. return $this->_parent->getSharedComponent()->getBorders();
  196. }
  197. /**
  198. * Get the currently active sheet. Only used for supervisor
  199. *
  200. * @return PHPExcel_Worksheet
  201. */
  202. public function getActiveSheet()
  203. {
  204. return $this->_parent->getActiveSheet();
  205. }
  206. /**
  207. * Get the currently active cell coordinate in currently active sheet.
  208. * Only used for supervisor
  209. *
  210. * @return string E.g. 'A1'
  211. */
  212. public function getSelectedCells()
  213. {
  214. return $this->getActiveSheet()->getSelectedCells();
  215. }
  216. /**
  217. * Get the currently active cell coordinate in currently active sheet.
  218. * Only used for supervisor
  219. *
  220. * @return string E.g. 'A1'
  221. */
  222. public function getActiveCell()
  223. {
  224. return $this->getActiveSheet()->getActiveCell();
  225. }
  226. /**
  227. * Build style array from subcomponents
  228. *
  229. * @param array $array
  230. * @return array
  231. */
  232. public function getStyleArray($array)
  233. {
  234. return array('borders' => $array);
  235. }
  236. /**
  237. * Apply styles from array
  238. *
  239. * <code>
  240. * $objPHPExcel->getActiveSheet()->getStyle('B2')->getBorders()->applyFromArray(
  241. * array(
  242. * 'bottom' => array(
  243. * 'style' => PHPExcel_Style_Border::BORDER_DASHDOT,
  244. * 'color' => array(
  245. * 'rgb' => '808080'
  246. * )
  247. * ),
  248. * 'top' => array(
  249. * 'style' => PHPExcel_Style_Border::BORDER_DASHDOT,
  250. * 'color' => array(
  251. * 'rgb' => '808080'
  252. * )
  253. * )
  254. * )
  255. * );
  256. * </code>
  257. * <code>
  258. * $objPHPExcel->getActiveSheet()->getStyle('B2')->getBorders()->applyFromArray(
  259. * array(
  260. * 'allborders' => array(
  261. * 'style' => PHPExcel_Style_Border::BORDER_DASHDOT,
  262. * 'color' => array(
  263. * 'rgb' => '808080'
  264. * )
  265. * )
  266. * )
  267. * );
  268. * </code>
  269. *
  270. * @param array $pStyles Array containing style information
  271. * @throws Exception
  272. * @return PHPExcel_Style_Borders
  273. */
  274. public function applyFromArray($pStyles = null) {
  275. if (is_array($pStyles)) {
  276. if ($this->_isSupervisor) {
  277. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
  278. } else {
  279. if (array_key_exists('left', $pStyles)) {
  280. $this->getLeft()->applyFromArray($pStyles['left']);
  281. }
  282. if (array_key_exists('right', $pStyles)) {
  283. $this->getRight()->applyFromArray($pStyles['right']);
  284. }
  285. if (array_key_exists('top', $pStyles)) {
  286. $this->getTop()->applyFromArray($pStyles['top']);
  287. }
  288. if (array_key_exists('bottom', $pStyles)) {
  289. $this->getBottom()->applyFromArray($pStyles['bottom']);
  290. }
  291. if (array_key_exists('diagonal', $pStyles)) {
  292. $this->getDiagonal()->applyFromArray($pStyles['diagonal']);
  293. }
  294. if (array_key_exists('diagonaldirection', $pStyles)) {
  295. $this->setDiagonalDirection($pStyles['diagonaldirection']);
  296. }
  297. if (array_key_exists('allborders', $pStyles)) {
  298. $this->getLeft()->applyFromArray($pStyles['allborders']);
  299. $this->getRight()->applyFromArray($pStyles['allborders']);
  300. $this->getTop()->applyFromArray($pStyles['allborders']);
  301. $this->getBottom()->applyFromArray($pStyles['allborders']);
  302. }
  303. }
  304. } else {
  305. throw new Exception("Invalid style array passed.");
  306. }
  307. return $this;
  308. }
  309. /**
  310. * Get Left
  311. *
  312. * @return PHPExcel_Style_Border
  313. */
  314. public function getLeft() {
  315. return $this->_left;
  316. }
  317. /**
  318. * Get Right
  319. *
  320. * @return PHPExcel_Style_Border
  321. */
  322. public function getRight() {
  323. return $this->_right;
  324. }
  325. /**
  326. * Get Top
  327. *
  328. * @return PHPExcel_Style_Border
  329. */
  330. public function getTop() {
  331. return $this->_top;
  332. }
  333. /**
  334. * Get Bottom
  335. *
  336. * @return PHPExcel_Style_Border
  337. */
  338. public function getBottom() {
  339. return $this->_bottom;
  340. }
  341. /**
  342. * Get Diagonal
  343. *
  344. * @return PHPExcel_Style_Border
  345. */
  346. public function getDiagonal() {
  347. return $this->_diagonal;
  348. }
  349. /**
  350. * Get AllBorders (pseudo-border). Only applies to supervisor.
  351. *
  352. * @return PHPExcel_Style_Border
  353. * @throws Exception
  354. */
  355. public function getAllBorders() {
  356. if (!$this->_isSupervisor) {
  357. throw new Exception('Can only get pseudo-border for supervisor.');
  358. }
  359. return $this->_allBorders;
  360. }
  361. /**
  362. * Get Outline (pseudo-border). Only applies to supervisor.
  363. *
  364. * @return boolean
  365. * @throws Exception
  366. */
  367. public function getOutline() {
  368. if (!$this->_isSupervisor) {
  369. throw new Exception('Can only get pseudo-border for supervisor.');
  370. }
  371. return $this->_outline;
  372. }
  373. /**
  374. * Get Inside (pseudo-border). Only applies to supervisor.
  375. *
  376. * @return boolean
  377. * @throws Exception
  378. */
  379. public function getInside() {
  380. if (!$this->_isSupervisor) {
  381. throw new Exception('Can only get pseudo-border for supervisor.');
  382. }
  383. return $this->_inside;
  384. }
  385. /**
  386. * Get Vertical (pseudo-border). Only applies to supervisor.
  387. *
  388. * @return PHPExcel_Style_Border
  389. * @throws Exception
  390. */
  391. public function getVertical() {
  392. if (!$this->_isSupervisor) {
  393. throw new Exception('Can only get pseudo-border for supervisor.');
  394. }
  395. return $this->_vertical;
  396. }
  397. /**
  398. * Get Horizontal (pseudo-border). Only applies to supervisor.
  399. *
  400. * @return PHPExcel_Style_Border
  401. * @throws Exception
  402. */
  403. public function getHorizontal() {
  404. if (!$this->_isSupervisor) {
  405. throw new Exception('Can only get pseudo-border for supervisor.');
  406. }
  407. return $this->_horizontal;
  408. }
  409. /**
  410. * Get DiagonalDirection
  411. *
  412. * @return int
  413. */
  414. public function getDiagonalDirection() {
  415. if ($this->_isSupervisor) {
  416. return $this->getSharedComponent()->getDiagonalDirection();
  417. }
  418. return $this->_diagonalDirection;
  419. }
  420. /**
  421. * Set DiagonalDirection
  422. *
  423. * @param int $pValue
  424. * @return PHPExcel_Style_Borders
  425. */
  426. public function setDiagonalDirection($pValue = PHPExcel_Style_Borders::DIAGONAL_NONE) {
  427. if ($pValue == '') {
  428. $pValue = PHPExcel_Style_Borders::DIAGONAL_NONE;
  429. }
  430. if ($this->_isSupervisor) {
  431. $styleArray = $this->getStyleArray(array('diagonaldirection' => $pValue));
  432. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  433. } else {
  434. $this->_diagonalDirection = $pValue;
  435. }
  436. return $this;
  437. }
  438. /**
  439. * Get hash code
  440. *
  441. * @return string Hash code
  442. */
  443. public function getHashCode() {
  444. if ($this->_isSupervisor) {
  445. return $this->getSharedComponent()->getHashcode();
  446. }
  447. return md5(
  448. $this->getLeft()->getHashCode()
  449. . $this->getRight()->getHashCode()
  450. . $this->getTop()->getHashCode()
  451. . $this->getBottom()->getHashCode()
  452. . $this->getDiagonal()->getHashCode()
  453. . $this->getDiagonalDirection()
  454. . __CLASS__
  455. );
  456. }
  457. /**
  458. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  459. */
  460. public function __clone() {
  461. $vars = get_object_vars($this);
  462. foreach ($vars as $key => $value) {
  463. if ((is_object($value)) && ($key != '_parent')) {
  464. $this->$key = clone $value;
  465. } else {
  466. $this->$key = $value;
  467. }
  468. }
  469. }
  470. }