1: <?php
2: /**
3: * PHPExcel
4: *
5: * Copyright (c) 2006 - 2014 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 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
24: * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
25: * @version 1.8.0, 2014-03-02
26: */
27:
28:
29: /**
30: * PHPExcel_Style_Border
31: *
32: * @category PHPExcel
33: * @package PHPExcel_Style
34: * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
35: */
36: class PHPExcel_Style_Border extends PHPExcel_Style_Supervisor 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';
53:
54: /**
55: * Border style
56: *
57: * @var string
58: */
59: protected $_borderStyle = PHPExcel_Style_Border::BORDER_NONE;
60:
61: /**
62: * Border color
63: *
64: * @var PHPExcel_Style_Color
65: */
66: protected $_color;
67:
68: /**
69: * Parent property name
70: *
71: * @var string
72: */
73: protected $_parentPropertyName;
74:
75: /**
76: * Create a new PHPExcel_Style_Border
77: *
78: * @param boolean $isSupervisor Flag indicating if this is a supervisor or not
79: * Leave this value at default unless you understand exactly what
80: * its ramifications are
81: * @param boolean $isConditional Flag indicating if this is a conditional style or not
82: * Leave this value at default unless you understand exactly what
83: * its ramifications are
84: */
85: public function __construct($isSupervisor = FALSE, $isConditional = FALSE)
86: {
87: // Supervisor?
88: parent::__construct($isSupervisor);
89:
90: // Initialise values
91: $this->_color = new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_BLACK, $isSupervisor);
92:
93: // bind parent if we are a supervisor
94: if ($isSupervisor) {
95: $this->_color->bindParent($this, '_color');
96: }
97: }
98:
99: /**
100: * Bind parent. Only used for supervisor
101: *
102: * @param PHPExcel_Style_Borders $parent
103: * @param string $parentPropertyName
104: * @return PHPExcel_Style_Border
105: */
106: public function bindParent($parent, $parentPropertyName=NULL)
107: {
108: $this->_parent = $parent;
109: $this->_parentPropertyName = $parentPropertyName;
110: return $this;
111: }
112:
113: /**
114: * Get the shared style component for the currently active cell in currently active sheet.
115: * Only used for style supervisor
116: *
117: * @return PHPExcel_Style_Border
118: * @throws PHPExcel_Exception
119: */
120: public function getSharedComponent()
121: {
122: switch ($this->_parentPropertyName) {
123: case '_allBorders':
124: case '_horizontal':
125: case '_inside':
126: case '_outline':
127: case '_vertical':
128: throw new PHPExcel_Exception('Cannot get shared component for a pseudo-border.');
129: break;
130: case '_bottom':
131: return $this->_parent->getSharedComponent()->getBottom(); break;
132: case '_diagonal':
133: return $this->_parent->getSharedComponent()->getDiagonal(); break;
134: case '_left':
135: return $this->_parent->getSharedComponent()->getLeft(); break;
136: case '_right':
137: return $this->_parent->getSharedComponent()->getRight(); break;
138: case '_top':
139: return $this->_parent->getSharedComponent()->getTop(); break;
140:
141: }
142: }
143:
144: /**
145: * Build style array from subcomponents
146: *
147: * @param array $array
148: * @return array
149: */
150: public function getStyleArray($array)
151: {
152: switch ($this->_parentPropertyName) {
153: case '_allBorders':
154: $key = 'allborders'; break;
155: case '_bottom':
156: $key = 'bottom'; break;
157: case '_diagonal':
158: $key = 'diagonal'; break;
159: case '_horizontal':
160: $key = 'horizontal'; break;
161: case '_inside':
162: $key = 'inside'; break;
163: case '_left':
164: $key = 'left'; break;
165: case '_outline':
166: $key = 'outline'; break;
167: case '_right':
168: $key = 'right'; break;
169: case '_top':
170: $key = 'top'; break;
171: case '_vertical':
172: $key = 'vertical'; break;
173: }
174: return $this->_parent->getStyleArray(array($key => $array));
175: }
176:
177: /**
178: * Apply styles from array
179: *
180: * <code>
181: * $objPHPExcel->getActiveSheet()->getStyle('B2')->getBorders()->getTop()->applyFromArray(
182: * array(
183: * 'style' => PHPExcel_Style_Border::BORDER_DASHDOT,
184: * 'color' => array(
185: * 'rgb' => '808080'
186: * )
187: * )
188: * );
189: * </code>
190: *
191: * @param array $pStyles Array containing style information
192: * @throws PHPExcel_Exception
193: * @return PHPExcel_Style_Border
194: */
195: public function applyFromArray($pStyles = null) {
196: if (is_array($pStyles)) {
197: if ($this->_isSupervisor) {
198: $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
199: } else {
200: if (isset($pStyles['style'])) {
201: $this->setBorderStyle($pStyles['style']);
202: }
203: if (isset($pStyles['color'])) {
204: $this->getColor()->applyFromArray($pStyles['color']);
205: }
206: }
207: } else {
208: throw new PHPExcel_Exception("Invalid style array passed.");
209: }
210: return $this;
211: }
212:
213: /**
214: * Get Border style
215: *
216: * @return string
217: */
218: public function getBorderStyle() {
219: if ($this->_isSupervisor) {
220: return $this->getSharedComponent()->getBorderStyle();
221: }
222: return $this->_borderStyle;
223: }
224:
225: /**
226: * Set Border style
227: *
228: * @param string|boolean $pValue
229: * When passing a boolean, FALSE equates PHPExcel_Style_Border::BORDER_NONE
230: * and TRUE to PHPExcel_Style_Border::BORDER_MEDIUM
231: * @return PHPExcel_Style_Border
232: */
233: public function setBorderStyle($pValue = PHPExcel_Style_Border::BORDER_NONE) {
234:
235: if (empty($pValue)) {
236: $pValue = PHPExcel_Style_Border::BORDER_NONE;
237: } elseif(is_bool($pValue) && $pValue) {
238: $pValue = PHPExcel_Style_Border::BORDER_MEDIUM;
239: }
240: if ($this->_isSupervisor) {
241: $styleArray = $this->getStyleArray(array('style' => $pValue));
242: $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
243: } else {
244: $this->_borderStyle = $pValue;
245: }
246: return $this;
247: }
248:
249: /**
250: * Get Border Color
251: *
252: * @return PHPExcel_Style_Color
253: */
254: public function getColor() {
255: return $this->_color;
256: }
257:
258: /**
259: * Set Border Color
260: *
261: * @param PHPExcel_Style_Color $pValue
262: * @throws PHPExcel_Exception
263: * @return PHPExcel_Style_Border
264: */
265: public function setColor(PHPExcel_Style_Color $pValue = null) {
266: // make sure parameter is a real color and not a supervisor
267: $color = $pValue->getIsSupervisor() ? $pValue->getSharedComponent() : $pValue;
268:
269: if ($this->_isSupervisor) {
270: $styleArray = $this->getColor()->getStyleArray(array('argb' => $color->getARGB()));
271: $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
272: } else {
273: $this->_color = $color;
274: }
275: return $this;
276: }
277:
278: /**
279: * Get hash code
280: *
281: * @return string Hash code
282: */
283: public function getHashCode() {
284: if ($this->_isSupervisor) {
285: return $this->getSharedComponent()->getHashCode();
286: }
287: return md5(
288: $this->_borderStyle
289: . $this->_color->getHashCode()
290: . __CLASS__
291: );
292: }
293:
294: }
295: