1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Style; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\IComparable; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Copyright (c) 2006 - 2016 PhpSpreadsheet. |
10
|
|
|
* |
11
|
|
|
* This library is free software; you can redistribute it and/or |
12
|
|
|
* modify it under the terms of the GNU Lesser General Public |
13
|
|
|
* License as published by the Free Software Foundation; either |
14
|
|
|
* version 2.1 of the License, or (at your option) any later version. |
15
|
|
|
* |
16
|
|
|
* This library is distributed in the hope that it will be useful, |
17
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
18
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
19
|
|
|
* Lesser General Public License for more details. |
20
|
|
|
* |
21
|
|
|
* You should have received a copy of the GNU Lesser General Public |
22
|
|
|
* License along with this library; if not, write to the Free Software |
23
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
24
|
|
|
* |
25
|
|
|
* @category PhpSpreadsheet |
26
|
|
|
* |
27
|
|
|
* @copyright Copyright (c) 2006 - 2016 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet) |
28
|
|
|
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL |
29
|
|
|
*/ |
30
|
|
|
class Border extends Supervisor implements IComparable |
31
|
|
|
{ |
32
|
|
|
/* Border style */ |
33
|
|
|
const BORDER_NONE = 'none'; |
34
|
|
|
const BORDER_DASHDOT = 'dashDot'; |
35
|
|
|
const BORDER_DASHDOTDOT = 'dashDotDot'; |
36
|
|
|
const BORDER_DASHED = 'dashed'; |
37
|
|
|
const BORDER_DOTTED = 'dotted'; |
38
|
|
|
const BORDER_DOUBLE = 'double'; |
39
|
|
|
const BORDER_HAIR = 'hair'; |
40
|
|
|
const BORDER_MEDIUM = 'medium'; |
41
|
|
|
const BORDER_MEDIUMDASHDOT = 'mediumDashDot'; |
42
|
|
|
const BORDER_MEDIUMDASHDOTDOT = 'mediumDashDotDot'; |
43
|
|
|
const BORDER_MEDIUMDASHED = 'mediumDashed'; |
44
|
|
|
const BORDER_SLANTDASHDOT = 'slantDashDot'; |
45
|
|
|
const BORDER_THICK = 'thick'; |
46
|
|
|
const BORDER_THIN = 'thin'; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Border style. |
50
|
|
|
* |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
protected $borderStyle = self::BORDER_NONE; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Border color. |
57
|
|
|
* |
58
|
|
|
* @var Color |
59
|
|
|
*/ |
60
|
|
|
protected $color; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Parent property name. |
64
|
|
|
* |
65
|
|
|
* @var string |
66
|
|
|
*/ |
67
|
|
|
protected $parentPropertyName; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Create a new Border. |
71
|
|
|
* |
72
|
|
|
* @param bool $isSupervisor Flag indicating if this is a supervisor or not |
73
|
|
|
* Leave this value at default unless you understand exactly what |
74
|
|
|
* its ramifications are |
75
|
|
|
* @param bool $isConditional Flag indicating if this is a conditional style or not |
76
|
|
|
* Leave this value at default unless you understand exactly what |
77
|
|
|
* its ramifications are |
78
|
|
|
*/ |
79
|
80 |
|
public function __construct($isSupervisor = false, $isConditional = false) |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
// Supervisor? |
82
|
80 |
|
parent::__construct($isSupervisor); |
83
|
|
|
|
84
|
|
|
// Initialise values |
85
|
80 |
|
$this->color = new Color(Color::COLOR_BLACK, $isSupervisor); |
86
|
|
|
|
87
|
|
|
// bind parent if we are a supervisor |
88
|
80 |
|
if ($isSupervisor) { |
89
|
80 |
|
$this->color->bindParent($this, 'color'); |
90
|
|
|
} |
91
|
80 |
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Bind parent. Only used for supervisor. |
95
|
|
|
* |
96
|
|
|
* @param Borders $parent |
97
|
|
|
* @param string $parentPropertyName |
98
|
|
|
* |
99
|
|
|
* @return Border |
100
|
|
|
*/ |
101
|
80 |
|
public function bindParent($parent, $parentPropertyName = null) |
102
|
|
|
{ |
103
|
80 |
|
$this->parent = $parent; |
|
|
|
|
104
|
80 |
|
$this->parentPropertyName = $parentPropertyName; |
105
|
|
|
|
106
|
80 |
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Get the shared style component for the currently active cell in currently active sheet. |
111
|
|
|
* Only used for style supervisor. |
112
|
|
|
* |
113
|
|
|
* @throws PhpSpreadsheetException |
114
|
|
|
* |
115
|
|
|
* @return Border |
116
|
|
|
*/ |
117
|
1 |
|
public function getSharedComponent() |
118
|
|
|
{ |
119
|
1 |
|
switch ($this->parentPropertyName) { |
120
|
1 |
|
case 'allBorders': |
121
|
1 |
|
case 'horizontal': |
122
|
1 |
|
case 'inside': |
123
|
1 |
|
case 'outline': |
124
|
1 |
|
case 'vertical': |
125
|
|
|
throw new PhpSpreadsheetException('Cannot get shared component for a pseudo-border.'); |
126
|
|
|
break; |
|
|
|
|
127
|
1 |
|
case 'bottom': |
128
|
1 |
|
return $this->parent->getSharedComponent()->getBottom(); |
|
|
|
|
129
|
|
|
case 'diagonal': |
130
|
|
|
return $this->parent->getSharedComponent()->getDiagonal(); |
|
|
|
|
131
|
|
|
case 'left': |
132
|
|
|
return $this->parent->getSharedComponent()->getLeft(); |
|
|
|
|
133
|
|
|
case 'right': |
134
|
|
|
return $this->parent->getSharedComponent()->getRight(); |
|
|
|
|
135
|
|
|
case 'top': |
136
|
|
|
return $this->parent->getSharedComponent()->getTop(); |
|
|
|
|
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Build style array from subcomponents. |
142
|
|
|
* |
143
|
|
|
* @param array $array |
144
|
|
|
* |
145
|
|
|
* @return array |
146
|
|
|
*/ |
147
|
1 |
|
public function getStyleArray($array) |
148
|
|
|
{ |
149
|
1 |
|
return $this->parent->getStyleArray([$this->parentPropertyName => $array]); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Apply styles from array. |
154
|
|
|
* |
155
|
|
|
* <code> |
156
|
|
|
* $spreadsheet->getActiveSheet()->getStyle('B2')->getBorders()->getTop()->applyFromArray( |
157
|
|
|
* array( |
158
|
|
|
* 'borderStyle' => Border::BORDER_DASHDOT, |
159
|
|
|
* 'color' => array( |
160
|
|
|
* 'rgb' => '808080' |
161
|
|
|
* ) |
162
|
|
|
* ) |
163
|
|
|
* ); |
164
|
|
|
* </code> |
165
|
|
|
* |
166
|
|
|
* @param array $pStyles Array containing style information |
167
|
|
|
* |
168
|
|
|
* @throws PhpSpreadsheetException |
169
|
|
|
* |
170
|
|
|
* @return Border |
171
|
|
|
*/ |
172
|
18 |
|
public function applyFromArray(array $pStyles) |
173
|
|
|
{ |
174
|
18 |
|
if ($this->isSupervisor) { |
175
|
|
|
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles)); |
176
|
|
|
} else { |
177
|
18 |
|
if (isset($pStyles['borderStyle'])) { |
178
|
18 |
|
$this->setBorderStyle($pStyles['borderStyle']); |
179
|
|
|
} |
180
|
18 |
|
if (isset($pStyles['color'])) { |
181
|
14 |
|
$this->getColor()->applyFromArray($pStyles['color']); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
18 |
|
return $this; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Get Border style. |
190
|
|
|
* |
191
|
|
|
* @return string |
192
|
|
|
*/ |
193
|
63 |
|
public function getBorderStyle() |
194
|
|
|
{ |
195
|
63 |
|
if ($this->isSupervisor) { |
196
|
1 |
|
return $this->getSharedComponent()->getBorderStyle(); |
197
|
|
|
} |
198
|
|
|
|
199
|
63 |
|
return $this->borderStyle; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Set Border style. |
204
|
|
|
* |
205
|
|
|
* @param string|bool $pValue |
206
|
|
|
* When passing a boolean, FALSE equates Border::BORDER_NONE |
207
|
|
|
* and TRUE to Border::BORDER_MEDIUM |
208
|
|
|
* |
209
|
|
|
* @return Border |
210
|
|
|
*/ |
211
|
21 |
View Code Duplication |
public function setBorderStyle($pValue) |
|
|
|
|
212
|
|
|
{ |
213
|
21 |
|
if (empty($pValue)) { |
214
|
|
|
$pValue = self::BORDER_NONE; |
215
|
21 |
|
} elseif (is_bool($pValue) && $pValue) { |
216
|
|
|
$pValue = self::BORDER_MEDIUM; |
217
|
|
|
} |
218
|
21 |
|
if ($this->isSupervisor) { |
219
|
1 |
|
$styleArray = $this->getStyleArray(['borderStyle' => $pValue]); |
220
|
1 |
|
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
221
|
|
|
} else { |
222
|
21 |
|
$this->borderStyle = $pValue; |
|
|
|
|
223
|
|
|
} |
224
|
|
|
|
225
|
21 |
|
return $this; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Get Border Color. |
230
|
|
|
* |
231
|
|
|
* @return Color |
232
|
|
|
*/ |
233
|
47 |
|
public function getColor() |
234
|
|
|
{ |
235
|
47 |
|
return $this->color; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Set Border Color. |
240
|
|
|
* |
241
|
|
|
* @param Color $pValue |
242
|
|
|
* |
243
|
|
|
* @throws PhpSpreadsheetException |
244
|
|
|
* |
245
|
|
|
* @return Border |
246
|
|
|
*/ |
247
|
|
View Code Duplication |
public function setColor(Color $pValue) |
|
|
|
|
248
|
|
|
{ |
249
|
|
|
// make sure parameter is a real color and not a supervisor |
250
|
|
|
$color = $pValue->getIsSupervisor() ? $pValue->getSharedComponent() : $pValue; |
251
|
|
|
|
252
|
|
|
if ($this->isSupervisor) { |
253
|
|
|
$styleArray = $this->getColor()->getStyleArray(['argb' => $color->getARGB()]); |
254
|
|
|
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
255
|
|
|
} else { |
256
|
|
|
$this->color = $color; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
return $this; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Get hash code. |
264
|
|
|
* |
265
|
|
|
* @return string Hash code |
266
|
|
|
*/ |
267
|
70 |
|
public function getHashCode() |
268
|
|
|
{ |
269
|
70 |
|
if ($this->isSupervisor) { |
270
|
|
|
return $this->getSharedComponent()->getHashCode(); |
271
|
|
|
} |
272
|
|
|
|
273
|
70 |
|
return md5( |
274
|
70 |
|
$this->borderStyle . |
275
|
70 |
|
$this->color->getHashCode() . |
276
|
70 |
|
__CLASS__ |
277
|
|
|
); |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.