PageRenderTime 52ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/wi6857-memory/Classes/PHPExcel/Style/Borders.php

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