1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Style; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException; |
6
|
|
|
|
7
|
|
|
class Border extends Supervisor |
8
|
|
|
{ |
9
|
|
|
// Border style |
10
|
|
|
const BORDER_NONE = 'none'; |
11
|
|
|
const BORDER_DASHDOT = 'dashDot'; |
12
|
|
|
const BORDER_DASHDOTDOT = 'dashDotDot'; |
13
|
|
|
const BORDER_DASHED = 'dashed'; |
14
|
|
|
const BORDER_DOTTED = 'dotted'; |
15
|
|
|
const BORDER_DOUBLE = 'double'; |
16
|
|
|
const BORDER_HAIR = 'hair'; |
17
|
|
|
const BORDER_MEDIUM = 'medium'; |
18
|
|
|
const BORDER_MEDIUMDASHDOT = 'mediumDashDot'; |
19
|
|
|
const BORDER_MEDIUMDASHDOTDOT = 'mediumDashDotDot'; |
20
|
|
|
const BORDER_MEDIUMDASHED = 'mediumDashed'; |
21
|
|
|
const BORDER_SLANTDASHDOT = 'slantDashDot'; |
22
|
|
|
const BORDER_THICK = 'thick'; |
23
|
|
|
const BORDER_THIN = 'thin'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Border style. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $borderStyle = self::BORDER_NONE; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Border color. |
34
|
|
|
* |
35
|
|
|
* @var Color |
36
|
|
|
*/ |
37
|
|
|
protected $color; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var int |
41
|
|
|
*/ |
42
|
|
|
public $colorIndex; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Create a new Border. |
46
|
|
|
* |
47
|
|
|
* @param bool $isSupervisor Flag indicating if this is a supervisor or not |
48
|
|
|
* Leave this value at default unless you understand exactly what |
49
|
|
|
* its ramifications are |
50
|
|
|
* @param bool $isConditional Flag indicating if this is a conditional style or not |
51
|
|
|
* Leave this value at default unless you understand exactly what |
52
|
|
|
* its ramifications are |
53
|
|
|
*/ |
54
|
185 |
|
public function __construct($isSupervisor = false, $isConditional = false) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
// Supervisor? |
57
|
185 |
|
parent::__construct($isSupervisor); |
58
|
|
|
|
59
|
|
|
// Initialise values |
60
|
185 |
|
$this->color = new Color(Color::COLOR_BLACK, $isSupervisor); |
61
|
|
|
|
62
|
|
|
// bind parent if we are a supervisor |
63
|
185 |
|
if ($isSupervisor) { |
64
|
185 |
|
$this->color->bindParent($this, 'color'); |
|
|
|
|
65
|
|
|
} |
66
|
185 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get the shared style component for the currently active cell in currently active sheet. |
70
|
|
|
* Only used for style supervisor. |
71
|
|
|
* |
72
|
|
|
* @throws PhpSpreadsheetException |
73
|
|
|
* |
74
|
|
|
* @return Border |
75
|
|
|
*/ |
76
|
2 |
|
public function getSharedComponent() |
77
|
|
|
{ |
78
|
2 |
|
switch ($this->parentPropertyName) { |
79
|
2 |
|
case 'allBorders': |
80
|
2 |
|
case 'horizontal': |
81
|
2 |
|
case 'inside': |
82
|
2 |
|
case 'outline': |
83
|
2 |
|
case 'vertical': |
84
|
|
|
throw new PhpSpreadsheetException('Cannot get shared component for a pseudo-border.'); |
85
|
|
|
|
86
|
|
|
break; |
87
|
2 |
|
case 'bottom': |
88
|
2 |
|
return $this->parent->getSharedComponent()->getBottom(); |
|
|
|
|
89
|
1 |
|
case 'diagonal': |
90
|
|
|
return $this->parent->getSharedComponent()->getDiagonal(); |
|
|
|
|
91
|
1 |
|
case 'left': |
92
|
1 |
|
return $this->parent->getSharedComponent()->getLeft(); |
|
|
|
|
93
|
1 |
|
case 'right': |
94
|
1 |
|
return $this->parent->getSharedComponent()->getRight(); |
|
|
|
|
95
|
1 |
|
case 'top': |
96
|
1 |
|
return $this->parent->getSharedComponent()->getTop(); |
|
|
|
|
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Build style array from subcomponents. |
102
|
|
|
* |
103
|
|
|
* @param array $array |
104
|
|
|
* |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
3 |
|
public function getStyleArray($array) |
108
|
|
|
{ |
109
|
3 |
|
return $this->parent->getStyleArray([$this->parentPropertyName => $array]); |
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Apply styles from array. |
114
|
|
|
* |
115
|
|
|
* <code> |
116
|
|
|
* $spreadsheet->getActiveSheet()->getStyle('B2')->getBorders()->getTop()->applyFromArray( |
117
|
|
|
* [ |
118
|
|
|
* 'borderStyle' => Border::BORDER_DASHDOT, |
119
|
|
|
* 'color' => [ |
120
|
|
|
* 'rgb' => '808080' |
121
|
|
|
* ] |
122
|
|
|
* ] |
123
|
|
|
* ); |
124
|
|
|
* </code> |
125
|
|
|
* |
126
|
|
|
* @param array $pStyles Array containing style information |
127
|
|
|
* |
128
|
|
|
* @throws PhpSpreadsheetException |
129
|
|
|
* |
130
|
|
|
* @return Border |
131
|
|
|
*/ |
132
|
21 |
|
public function applyFromArray(array $pStyles) |
133
|
|
|
{ |
134
|
21 |
|
if ($this->isSupervisor) { |
135
|
|
|
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles)); |
136
|
|
|
} else { |
137
|
21 |
|
if (isset($pStyles['borderStyle'])) { |
138
|
21 |
|
$this->setBorderStyle($pStyles['borderStyle']); |
139
|
|
|
} |
140
|
21 |
|
if (isset($pStyles['color'])) { |
141
|
16 |
|
$this->getColor()->applyFromArray($pStyles['color']); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
21 |
|
return $this; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Get Border style. |
150
|
|
|
* |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
99 |
|
public function getBorderStyle() |
154
|
|
|
{ |
155
|
99 |
|
if ($this->isSupervisor) { |
156
|
2 |
|
return $this->getSharedComponent()->getBorderStyle(); |
157
|
|
|
} |
158
|
|
|
|
159
|
99 |
|
return $this->borderStyle; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Set Border style. |
164
|
|
|
* |
165
|
|
|
* @param bool|string $pValue |
166
|
|
|
* When passing a boolean, FALSE equates Border::BORDER_NONE |
167
|
|
|
* and TRUE to Border::BORDER_MEDIUM |
168
|
|
|
* |
169
|
|
|
* @return Border |
170
|
|
|
*/ |
171
|
42 |
|
public function setBorderStyle($pValue) |
172
|
|
|
{ |
173
|
42 |
|
if (empty($pValue)) { |
174
|
|
|
$pValue = self::BORDER_NONE; |
175
|
42 |
|
} elseif (is_bool($pValue) && $pValue) { |
176
|
|
|
$pValue = self::BORDER_MEDIUM; |
177
|
|
|
} |
178
|
42 |
|
if ($this->isSupervisor) { |
179
|
3 |
|
$styleArray = $this->getStyleArray(['borderStyle' => $pValue]); |
180
|
3 |
|
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
181
|
|
|
} else { |
182
|
42 |
|
$this->borderStyle = $pValue; |
|
|
|
|
183
|
|
|
} |
184
|
|
|
|
185
|
42 |
|
return $this; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Get Border Color. |
190
|
|
|
* |
191
|
|
|
* @return Color |
192
|
|
|
*/ |
193
|
75 |
|
public function getColor() |
194
|
|
|
{ |
195
|
75 |
|
return $this->color; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Set Border Color. |
200
|
|
|
* |
201
|
|
|
* @param Color $pValue |
202
|
|
|
* |
203
|
|
|
* @throws PhpSpreadsheetException |
204
|
|
|
* |
205
|
|
|
* @return Border |
206
|
|
|
*/ |
207
|
1 |
|
public function setColor(Color $pValue) |
208
|
|
|
{ |
209
|
|
|
// make sure parameter is a real color and not a supervisor |
210
|
1 |
|
$color = $pValue->getIsSupervisor() ? $pValue->getSharedComponent() : $pValue; |
211
|
|
|
|
212
|
1 |
|
if ($this->isSupervisor) { |
213
|
1 |
|
$styleArray = $this->getColor()->getStyleArray(['argb' => $color->getARGB()]); |
214
|
1 |
|
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
215
|
|
|
} else { |
216
|
|
|
$this->color = $color; |
217
|
|
|
} |
218
|
|
|
|
219
|
1 |
|
return $this; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Get hash code. |
224
|
|
|
* |
225
|
|
|
* @return string Hash code |
226
|
|
|
*/ |
227
|
106 |
|
public function getHashCode() |
228
|
|
|
{ |
229
|
106 |
|
if ($this->isSupervisor) { |
230
|
|
|
return $this->getSharedComponent()->getHashCode(); |
231
|
|
|
} |
232
|
|
|
|
233
|
106 |
|
return md5( |
234
|
106 |
|
$this->borderStyle . |
235
|
106 |
|
$this->color->getHashCode() . |
236
|
106 |
|
__CLASS__ |
237
|
|
|
); |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.