| 1441 |
ariadna |
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 |
const BORDER_OMIT = 'omit'; // should be used only for Conditional
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
* Border style.
|
|
|
28 |
*/
|
|
|
29 |
protected string $borderStyle = self::BORDER_NONE;
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* Border color.
|
|
|
33 |
*/
|
|
|
34 |
protected Color $color;
|
|
|
35 |
|
|
|
36 |
public ?int $colorIndex = null;
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* Create a new Border.
|
|
|
40 |
*
|
|
|
41 |
* @param bool $isSupervisor Flag indicating if this is a supervisor or not
|
|
|
42 |
* Leave this value at default unless you understand exactly what
|
|
|
43 |
* its ramifications are
|
|
|
44 |
*/
|
|
|
45 |
public function __construct(bool $isSupervisor = false, bool $isConditional = false)
|
|
|
46 |
{
|
|
|
47 |
// Supervisor?
|
|
|
48 |
parent::__construct($isSupervisor);
|
|
|
49 |
|
|
|
50 |
// Initialise values
|
|
|
51 |
$this->color = new Color(Color::COLOR_BLACK, $isSupervisor);
|
|
|
52 |
|
|
|
53 |
// bind parent if we are a supervisor
|
|
|
54 |
if ($isSupervisor) {
|
|
|
55 |
$this->color->bindParent($this, 'color');
|
|
|
56 |
}
|
|
|
57 |
if ($isConditional) {
|
|
|
58 |
$this->borderStyle = self::BORDER_OMIT;
|
|
|
59 |
}
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
/**
|
|
|
63 |
* Get the shared style component for the currently active cell in currently active sheet.
|
|
|
64 |
* Only used for style supervisor.
|
|
|
65 |
*/
|
|
|
66 |
public function getSharedComponent(): self
|
|
|
67 |
{
|
|
|
68 |
/** @var Style $parent */
|
|
|
69 |
$parent = $this->parent;
|
|
|
70 |
|
|
|
71 |
/** @var Borders $sharedComponent */
|
|
|
72 |
$sharedComponent = $parent->getSharedComponent();
|
|
|
73 |
|
|
|
74 |
return match ($this->parentPropertyName) {
|
|
|
75 |
'bottom' => $sharedComponent->getBottom(),
|
|
|
76 |
'diagonal' => $sharedComponent->getDiagonal(),
|
|
|
77 |
'left' => $sharedComponent->getLeft(),
|
|
|
78 |
'right' => $sharedComponent->getRight(),
|
|
|
79 |
'top' => $sharedComponent->getTop(),
|
|
|
80 |
default => throw new PhpSpreadsheetException('Cannot get shared component for a pseudo-border.'),
|
|
|
81 |
};
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
/**
|
|
|
85 |
* Build style array from subcomponents.
|
|
|
86 |
*/
|
|
|
87 |
public function getStyleArray(array $array): array
|
|
|
88 |
{
|
|
|
89 |
/** @var Style $parent */
|
|
|
90 |
$parent = $this->parent;
|
|
|
91 |
|
|
|
92 |
return $parent->getStyleArray([$this->parentPropertyName => $array]);
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
/**
|
|
|
96 |
* Apply styles from array.
|
|
|
97 |
*
|
|
|
98 |
* <code>
|
|
|
99 |
* $spreadsheet->getActiveSheet()->getStyle('B2')->getBorders()->getTop()->applyFromArray(
|
|
|
100 |
* [
|
|
|
101 |
* 'borderStyle' => Border::BORDER_DASHDOT,
|
|
|
102 |
* 'color' => [
|
|
|
103 |
* 'rgb' => '808080'
|
|
|
104 |
* ]
|
|
|
105 |
* ]
|
|
|
106 |
* );
|
|
|
107 |
* </code>
|
|
|
108 |
*
|
|
|
109 |
* @param array $styleArray Array containing style information
|
|
|
110 |
*
|
|
|
111 |
* @return $this
|
|
|
112 |
*/
|
|
|
113 |
public function applyFromArray(array $styleArray): static
|
|
|
114 |
{
|
|
|
115 |
if ($this->isSupervisor) {
|
|
|
116 |
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($styleArray));
|
|
|
117 |
} else {
|
|
|
118 |
if (isset($styleArray['borderStyle'])) {
|
|
|
119 |
$this->setBorderStyle($styleArray['borderStyle']);
|
|
|
120 |
}
|
|
|
121 |
if (isset($styleArray['color'])) {
|
|
|
122 |
$this->getColor()->applyFromArray($styleArray['color']);
|
|
|
123 |
}
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
return $this;
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
/**
|
|
|
130 |
* Get Border style.
|
|
|
131 |
*/
|
|
|
132 |
public function getBorderStyle(): string
|
|
|
133 |
{
|
|
|
134 |
if ($this->isSupervisor) {
|
|
|
135 |
return $this->getSharedComponent()->getBorderStyle();
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
return $this->borderStyle;
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
/**
|
|
|
142 |
* Set Border style.
|
|
|
143 |
*
|
|
|
144 |
* @param bool|string $style When passing a boolean, FALSE equates Border::BORDER_NONE
|
|
|
145 |
* and TRUE to Border::BORDER_MEDIUM
|
|
|
146 |
*
|
|
|
147 |
* @return $this
|
|
|
148 |
*/
|
|
|
149 |
public function setBorderStyle(bool|string $style): static
|
|
|
150 |
{
|
|
|
151 |
if (empty($style)) {
|
|
|
152 |
$style = self::BORDER_NONE;
|
|
|
153 |
} elseif (is_bool($style)) {
|
|
|
154 |
$style = self::BORDER_MEDIUM;
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
if ($this->isSupervisor) {
|
|
|
158 |
$styleArray = $this->getStyleArray(['borderStyle' => $style]);
|
|
|
159 |
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
|
|
|
160 |
} else {
|
|
|
161 |
$this->borderStyle = $style;
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
return $this;
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
/**
|
|
|
168 |
* Get Border Color.
|
|
|
169 |
*/
|
|
|
170 |
public function getColor(): Color
|
|
|
171 |
{
|
|
|
172 |
return $this->color;
|
|
|
173 |
}
|
|
|
174 |
|
|
|
175 |
/**
|
|
|
176 |
* Set Border Color.
|
|
|
177 |
*
|
|
|
178 |
* @return $this
|
|
|
179 |
*/
|
|
|
180 |
public function setColor(Color $color): static
|
|
|
181 |
{
|
|
|
182 |
// make sure parameter is a real color and not a supervisor
|
|
|
183 |
$color = $color->getIsSupervisor() ? $color->getSharedComponent() : $color;
|
|
|
184 |
|
|
|
185 |
if ($this->isSupervisor) {
|
|
|
186 |
$styleArray = $this->getColor()->getStyleArray(['argb' => $color->getARGB()]);
|
|
|
187 |
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
|
|
|
188 |
} else {
|
|
|
189 |
$this->color = $color;
|
|
|
190 |
}
|
|
|
191 |
|
|
|
192 |
return $this;
|
|
|
193 |
}
|
|
|
194 |
|
|
|
195 |
/**
|
|
|
196 |
* Get hash code.
|
|
|
197 |
*
|
|
|
198 |
* @return string Hash code
|
|
|
199 |
*/
|
|
|
200 |
public function getHashCode(): string
|
|
|
201 |
{
|
|
|
202 |
if ($this->isSupervisor) {
|
|
|
203 |
return $this->getSharedComponent()->getHashCode();
|
|
|
204 |
}
|
|
|
205 |
|
|
|
206 |
return md5(
|
|
|
207 |
$this->borderStyle
|
|
|
208 |
. $this->color->getHashCode()
|
|
|
209 |
. __CLASS__
|
|
|
210 |
);
|
|
|
211 |
}
|
|
|
212 |
|
|
|
213 |
protected function exportArray1(): array
|
|
|
214 |
{
|
|
|
215 |
$exportedArray = [];
|
|
|
216 |
$this->exportArray2($exportedArray, 'borderStyle', $this->getBorderStyle());
|
|
|
217 |
$this->exportArray2($exportedArray, 'color', $this->getColor());
|
|
|
218 |
|
|
|
219 |
return $exportedArray;
|
|
|
220 |
}
|
|
|
221 |
}
|