File Source for Border.php

PHPExcel_Style

# Source for file Border.php

Documentation is available at Border.php

1. <?php
2. /**
3. * PHPExcel
4. *
5. * Copyright (c) 2006 - 2011 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 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
25. * @version 1.7.6, 2011-02-27
26. */
29. /**
30. * PHPExcel_Style_Border
31. *
32. * @category PHPExcel
33. * @package PHPExcel_Style
34. * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
35. */
36. class PHPExcel_Style_Border implements PHPExcel_IComparable
37. {
38. /* Border style */
39. const BORDER_NONE = 'none';
40. const BORDER_DASHDOT = 'dashDot';
41. const BORDER_DASHDOTDOT = 'dashDotDot';
42. const BORDER_DASHED = 'dashed';
43. const BORDER_DOTTED = 'dotted';
44. const BORDER_DOUBLE = 'double';
45. const BORDER_HAIR = 'hair';
46. const BORDER_MEDIUM = 'medium';
47. const BORDER_MEDIUMDASHDOT = 'mediumDashDot';
48. const BORDER_MEDIUMDASHDOTDOT = 'mediumDashDotDot';
49. const BORDER_MEDIUMDASHED = 'mediumDashed';
50. const BORDER_SLANTDASHDOT = 'slantDashDot';
51. const BORDER_THICK = 'thick';
52. const BORDER_THIN = 'thin';
54. /**
55. * Border style
56. *
57. * @var string
58. */
59. private $_borderStyle = PHPExcel_Style_Border:: BORDER_NONE;
61. /**
62. * Border color
63. *
64. * @var PHPExcel_Style_Color
65. */
66. private $_color;
68. /**
69. * Supervisor?
70. *
71. * @var boolean
72. */
73. private $_isSupervisor;
75. /**
76. * Parent. Only used for supervisor
77. *
78. * @var PHPExcel_Style_Borders
79. */
80. private $_parent;
82. /**
83. * Parent property name
84. *
85. * @var string
86. */
87. private $_parentPropertyName;
89. /**
90. * Create a new PHPExcel_Style_Border
91. */
92. public function __construct($isSupervisor = false)
93. {
94. // Supervisor?
95. $this->_isSupervisor = $isSupervisor;
97. // Initialise values
98. $this->_color = new PHPExcel_Style_Color( PHPExcel_Style_Color:: COLOR_BLACK, $isSupervisor);
100. // bind parent if we are a supervisor
101. if ($isSupervisor) {
102. $this->_color-> bindParent($this, '_color');
103. }
104. }
106. /**
107. * Bind parent. Only used for supervisor
108. *
109. * @param PHPExcel_Style_Borders $parent
110. * @param string $parentPropertyName
111. * @return PHPExcel_Style_Border
112. */
113. public function bindParent($parent, $parentPropertyName)
114. {
115. $this->_parent = $parent;
116. $this->_parentPropertyName = $parentPropertyName;
117. return $this;
118. }
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. }
130. /**
131. * Get the shared style component for the currently active cell in currently active sheet.
132. * Only used for style supervisor
133. *
134. * @return PHPExcel_Style_Border
135. * @throws Exception
136. */
137. public function getSharedComponent()
138. {
139. switch ($this->_parentPropertyName) {
140. case '_allBorders':
141. case '_horizontal':
142. case '_inside':
143. case '_outline':
144. case '_vertical':
145. throw new Exception('Cannot get shared component for a pseudo-border.');
146. break;
148. case '_bottom':
149. return $this->_parent-> getSharedComponent()->getBottom();
150. break;
152. case '_diagonal':
153. return $this->_parent-> getSharedComponent()->getDiagonal();
154. break;
156. case '_left':
157. return $this->_parent-> getSharedComponent()->getLeft();
158. break;
160. case '_right':
161. return $this->_parent-> getSharedComponent()->getRight();
162. break;
164. case '_top':
165. return $this->_parent-> getSharedComponent()->getTop();
166. break;
168. }
169. }
171. /**
172. * Get the currently active sheet. Only used for supervisor
173. *
174. * @return PHPExcel_Worksheet
175. */
176. public function getActiveSheet()
177. {
178. return $this->_parent-> getActiveSheet();
179. }
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 getSelectedCells()
188. {
189. return $this-> getActiveSheet()->getSelectedCells();
190. }
192. /**
193. * Get the currently active cell coordinate in currently active sheet.
194. * Only used for supervisor
195. *
196. * @return string E.g. 'A1'
197. */
198. public function getActiveCell()
199. {
200. return $this-> getActiveSheet()->getActiveCell();
201. }
203. /**
204. * Build style array from subcomponents
205. *
206. * @param array $array
207. * @return array
208. */
209. public function getStyleArray($array)
210. {
211. switch ($this->_parentPropertyName) {
212. case '_allBorders':
213. $key = 'allborders';
214. break;
216. case '_bottom':
217. $key = 'bottom';
218. break;
220. case '_diagonal':
221. $key = 'diagonal';
222. break;
224. case '_horizontal':
225. $key = 'horizontal';
226. break;
228. case '_inside':
229. $key = 'inside';
230. break;
232. case '_left':
233. $key = 'left';
234. break;
236. case '_outline':
237. $key = 'outline';
238. break;
240. case '_right':
241. $key = 'right';
242. break;
244. case '_top':
245. $key = 'top';
246. break;
248. case '_vertical':
249. $key = 'vertical';
250. break;
251. }
252. return $this->_parent-> getStyleArray(array($key => $array));
253. }
255. /**
256. * Apply styles from array
257. *
258. * <code>
259. * $objPHPExcel->getActiveSheet()->getStyle('B2')->getBorders()->getTop()->applyFromArray(
260. * array(
261. * 'style' => PHPExcel_Style_Border::BORDER_DASHDOT,
262. * 'color' => array(
263. * 'rgb' => '808080'
264. * )
265. * )
266. * );
267. * </code>
268. *
269. * @param array $pStyles Array containing style information
270. * @throws Exception
271. * @return PHPExcel_Style_Border
272. */
273. public function applyFromArray($pStyles = null) {
274. if ( is_array($pStyles)) {
275. if ($this->_isSupervisor) {
276. $this-> getActiveSheet()->getStyle($this-> getSelectedCells())->applyFromArray($this-> getStyleArray($pStyles));
277. } else {
278. if ( array_key_exists('style', $pStyles)) {
279. $this-> setBorderStyle($pStyles['style']);
280. }
281. if ( array_key_exists('color', $pStyles)) {
282. $this-> getColor()->applyFromArray($pStyles['color']);
283. }
284. }
285. } else {
286. throw new Exception("Invalid style array passed.");
287. }
288. return $this;
289. }
291. /**
292. * Get Border style
293. *
294. * @return string
295. */
296. public function getBorderStyle() {
297. if ($this->_isSupervisor) {
298. return $this-> getSharedComponent()->getBorderStyle();
299. }
300. return $this->_borderStyle;
301. }
303. /**
304. * Set Border style
305. *
306. * @param string $pValue
307. * @return PHPExcel_Style_Border
308. */
309. public function setBorderStyle($pValue = PHPExcel_Style_Border:: BORDER_NONE) {
311. if ($pValue == '') {
312. $pValue = PHPExcel_Style_Border:: BORDER_NONE;
313. }
314. if ($this->_isSupervisor) {
315. $styleArray = $this-> getStyleArray(array('style' => $pValue));
316. $this-> getActiveSheet()->getStyle($this-> getSelectedCells())->applyFromArray($styleArray);
317. } else {
318. $this->_borderStyle = $pValue;
319. }
320. return $this;
321. }
323. /**
324. * Get Border Color
325. *
326. * @return PHPExcel_Style_Color
327. */
328. public function getColor() {
329. return $this->_color;
330. }
332. /**
333. * Set Border Color
334. *
335. * @param PHPExcel_Style_Color $pValue
336. * @throws Exception
337. * @return PHPExcel_Style_Border
338. */
339. public function setColor(PHPExcel_Style_Color $pValue = null) {
340. // make sure parameter is a real color and not a supervisor
341. $color = $pValue->getIsSupervisor() ? $pValue->getSharedComponent() : $pValue;
343. if ($this->_isSupervisor) {
344. $styleArray = $this-> getColor()->getStyleArray(array('argb' => $color->getARGB()));
345. $this-> getActiveSheet()->getStyle($this-> getSelectedCells())->applyFromArray($styleArray);
346. } else {
347. $this->_color = $color;
348. }
349. return $this;
350. }
352. /**
353. * Get hash code
354. *
355. * @return string Hash code
356. */
357. public function getHashCode() {
358. if ($this->_isSupervisor) {
359. return $this-> getSharedComponent()->getHashCode();
360. }
361. return md5(
362. $this->_borderStyle
363. . $this->_color-> getHashCode()
364. . __CLASS__
365. );
366. }
368. /**
369. * Implement PHP __clone to create a deep clone, not just a shallow copy.
370. */
371. public function __clone() {
372. $vars = get_object_vars($this);
373. foreach ($vars as $key => $value) {
374. if (( is_object($value)) && ($key != '_parent')) {
375. $this->$key = clone $value;
376. } else {
377. $this->$key = $value;
378. }
379. }
380. }
381. }

Documentation generated on Sun, 27 Feb 2011 16:28:38 -0800 by phpDocumentor 1.4.3