PageRenderTime 26ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/gui/tools/pma/libraries/PHPExcel/PHPExcel/Style/Border.php

https://github.com/okbutton/ispCP-distributed
PHP | 428 lines | 197 code | 48 blank | 183 comment | 20 complexity | d17cf4b1fa48451b4edb7b0455d9f4c5 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.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 1.7.0, 2009-08-10
  26. */
  27. /** PHPExcel root directory */
  28. if (!defined('PHPEXCEL_ROOT')) {
  29. /**
  30. * @ignore
  31. */
  32. define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
  33. }
  34. /** PHPExcel_Style_Color */
  35. require_once PHPEXCEL_ROOT . 'PHPExcel/Style/Color.php';
  36. /** PHPExcel_IComparable */
  37. require_once PHPEXCEL_ROOT . 'PHPExcel/IComparable.php';
  38. /**
  39. * PHPExcel_Style_Border
  40. *
  41. * @category PHPExcel
  42. * @package PHPExcel_Style
  43. * @copyright Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  44. */
  45. class PHPExcel_Style_Border implements PHPExcel_IComparable
  46. {
  47. /* Border style */
  48. const BORDER_NONE = 'none';
  49. const BORDER_DASHDOT = 'dashDot';
  50. const BORDER_DASHDOTDOT = 'dashDotDot';
  51. const BORDER_DASHED = 'dashed';
  52. const BORDER_DOTTED = 'dotted';
  53. const BORDER_DOUBLE = 'double';
  54. const BORDER_HAIR = 'hair';
  55. const BORDER_MEDIUM = 'medium';
  56. const BORDER_MEDIUMDASHDOT = 'mediumDashDot';
  57. const BORDER_MEDIUMDASHDOTDOT = 'mediumDashDotDot';
  58. const BORDER_MEDIUMDASHED = 'mediumDashed';
  59. const BORDER_SLANTDASHDOT = 'slantDashDot';
  60. const BORDER_THICK = 'thick';
  61. const BORDER_THIN = 'thin';
  62. /**
  63. * Border style
  64. *
  65. * @var string
  66. */
  67. private $_borderStyle;
  68. /**
  69. * Border color
  70. *
  71. * @var PHPExcel_Style_Color
  72. */
  73. private $_color;
  74. /**
  75. * Supervisor?
  76. *
  77. * @var boolean
  78. */
  79. private $_isSupervisor;
  80. /**
  81. * Parent. Only used for supervisor
  82. *
  83. * @var PHPExcel_Style_Borders
  84. */
  85. private $_parent;
  86. /**
  87. * Parent property name
  88. *
  89. * @var string
  90. */
  91. private $_parentPropertyName;
  92. /**
  93. * Create a new PHPExcel_Style_Border
  94. */
  95. public function __construct($isSupervisor = false)
  96. {
  97. // Supervisor?
  98. $this->_isSupervisor = $isSupervisor;
  99. // Initialise values
  100. $this->_borderStyle = PHPExcel_Style_Border::BORDER_NONE;
  101. $this->_color = new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_BLACK, $isSupervisor);
  102. // bind parent if we are a supervisor
  103. if ($isSupervisor) {
  104. $this->_color->bindParent($this, '_color');
  105. }
  106. }
  107. /**
  108. * Bind parent. Only used for supervisor
  109. *
  110. * @param PHPExcel_Style_Borders $parent
  111. * @param string $parentPropertyName
  112. * @return PHPExcel_Style_Border
  113. */
  114. public function bindParent($parent, $parentPropertyName)
  115. {
  116. $this->_parent = $parent;
  117. $this->_parentPropertyName = $parentPropertyName;
  118. return $this;
  119. }
  120. /**
  121. * Is this a supervisor or a real style component?
  122. *
  123. * @return boolean
  124. */
  125. public function getIsSupervisor()
  126. {
  127. return $this->_isSupervisor;
  128. }
  129. /**
  130. * Get the shared style component for the currently active cell in currently active sheet.
  131. * Only used for style supervisor
  132. *
  133. * @return PHPExcel_Style_Border
  134. * @throws Exception
  135. */
  136. public function getSharedComponent()
  137. {
  138. switch ($this->_parentPropertyName) {
  139. case '_allBorders':
  140. case '_horizontal':
  141. case '_inside':
  142. case '_outline':
  143. case '_vertical':
  144. throw new Exception('Cannot get shared component for a pseudo-border.');
  145. break;
  146. case '_bottom':
  147. return $this->_parent->getSharedComponent()->getBottom();
  148. break;
  149. case '_diagonal':
  150. return $this->_parent->getSharedComponent()->getDiagonal();
  151. break;
  152. case '_left':
  153. return $this->_parent->getSharedComponent()->getLeft();
  154. break;
  155. case '_right':
  156. return $this->_parent->getSharedComponent()->getRight();
  157. break;
  158. case '_top':
  159. return $this->_parent->getSharedComponent()->getTop();
  160. break;
  161. }
  162. }
  163. /**
  164. * Get the currently active sheet. Only used for supervisor
  165. *
  166. * @return PHPExcel_Worksheet
  167. */
  168. public function getActiveSheet()
  169. {
  170. return $this->_parent->getActiveSheet();
  171. }
  172. /**
  173. * Get the currently active cell coordinate in currently active sheet.
  174. * Only used for supervisor
  175. *
  176. * @return string E.g. 'A1'
  177. */
  178. public function getXSelectedCells()
  179. {
  180. return $this->getActiveSheet()->getXSelectedCells();
  181. }
  182. /**
  183. * Get the currently active cell coordinate in currently active sheet.
  184. * Only used for supervisor
  185. *
  186. * @return string E.g. 'A1'
  187. */
  188. public function getXActiveCell()
  189. {
  190. return $this->getActiveSheet()->getXActiveCell();
  191. }
  192. /**
  193. * Build style array from subcomponents
  194. *
  195. * @param array $array
  196. * @return array
  197. */
  198. public function getStyleArray($array)
  199. {
  200. switch ($this->_parentPropertyName) {
  201. case '_allBorders':
  202. $key = 'allborders';
  203. break;
  204. case '_bottom':
  205. $key = 'bottom';
  206. break;
  207. case '_diagonal':
  208. $key = 'diagonal';
  209. break;
  210. case '_horizontal':
  211. $key = 'horizontal';
  212. break;
  213. case '_inside':
  214. $key = 'inside';
  215. break;
  216. case '_left':
  217. $key = 'left';
  218. break;
  219. case '_outline':
  220. $key = 'outline';
  221. break;
  222. case '_right':
  223. $key = 'right';
  224. break;
  225. case '_top':
  226. $key = 'top';
  227. break;
  228. case '_vertical':
  229. $key = 'vertical';
  230. break;
  231. }
  232. return $this->_parent->getStyleArray(array($key => $array));
  233. }
  234. /**
  235. * Apply styles from array
  236. *
  237. * <code>
  238. * $objPHPExcel->getActiveSheet()->getStyle('B2')->getBorders()->getTop()->applyFromArray(
  239. * array(
  240. * 'style' => PHPExcel_Style_Border::BORDER_DASHDOT,
  241. * 'color' => array(
  242. * 'rgb' => '808080'
  243. * )
  244. * )
  245. * );
  246. * </code>
  247. *
  248. * @param array $pStyles Array containing style information
  249. * @throws Exception
  250. * @return PHPExcel_Style_Border
  251. */
  252. public function applyFromArray($pStyles = null) {
  253. if (is_array($pStyles)) {
  254. if ($this->_isSupervisor) {
  255. $this->getActiveSheet()->getStyle($this->getXSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
  256. } else {
  257. if (array_key_exists('style', $pStyles)) {
  258. $this->setBorderStyle($pStyles['style']);
  259. }
  260. if (array_key_exists('color', $pStyles)) {
  261. $this->getColor()->applyFromArray($pStyles['color']);
  262. }
  263. }
  264. } else {
  265. throw new Exception("Invalid style array passed.");
  266. }
  267. return $this;
  268. }
  269. /**
  270. * Get Border style
  271. *
  272. * @return string
  273. */
  274. public function getBorderStyle() {
  275. if ($this->_isSupervisor) {
  276. return $this->getSharedComponent()->getBorderStyle();
  277. }
  278. return $this->_borderStyle;
  279. }
  280. /**
  281. * Set Border style
  282. *
  283. * @param string $pValue
  284. * @return PHPExcel_Style_Border
  285. */
  286. public function setBorderStyle($pValue = PHPExcel_Style_Border::BORDER_NONE) {
  287. if ($pValue == '') {
  288. $pValue = PHPExcel_Style_Border::BORDER_NONE;
  289. }
  290. if ($this->_isSupervisor) {
  291. $styleArray = $this->getStyleArray(array('style' => $pValue));
  292. $this->getActiveSheet()->getStyle($this->getXSelectedCells())->applyFromArray($styleArray);
  293. } else {
  294. $this->_borderStyle = $pValue;
  295. }
  296. return $this;
  297. }
  298. /**
  299. * Get Border Color
  300. *
  301. * @return PHPExcel_Style_Color
  302. */
  303. public function getColor() {
  304. return $this->_color;
  305. }
  306. /**
  307. * Set Border Color
  308. *
  309. * @param PHPExcel_Style_Color $pValue
  310. * @throws Exception
  311. * @return PHPExcel_Style_Border
  312. */
  313. public function setColor(PHPExcel_Style_Color $pValue = null) {
  314. // make sure parameter is a real color and not a supervisor
  315. $color = $pValue->getIsSupervisor() ? $pValue->getSharedComponent() : $pValue;
  316. if ($this->_isSupervisor) {
  317. $styleArray = $this->getColor()->getStyleArray(array('argb' => $color->getARGB()));
  318. $this->getActiveSheet()->getStyle($this->getXSelectedCells())->applyFromArray($styleArray);
  319. } else {
  320. $this->_color = $color;
  321. }
  322. return $this;
  323. }
  324. /**
  325. * Get hash code
  326. *
  327. * @return string Hash code
  328. */
  329. public function getHashCode() {
  330. if ($this->_isSupervisor) {
  331. return $this->getSharedComponent()->getHashCode();
  332. }
  333. return md5(
  334. $this->_borderStyle
  335. . $this->_color->getHashCode()
  336. . __CLASS__
  337. );
  338. }
  339. /**
  340. * Hash index
  341. *
  342. * @var string
  343. */
  344. private $_hashIndex;
  345. /**
  346. * Get hash index
  347. *
  348. * Note that this index may vary during script execution! Only reliable moment is
  349. * while doing a write of a workbook and when changes are not allowed.
  350. *
  351. * @return string Hash index
  352. */
  353. public function getHashIndex() {
  354. return $this->_hashIndex;
  355. }
  356. /**
  357. * Set hash index
  358. *
  359. * Note that this index may vary during script execution! Only reliable moment is
  360. * while doing a write of a workbook and when changes are not allowed.
  361. *
  362. * @param string $value Hash index
  363. */
  364. public function setHashIndex($value) {
  365. $this->_hashIndex = $value;
  366. }
  367. /**
  368. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  369. */
  370. public function __clone() {
  371. $vars = get_object_vars($this);
  372. foreach ($vars as $key => $value) {
  373. if (is_object($value)) {
  374. $this->$key = clone $value;
  375. } else {
  376. $this->$key = $value;
  377. }
  378. }
  379. }
  380. }