PageRenderTime 32ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
PHP | 577 lines | 238 code | 71 blank | 268 comment | 29 complexity | 1aa5135dbd1a10d1f3d0b4f9d47d7fe7 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
  100. *
  101. * @var PHPExcel_Style
  102. */
  103. private $_parent;
  104. /**
  105. * Parent Borders
  106. *
  107. * @var _parentPropertyName string
  108. */
  109. private $_parentPropertyName;
  110. /**
  111. * Create a new PHPExcel_Style_Borders
  112. */
  113. public function __construct()
  114. {
  115. // Initialise values
  116. /**
  117. * The following properties are late bound. Binding is initiated by property classes when they are modified.
  118. *
  119. * _left
  120. * _right
  121. * _top
  122. * _bottom
  123. * _diagonal
  124. * _vertical
  125. * _horizontal
  126. *
  127. */
  128. $this->_diagonalDirection = PHPExcel_Style_Borders::DIAGONAL_NONE;
  129. $this->_outline = true;
  130. }
  131. /**
  132. * Property Prepare bind
  133. *
  134. * Configures this object for late binding as a property of a parent object
  135. *
  136. * @param $parent
  137. * @param $parentPropertyName
  138. */
  139. public function propertyPrepareBind($parent, $parentPropertyName)
  140. {
  141. // Initialize parent PHPExcel_Style for late binding. This relationship purposely ends immediately when this object
  142. // is bound to the PHPExcel_Style object pointed to so as to prevent circular references.
  143. $this->_parent = $parent;
  144. $this->_parentPropertyName = $parentPropertyName;
  145. }
  146. /**
  147. * Property Get Bound
  148. *
  149. * Returns the PHPExcel_Style_Borders that is actual bound to PHPExcel_Style
  150. *
  151. * @return PHPExcel_Style_Borders
  152. */
  153. private function propertyGetBound() {
  154. if(!isset($this->_parent))
  155. return $this; // I am bound
  156. if($this->_parent->propertyIsBound($this->_parentPropertyName))
  157. return $this->_parent->getBorders(); // Another one is bound
  158. return $this; // No one is bound yet
  159. }
  160. /**
  161. * Property Begin Bind
  162. *
  163. * If no PHPExcel_Style_Borders has been bound to PHPExcel_Style then bind this one. Return the actual bound one.
  164. *
  165. * @return PHPExcel_Style_Borders
  166. */
  167. private function propertyBeginBind() {
  168. if(!isset($this->_parent))
  169. return $this; // I am already bound
  170. if($this->_parent->propertyIsBound($this->_parentPropertyName))
  171. return $this->_parent->getBorders(); // Another one is already bound
  172. $this->_parent->propertyCompleteBind($this, $this->_parentPropertyName); // Bind myself
  173. $this->_parent = null;
  174. return $this;
  175. }
  176. /**
  177. * Property Complete Bind
  178. *
  179. * Complete the binding process a child property object started
  180. *
  181. * @param $propertyObject
  182. * @param $propertyName Name of this property in the parent object
  183. */
  184. public function propertyCompleteBind($propertyObject, $propertyName) {
  185. switch($propertyName) {
  186. case "_left":
  187. $this->propertyBeginBind()->_left = $propertyObject;
  188. break;
  189. case "_right":
  190. $this->propertyBeginBind()->_right = $propertyObject;
  191. break;
  192. case "_top":
  193. $this->propertyBeginBind()->_top = $propertyObject;
  194. break;
  195. case "_bottom":
  196. $this->propertyBeginBind()->_bottom = $propertyObject;
  197. break;
  198. case "_diagonal":
  199. $this->propertyBeginBind()->_diagonal = $propertyObject;
  200. break;
  201. case "_vertical":
  202. $this->propertyBeginBind()->_vertical = $propertyObject;
  203. break;
  204. case "_horizontal":
  205. $this->propertyBeginBind()->_horizontal = $propertyObject;
  206. break;
  207. default:
  208. throw new Exception("Invalid property passed.");
  209. }
  210. }
  211. /**
  212. * Property Is Bound
  213. *
  214. * Determines if a child property is bound to this one
  215. *
  216. * @param $propertyName Name of this property in the parent object
  217. *
  218. * @return boolean
  219. */
  220. public function propertyIsBound($propertyName) {
  221. switch($propertyName) {
  222. case "_left":
  223. return isset($this->propertyGetBound()->_left);
  224. case "_right":
  225. return isset($this->propertyGetBound()->_right);
  226. case "_top":
  227. return isset($this->propertyGetBound()->_top);
  228. case "_bottom":
  229. return isset($this->propertyGetBound()->_bottom);
  230. case "_diagonal":
  231. return isset($this->propertyGetBound()->_diagonal);
  232. case "_vertical":
  233. return isset($this->propertyGetBound()->_vertical);
  234. case "_horizontal":
  235. return isset($this->propertyGetBound()->_horizontal);
  236. default:
  237. throw new Exception("Invalid property passed.");
  238. }
  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. */
  277. public function applyFromArray($pStyles = null) {
  278. if (is_array($pStyles)) {
  279. if (array_key_exists('allborders', $pStyles)) {
  280. $this->getLeft()->applyFromArray($pStyles['allborders']);
  281. $this->getRight()->applyFromArray($pStyles['allborders']);
  282. $this->getTop()->applyFromArray($pStyles['allborders']);
  283. $this->getBottom()->applyFromArray($pStyles['allborders']);
  284. }
  285. if (array_key_exists('left', $pStyles)) {
  286. $this->getLeft()->applyFromArray($pStyles['left']);
  287. }
  288. if (array_key_exists('right', $pStyles)) {
  289. $this->getRight()->applyFromArray($pStyles['right']);
  290. }
  291. if (array_key_exists('top', $pStyles)) {
  292. $this->getTop()->applyFromArray($pStyles['top']);
  293. }
  294. if (array_key_exists('bottom', $pStyles)) {
  295. $this->getBottom()->applyFromArray($pStyles['bottom']);
  296. }
  297. if (array_key_exists('diagonal', $pStyles)) {
  298. $this->getDiagonal()->applyFromArray($pStyles['diagonal']);
  299. }
  300. if (array_key_exists('vertical', $pStyles)) {
  301. $this->getVertical()->applyFromArray($pStyles['vertical']);
  302. }
  303. if (array_key_exists('horizontal', $pStyles)) {
  304. $this->getHorizontal()->applyFromArray($pStyles['horizontal']);
  305. }
  306. if (array_key_exists('diagonaldirection', $pStyles)) {
  307. $this->setDiagonalDirection($pStyles['diagonaldirection']);
  308. }
  309. if (array_key_exists('outline', $pStyles)) {
  310. $this->setOutline($pStyles['outline']);
  311. }
  312. } else {
  313. throw new Exception("Invalid style array passed.");
  314. }
  315. }
  316. /**
  317. * Get Left
  318. *
  319. * @return PHPExcel_Style_Border
  320. */
  321. public function getLeft() {
  322. $property = $this->propertyGetBound();
  323. if(isset($property->_left))
  324. return $property->_left;
  325. $property = new PHPExcel_Style_Border();
  326. $property->propertyPrepareBind($this, "_left");
  327. return $property;
  328. }
  329. /**
  330. * Get Right
  331. *
  332. * @return PHPExcel_Style_Border
  333. */
  334. public function getRight() {
  335. $property = $this->propertyGetBound();
  336. if(isset($property->_right))
  337. return $property->_right;
  338. $property = new PHPExcel_Style_Border();
  339. $property->propertyPrepareBind($this, "_right");
  340. return $property;
  341. }
  342. /**
  343. * Get Top
  344. *
  345. * @return PHPExcel_Style_Border
  346. */
  347. public function getTop() {
  348. $property = $this->propertyGetBound();
  349. if(isset($property->_top))
  350. return $property->_top;
  351. $property = new PHPExcel_Style_Border();
  352. $property->propertyPrepareBind($this, "_top");
  353. return $property;
  354. }
  355. /**
  356. * Get Bottom
  357. *
  358. * @return PHPExcel_Style_Border
  359. */
  360. public function getBottom() {
  361. $property = $this->propertyGetBound();
  362. if(isset($property->_bottom))
  363. return $property->_bottom;
  364. $property = new PHPExcel_Style_Border();
  365. $property->propertyPrepareBind($this, "_bottom");
  366. return $property;
  367. }
  368. /**
  369. * Get Diagonal
  370. *
  371. * @return PHPExcel_Style_Border
  372. */
  373. public function getDiagonal() {
  374. $property = $this->propertyGetBound();
  375. if(isset($property->_diagonal))
  376. return $property->_diagonal;
  377. $property = new PHPExcel_Style_Border();
  378. $property->propertyPrepareBind($this, "_diagonal");
  379. return $property;
  380. }
  381. /**
  382. * Get Vertical
  383. *
  384. * @return PHPExcel_Style_Border
  385. */
  386. public function getVertical() {
  387. $property = $this->propertyGetBound();
  388. if(isset($property->_vertical))
  389. return $property->_vertical;
  390. $property = new PHPExcel_Style_Border();
  391. $property->propertyPrepareBind($this, "_vertical");
  392. return $property;
  393. }
  394. /**
  395. * Get Horizontal
  396. *
  397. * @return PHPExcel_Style_Border
  398. */
  399. public function getHorizontal() {
  400. $property = $this->propertyGetBound();
  401. if(isset($property->_horizontal))
  402. return $property->_horizontal;
  403. $property = new PHPExcel_Style_Border();
  404. $property->propertyPrepareBind($this, "_horizontal");
  405. return $property;
  406. }
  407. /**
  408. * Get DiagonalDirection
  409. *
  410. * @return int
  411. */
  412. public function getDiagonalDirection() {
  413. return $this->propertyGetBound()->_diagonalDirection;
  414. }
  415. /**
  416. * Set DiagonalDirection
  417. *
  418. * @param int $pValue
  419. */
  420. public function setDiagonalDirection($pValue = PHPExcel_Style_Borders::DIAGONAL_NONE) {
  421. if ($pValue == '') {
  422. $pValue = PHPExcel_Style_Borders::DIAGONAL_NONE;
  423. }
  424. $this->propertyBeginBind()->_diagonalDirection = $pValue;
  425. }
  426. /**
  427. * Get Outline
  428. *
  429. * @return boolean
  430. */
  431. public function getOutline() {
  432. return $this->propertyGetBound()->_outline;
  433. }
  434. /**
  435. * Set Outline
  436. *
  437. * @param boolean $pValue
  438. */
  439. public function setOutline($pValue = true) {
  440. if ($pValue == '') {
  441. $pValue = true;
  442. }
  443. $this->propertyBeginBind()->_outline = $pValue;
  444. }
  445. /**
  446. * Get hash code
  447. *
  448. * @return string Hash code
  449. */
  450. public function getHashCode() {
  451. $property = $this->propertyGetBound();
  452. return md5(
  453. $property->getLeft()->getHashCode()
  454. . $property->getRight()->getHashCode()
  455. . $property->getTop()->getHashCode()
  456. . $property->getBottom()->getHashCode()
  457. . $property->getDiagonal()->getHashCode()
  458. . $property->getVertical()->getHashCode()
  459. . $property->getHorizontal()->getHashCode()
  460. . $property->getDiagonalDirection()
  461. . ($property->getOutline() ? 't' : 'f')
  462. . __CLASS__
  463. );
  464. }
  465. /**
  466. * Hash index
  467. *
  468. * @var string
  469. */
  470. private $_hashIndex;
  471. /**
  472. * Get hash index
  473. *
  474. * Note that this index may vary during script execution! Only reliable moment is
  475. * while doing a write of a workbook and when changes are not allowed.
  476. *
  477. * @return string Hash index
  478. */
  479. public function getHashIndex() {
  480. return $this->_hashIndex;
  481. }
  482. /**
  483. * Set hash index
  484. *
  485. * Note that this index may vary during script execution! Only reliable moment is
  486. * while doing a write of a workbook and when changes are not allowed.
  487. *
  488. * @param string $value Hash index
  489. */
  490. public function setHashIndex($value) {
  491. $this->_hashIndex = $value;
  492. }
  493. /**
  494. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  495. */
  496. public function __clone() {
  497. $vars = get_object_vars($this);
  498. foreach ($vars as $key => $value) {
  499. if (is_object($value)) {
  500. $this->$key = clone $value;
  501. } else {
  502. $this->$key = $value;
  503. }
  504. }
  505. }
  506. }