PageRenderTime 21ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

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

#
PHP | 331 lines | 138 code | 41 blank | 152 comment | 12 complexity | dde21cc7ba6930969f1418df356b41a9 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_Color */
  28. require_once 'PHPExcel/Style/Color.php';
  29. /** PHPExcel_IComparable */
  30. require_once 'PHPExcel/IComparable.php';
  31. /**
  32. * PHPExcel_Style_Border
  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_Border implements PHPExcel_IComparable
  39. {
  40. /* Border style */
  41. const BORDER_NONE = 'none';
  42. const BORDER_DASHDOT = 'dashDot';
  43. const BORDER_DASHDOTDOT = 'dashDotDot';
  44. const BORDER_DASHED = 'dashed';
  45. const BORDER_DOTTED = 'dotted';
  46. const BORDER_DOUBLE = 'double';
  47. const BORDER_HAIR = 'hair';
  48. const BORDER_MEDIUM = 'medium';
  49. const BORDER_MEDIUMDASHDOT = 'mediumDashDot';
  50. const BORDER_MEDIUMDASHDOTDOT = 'mediumDashDotDot';
  51. const BORDER_MEDIUMDASHED = 'mediumDashed';
  52. const BORDER_SLANTDASHDOT = 'slantDashDot';
  53. const BORDER_THICK = 'thick';
  54. const BORDER_THIN = 'thin';
  55. /**
  56. * Border style
  57. *
  58. * @var string
  59. */
  60. private $_borderStyle;
  61. /**
  62. * Border color
  63. *
  64. * @var PHPExcel_Style_Color
  65. */
  66. private $_borderColor;
  67. /**
  68. * Parent
  69. *
  70. * @var PHPExcel_Style_Borders
  71. */
  72. private $_parent;
  73. /**
  74. * Parent Property Name
  75. *
  76. * @var string
  77. */
  78. private $_parentPropertyName;
  79. /**
  80. * Create a new PHPExcel_Style_Border
  81. */
  82. public function __construct()
  83. {
  84. // Initialise values
  85. $this->_borderStyle = PHPExcel_Style_Border::BORDER_NONE;
  86. $this->_borderColor = new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_BLACK);
  87. }
  88. /**
  89. * Property Prepare bind
  90. *
  91. * Configures this object for late binding as a property of a parent object
  92. *
  93. * @param $parent
  94. * @param $parentPropertyName
  95. */
  96. public function propertyPrepareBind($parent, $parentPropertyName)
  97. {
  98. // Initialize parent PHPExcel_Style for late binding. This relationship purposely ends immediately when this object
  99. // is bound to the PHPExcel_Style object pointed to so as to prevent circular references.
  100. $this->_parent = $parent;
  101. $this->_parentPropertyName = $parentPropertyName;
  102. }
  103. /**
  104. * Property Get Bound
  105. *
  106. * Returns the PHPExcel_Style_Border that is actual bound to PHPExcel_Style_Borders
  107. *
  108. * @return PHPExcel_Style_Border
  109. */
  110. private function propertyGetBound() {
  111. if(!isset($this->_parent))
  112. return $this; // I am bound
  113. if($this->_parent->propertyIsBound($this->_parentPropertyName))
  114. {
  115. switch($this->_parentPropertyName) // Another one is bound
  116. {
  117. case "_left":
  118. return $this->_parent->getLeft();
  119. case "_right":
  120. return $this->_parent->getRight();
  121. case "_top":
  122. return $this->_parent->getTop();
  123. case "_bottom":
  124. return $this->_parent->getBottom();
  125. case "_diagonal":
  126. return $this->_parent->getDiagonal();
  127. case "_vertical":
  128. return $this->_parent->getVertical();
  129. case "_horizontal":
  130. return $this->_parent->getHorizontal();
  131. }
  132. }
  133. return $this; // No one is bound yet
  134. }
  135. /**
  136. * Property Begin Bind
  137. *
  138. * If no PHPExcel_Style_Border has been bound to PHPExcel_Style_Borders then bind this one. Return the actual bound one.
  139. *
  140. * @return PHPExcel_Style_Border
  141. */
  142. private function propertyBeginBind() {
  143. if(!isset($this->_parent))
  144. return $this; // I am already bound
  145. if($this->_parent->propertyIsBound($this->_parentPropertyName))
  146. {
  147. switch($this->_parentPropertyName) // Another one is already bound
  148. {
  149. case "_left":
  150. return $this->_parent->getLeft();
  151. case "_right":
  152. return $this->_parent->getRight();
  153. case "_top":
  154. return $this->_parent->getTop();
  155. case "_bottom":
  156. return $this->_parent->getBottom();
  157. case "_diagonal":
  158. return $this->_parent->getDiagonal();
  159. case "_vertical":
  160. return $this->_parent->getVertical();
  161. case "_horizontal":
  162. return $this->_parent->getHorizontal();
  163. }
  164. }
  165. $this->_parent->propertyCompleteBind($this, $this->_parentPropertyName); // Bind myself
  166. $this->_parent = null;
  167. return $this;
  168. }
  169. /**
  170. * Apply styles from array
  171. *
  172. * <code>
  173. * $objPHPExcel->getActiveSheet()->getStyle('B2')->getBorders()->getTop()->applyFromArray(
  174. * array(
  175. * 'style' => PHPExcel_Style_Border::BORDER_DASHDOT,
  176. * 'color' => array(
  177. * 'rgb' => '808080'
  178. * )
  179. * )
  180. * );
  181. * </code>
  182. *
  183. * @param array $pStyles Array containing style information
  184. * @throws Exception
  185. */
  186. public function applyFromArray($pStyles = null) {
  187. if (is_array($pStyles)) {
  188. if (array_key_exists('style', $pStyles)) {
  189. $this->setBorderStyle($pStyles['style']);
  190. }
  191. if (array_key_exists('color', $pStyles)) {
  192. $this->getColor()->applyFromArray($pStyles['color']);
  193. }
  194. } else {
  195. throw new Exception("Invalid style array passed.");
  196. }
  197. }
  198. /**
  199. * Get Border style
  200. *
  201. * @return string
  202. */
  203. public function getBorderStyle() {
  204. return $this->propertyGetBound()->_borderStyle;
  205. }
  206. /**
  207. * Set Border style
  208. *
  209. * @param string $pValue
  210. */
  211. public function setBorderStyle($pValue = PHPExcel_Style_Border::BORDER_NONE) {
  212. if ($pValue == '') {
  213. $pValue = PHPExcel_Style_Border::BORDER_NONE;
  214. }
  215. $this->propertyBeginBind()->_borderStyle = $pValue;
  216. }
  217. /**
  218. * Get Border Color
  219. *
  220. * @return PHPExcel_Style_Color
  221. */
  222. public function getColor() {
  223. // It's a get but it may lead to a modified color which we won't detect but in which case we must bind.
  224. // So bind as an assurance.
  225. return $this->propertyBeginBind()->_borderColor;
  226. }
  227. /**
  228. * Set Border Color
  229. *
  230. * @param PHPExcel_Style_Color $pValue
  231. * @throws Exception
  232. */
  233. public function setColor(PHPExcel_Style_Color $pValue = null) {
  234. $this->propertyBeginBind()->_borderColor = $pValue;
  235. }
  236. /**
  237. * Get hash code
  238. *
  239. * @return string Hash code
  240. */
  241. public function getHashCode() {
  242. $property = $this->propertyGetBound();
  243. return md5(
  244. $property->_borderStyle
  245. . $property->_borderColor->getHashCode()
  246. . __CLASS__
  247. );
  248. }
  249. /**
  250. * Hash index
  251. *
  252. * @var string
  253. */
  254. private $_hashIndex;
  255. /**
  256. * Get hash index
  257. *
  258. * Note that this index may vary during script execution! Only reliable moment is
  259. * while doing a write of a workbook and when changes are not allowed.
  260. *
  261. * @return string Hash index
  262. */
  263. public function getHashIndex() {
  264. return $this->_hashIndex;
  265. }
  266. /**
  267. * Set hash index
  268. *
  269. * Note that this index may vary during script execution! Only reliable moment is
  270. * while doing a write of a workbook and when changes are not allowed.
  271. *
  272. * @param string $value Hash index
  273. */
  274. public function setHashIndex($value) {
  275. $this->_hashIndex = $value;
  276. }
  277. /**
  278. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  279. */
  280. public function __clone() {
  281. $vars = get_object_vars($this);
  282. foreach ($vars as $key => $value) {
  283. if (is_object($value)) {
  284. $this->$key = clone $value;
  285. } else {
  286. $this->$key = $value;
  287. }
  288. }
  289. }
  290. }