| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Style; |
| 4 |
|
| 5 |
use TablePress\PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException; |
| 6 |
|
| 7 |
class Borders extends Supervisor |
| 8 |
{ |
| 9 |
// Diagonal directions |
| 10 |
const DIAGONAL_NONE = 0; |
| 11 |
const DIAGONAL_UP = 1; |
| 12 |
const DIAGONAL_DOWN = 2; |
| 13 |
const DIAGONAL_BOTH = 3; |
| 14 |
|
| 15 |
/** |
| 16 |
* Left. |
| 17 |
*/ |
| 18 |
protected Border $left; |
| 19 |
|
| 20 |
/** |
| 21 |
* Right. |
| 22 |
*/ |
| 23 |
protected Border $right; |
| 24 |
|
| 25 |
/** |
| 26 |
* Top. |
| 27 |
*/ |
| 28 |
protected Border $top; |
| 29 |
|
| 30 |
/** |
| 31 |
* Bottom. |
| 32 |
*/ |
| 33 |
protected Border $bottom; |
| 34 |
|
| 35 |
/** |
| 36 |
* Diagonal. |
| 37 |
*/ |
| 38 |
protected Border $diagonal; |
| 39 |
|
| 40 |
/** |
| 41 |
* DiagonalDirection. |
| 42 |
*/ |
| 43 |
protected int $diagonalDirection; |
| 44 |
|
| 45 |
/** |
| 46 |
* All borders pseudo-border. Only applies to supervisor. |
| 47 |
*/ |
| 48 |
protected Border $allBorders; |
| 49 |
|
| 50 |
/** |
| 51 |
* Outline pseudo-border. Only applies to supervisor. |
| 52 |
*/ |
| 53 |
protected Border $outline; |
| 54 |
|
| 55 |
/** |
| 56 |
* Inside pseudo-border. Only applies to supervisor. |
| 57 |
*/ |
| 58 |
protected Border $inside; |
| 59 |
|
| 60 |
/** |
| 61 |
* Vertical pseudo-border. Only applies to supervisor. |
| 62 |
*/ |
| 63 |
protected Border $vertical; |
| 64 |
|
| 65 |
/** |
| 66 |
* Horizontal pseudo-border. Only applies to supervisor. |
| 67 |
*/ |
| 68 |
protected Border $horizontal; |
| 69 |
|
| 70 |
/** |
| 71 |
* Create a new Borders. |
| 72 |
* |
| 73 |
* @param bool $isSupervisor Flag indicating if this is a supervisor or not |
| 74 |
* Leave this value at default unless you understand exactly what |
| 75 |
* its ramifications are |
| 76 |
*/ |
| 77 |
public function __construct(bool $isSupervisor = false, bool $isConditional = false) |
| 78 |
{ |
| 79 |
// Supervisor? |
| 80 |
parent::__construct($isSupervisor); |
| 81 |
|
| 82 |
// Initialise values |
| 83 |
$this->left = new Border($isSupervisor, $isConditional); |
| 84 |
$this->right = new Border($isSupervisor, $isConditional); |
| 85 |
$this->top = new Border($isSupervisor, $isConditional); |
| 86 |
$this->bottom = new Border($isSupervisor, $isConditional); |
| 87 |
$this->diagonal = new Border($isSupervisor, $isConditional); |
| 88 |
$this->diagonalDirection = self::DIAGONAL_NONE; |
| 89 |
|
| 90 |
// Specially for supervisor |
| 91 |
if ($isSupervisor) { |
| 92 |
// Initialize pseudo-borders |
| 93 |
$this->allBorders = new Border(true, $isConditional); |
| 94 |
$this->outline = new Border(true, $isConditional); |
| 95 |
$this->inside = new Border(true, $isConditional); |
| 96 |
$this->vertical = new Border(true, $isConditional); |
| 97 |
$this->horizontal = new Border(true, $isConditional); |
| 98 |
|
| 99 |
// bind parent if we are a supervisor |
| 100 |
$this->left->bindParent($this, 'left'); |
| 101 |
$this->right->bindParent($this, 'right'); |
| 102 |
$this->top->bindParent($this, 'top'); |
| 103 |
$this->bottom->bindParent($this, 'bottom'); |
| 104 |
$this->diagonal->bindParent($this, 'diagonal'); |
| 105 |
$this->allBorders->bindParent($this, 'allBorders'); |
| 106 |
$this->outline->bindParent($this, 'outline'); |
| 107 |
$this->inside->bindParent($this, 'inside'); |
| 108 |
$this->vertical->bindParent($this, 'vertical'); |
| 109 |
$this->horizontal->bindParent($this, 'horizontal'); |
| 110 |
} |
| 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 |
public function getSharedComponent(): self |
| 118 |
{ |
| 119 |
/** @var Style $parent */ |
| 120 |
$parent = $this->parent; |
| 121 |
|
| 122 |
return $parent->getSharedComponent()->getBorders(); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Build style array from subcomponents. |
| 127 |
* |
| 128 |
* @param mixed[] $array |
| 129 |
* |
| 130 |
* @return array{borders: mixed[]} |
| 131 |
*/ |
| 132 |
public function getStyleArray(array $array): array |
| 133 |
{ |
| 134 |
return ['borders' => $array]; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Apply styles from array. |
| 139 |
* |
| 140 |
* <code> |
| 141 |
* $spreadsheet->getActiveSheet()->getStyle('B2')->getBorders()->applyFromArray( |
| 142 |
* [ |
| 143 |
* 'bottom' => [ |
| 144 |
* 'borderStyle' => Border::BORDER_DASHDOT, |
| 145 |
* 'color' => [ |
| 146 |
* 'rgb' => '808080' |
| 147 |
* ] |
| 148 |
* ], |
| 149 |
* 'top' => [ |
| 150 |
* 'borderStyle' => Border::BORDER_DASHDOT, |
| 151 |
* 'color' => [ |
| 152 |
* 'rgb' => '808080' |
| 153 |
* ] |
| 154 |
* ] |
| 155 |
* ] |
| 156 |
* ); |
| 157 |
* </code> |
| 158 |
* |
| 159 |
* <code> |
| 160 |
* $spreadsheet->getActiveSheet()->getStyle('B2')->getBorders()->applyFromArray( |
| 161 |
* [ |
| 162 |
* 'allBorders' => [ |
| 163 |
* 'borderStyle' => Border::BORDER_DASHDOT, |
| 164 |
* 'color' => [ |
| 165 |
* 'rgb' => '808080' |
| 166 |
* ] |
| 167 |
* ] |
| 168 |
* ] |
| 169 |
* ); |
| 170 |
* </code> |
| 171 |
* |
| 172 |
* @param mixed[] $styleArray Array containing style information |
| 173 |
* |
| 174 |
* @return $this |
| 175 |
*/ |
| 176 |
public function applyFromArray(array $styleArray) |
| 177 |
{ |
| 178 |
if ($this->isSupervisor) { |
| 179 |
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($styleArray)); |
| 180 |
} else { |
| 181 |
/** @var array{left?: float[], right?: float[], top?: float[], bottom?: float[], diagonal?: mixed[], diagonalDirection?: int, allBorders?: mixed[][]} $styleArray */ |
| 182 |
if (isset($styleArray['left'])) { |
| 183 |
$this->getLeft()->applyFromArray($styleArray['left']); |
| 184 |
} |
| 185 |
if (isset($styleArray['right'])) { |
| 186 |
$this->getRight()->applyFromArray($styleArray['right']); |
| 187 |
} |
| 188 |
if (isset($styleArray['top'])) { |
| 189 |
$this->getTop()->applyFromArray($styleArray['top']); |
| 190 |
} |
| 191 |
if (isset($styleArray['bottom'])) { |
| 192 |
$this->getBottom()->applyFromArray($styleArray['bottom']); |
| 193 |
} |
| 194 |
if (isset($styleArray['diagonal'])) { |
| 195 |
$this->getDiagonal()->applyFromArray($styleArray['diagonal']); |
| 196 |
} |
| 197 |
if (isset($styleArray['diagonalDirection'])) { |
| 198 |
$this->setDiagonalDirection($styleArray['diagonalDirection']); |
| 199 |
} |
| 200 |
if (isset($styleArray['allBorders'])) { |
| 201 |
$this->getLeft()->applyFromArray($styleArray['allBorders']); |
| 202 |
$this->getRight()->applyFromArray($styleArray['allBorders']); |
| 203 |
$this->getTop()->applyFromArray($styleArray['allBorders']); |
| 204 |
$this->getBottom()->applyFromArray($styleArray['allBorders']); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
return $this; |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Get Left. |
| 213 |
*/ |
| 214 |
public function getLeft(): Border |
| 215 |
{ |
| 216 |
return $this->left; |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Get Right. |
| 221 |
*/ |
| 222 |
public function getRight(): Border |
| 223 |
{ |
| 224 |
return $this->right; |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* Get Top. |
| 229 |
*/ |
| 230 |
public function getTop(): Border |
| 231 |
{ |
| 232 |
return $this->top; |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Get Bottom. |
| 237 |
*/ |
| 238 |
public function getBottom(): Border |
| 239 |
{ |
| 240 |
return $this->bottom; |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* Get Diagonal. |
| 245 |
*/ |
| 246 |
public function getDiagonal(): Border |
| 247 |
{ |
| 248 |
return $this->diagonal; |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Get AllBorders (pseudo-border). Only applies to supervisor. |
| 253 |
*/ |
| 254 |
public function getAllBorders(): Border |
| 255 |
{ |
| 256 |
if (!$this->isSupervisor) { |
| 257 |
throw new PhpSpreadsheetException('Can only get pseudo-border for supervisor.'); |
| 258 |
} |
| 259 |
|
| 260 |
return $this->allBorders; |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* Get Outline (pseudo-border). Only applies to supervisor. |
| 265 |
*/ |
| 266 |
public function getOutline(): Border |
| 267 |
{ |
| 268 |
if (!$this->isSupervisor) { |
| 269 |
throw new PhpSpreadsheetException('Can only get pseudo-border for supervisor.'); |
| 270 |
} |
| 271 |
|
| 272 |
return $this->outline; |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* Get Inside (pseudo-border). Only applies to supervisor. |
| 277 |
*/ |
| 278 |
public function getInside(): Border |
| 279 |
{ |
| 280 |
if (!$this->isSupervisor) { |
| 281 |
throw new PhpSpreadsheetException('Can only get pseudo-border for supervisor.'); |
| 282 |
} |
| 283 |
|
| 284 |
return $this->inside; |
| 285 |
} |
| 286 |
|
| 287 |
/** |
| 288 |
* Get Vertical (pseudo-border). Only applies to supervisor. |
| 289 |
*/ |
| 290 |
public function getVertical(): Border |
| 291 |
{ |
| 292 |
if (!$this->isSupervisor) { |
| 293 |
throw new PhpSpreadsheetException('Can only get pseudo-border for supervisor.'); |
| 294 |
} |
| 295 |
|
| 296 |
return $this->vertical; |
| 297 |
} |
| 298 |
|
| 299 |
/** |
| 300 |
* Get Horizontal (pseudo-border). Only applies to supervisor. |
| 301 |
*/ |
| 302 |
public function getHorizontal(): Border |
| 303 |
{ |
| 304 |
if (!$this->isSupervisor) { |
| 305 |
throw new PhpSpreadsheetException('Can only get pseudo-border for supervisor.'); |
| 306 |
} |
| 307 |
|
| 308 |
return $this->horizontal; |
| 309 |
} |
| 310 |
|
| 311 |
/** |
| 312 |
* Get DiagonalDirection. |
| 313 |
*/ |
| 314 |
public function getDiagonalDirection(): int |
| 315 |
{ |
| 316 |
if ($this->isSupervisor) { |
| 317 |
return $this->getSharedComponent()->getDiagonalDirection(); |
| 318 |
} |
| 319 |
|
| 320 |
return $this->diagonalDirection; |
| 321 |
} |
| 322 |
|
| 323 |
/** |
| 324 |
* Set DiagonalDirection. |
| 325 |
* |
| 326 |
* @param int $direction see self::DIAGONAL_* |
| 327 |
* |
| 328 |
* @return $this |
| 329 |
*/ |
| 330 |
public function setDiagonalDirection(int $direction) |
| 331 |
{ |
| 332 |
if ($this->isSupervisor) { |
| 333 |
$styleArray = $this->getStyleArray(['diagonalDirection' => $direction]); |
| 334 |
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
| 335 |
} else { |
| 336 |
$this->diagonalDirection = $direction; |
| 337 |
} |
| 338 |
|
| 339 |
return $this; |
| 340 |
} |
| 341 |
|
| 342 |
/** |
| 343 |
* Get hash code. |
| 344 |
* |
| 345 |
* @return string Hash code |
| 346 |
*/ |
| 347 |
public function getHashCode(): string |
| 348 |
{ |
| 349 |
if ($this->isSupervisor) { |
| 350 |
return $this->getSharedComponent()->getHashcode(); |
| 351 |
} |
| 352 |
|
| 353 |
return md5( |
| 354 |
$this->getLeft()->getHashCode() |
| 355 |
. $this->getRight()->getHashCode() |
| 356 |
. $this->getTop()->getHashCode() |
| 357 |
. $this->getBottom()->getHashCode() |
| 358 |
. $this->getDiagonal()->getHashCode() |
| 359 |
. $this->getDiagonalDirection() |
| 360 |
. __CLASS__ |
| 361 |
); |
| 362 |
} |
| 363 |
|
| 364 |
/** @return mixed[][] */ |
| 365 |
protected function exportArray1(): array |
| 366 |
{ |
| 367 |
$exportedArray = []; |
| 368 |
$this->exportArray2($exportedArray, 'bottom', $this->getBottom()); |
| 369 |
$this->exportArray2($exportedArray, 'diagonal', $this->getDiagonal()); |
| 370 |
$this->exportArray2($exportedArray, 'diagonalDirection', $this->getDiagonalDirection()); |
| 371 |
$this->exportArray2($exportedArray, 'left', $this->getLeft()); |
| 372 |
$this->exportArray2($exportedArray, 'right', $this->getRight()); |
| 373 |
$this->exportArray2($exportedArray, 'top', $this->getTop()); |
| 374 |
/** @var mixed[][] $exportedArray */ |
| 375 |
|
| 376 |
return $exportedArray; |
| 377 |
} |
| 378 |
} |
| 379 |
|