PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/v1.7.2/Classes/PHPExcel/Style/Borders.php

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