| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Style; |
| 4 |
|
| 5 |
use TablePress\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 |
switch ($this->parentPropertyName) { |
| 75 |
case 'bottom': |
| 76 |
return $sharedComponent->getBottom(); |
| 77 |
case 'diagonal': |
| 78 |
return $sharedComponent->getDiagonal(); |
| 79 |
case 'left': |
| 80 |
return $sharedComponent->getLeft(); |
| 81 |
case 'right': |
| 82 |
return $sharedComponent->getRight(); |
| 83 |
case 'top': |
| 84 |
return $sharedComponent->getTop(); |
| 85 |
default: |
| 86 |
throw new PhpSpreadsheetException('Cannot get shared component for a pseudo-border.'); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Build style array from subcomponents. |
| 92 |
* |
| 93 |
* @param mixed[] $array |
| 94 |
* |
| 95 |
* @return mixed[] |
| 96 |
*/ |
| 97 |
public function getStyleArray(array $array): array |
| 98 |
{ |
| 99 |
/** @var Style $parent */ |
| 100 |
$parent = $this->parent; |
| 101 |
|
| 102 |
return $parent->getStyleArray([$this->parentPropertyName => $array]); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Apply styles from array. |
| 107 |
* |
| 108 |
* <code> |
| 109 |
* $spreadsheet->getActiveSheet()->getStyle('B2')->getBorders()->getTop()->applyFromArray( |
| 110 |
* [ |
| 111 |
* 'borderStyle' => Border::BORDER_DASHDOT, |
| 112 |
* 'color' => [ |
| 113 |
* 'rgb' => '808080' |
| 114 |
* ] |
| 115 |
* ] |
| 116 |
* ); |
| 117 |
* </code> |
| 118 |
* |
| 119 |
* @param mixed[] $styleArray Array containing style information |
| 120 |
* |
| 121 |
* @return $this |
| 122 |
*/ |
| 123 |
public function applyFromArray(array $styleArray) |
| 124 |
{ |
| 125 |
if ($this->isSupervisor) { |
| 126 |
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($styleArray)); |
| 127 |
} else { |
| 128 |
/** @var array{borderStyle?: string, color?: array{rgb?: string, argb?: string}} $styleArray */ |
| 129 |
if (isset($styleArray['borderStyle'])) { |
| 130 |
$this->setBorderStyle($styleArray['borderStyle']); |
| 131 |
} |
| 132 |
if (isset($styleArray['color'])) { |
| 133 |
$this->getColor() |
| 134 |
->applyFromArray($styleArray['color']); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
return $this; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Get Border style. |
| 143 |
*/ |
| 144 |
public function getBorderStyle(): string |
| 145 |
{ |
| 146 |
if ($this->isSupervisor) { |
| 147 |
return $this->getSharedComponent()->getBorderStyle(); |
| 148 |
} |
| 149 |
|
| 150 |
return $this->borderStyle; |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Set Border style. |
| 155 |
* |
| 156 |
* @param bool|string $style When passing a boolean, FALSE equates Border::BORDER_NONE |
| 157 |
* and TRUE to Border::BORDER_MEDIUM |
| 158 |
* |
| 159 |
* @return $this |
| 160 |
*/ |
| 161 |
public function setBorderStyle($style) |
| 162 |
{ |
| 163 |
if (empty($style)) { |
| 164 |
$style = self::BORDER_NONE; |
| 165 |
} elseif (is_bool($style)) { |
| 166 |
$style = self::BORDER_MEDIUM; |
| 167 |
} |
| 168 |
|
| 169 |
if ($this->isSupervisor) { |
| 170 |
$styleArray = $this->getStyleArray(['borderStyle' => $style]); |
| 171 |
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
| 172 |
} else { |
| 173 |
$this->borderStyle = $style; |
| 174 |
} |
| 175 |
|
| 176 |
return $this; |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Get Border Color. |
| 181 |
*/ |
| 182 |
public function getColor(): Color |
| 183 |
{ |
| 184 |
return $this->color; |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Set Border Color. |
| 189 |
* |
| 190 |
* @return $this |
| 191 |
*/ |
| 192 |
public function setColor(Color $color) |
| 193 |
{ |
| 194 |
// make sure parameter is a real color and not a supervisor |
| 195 |
$color = $color->getIsSupervisor() ? $color->getSharedComponent() : $color; |
| 196 |
|
| 197 |
if ($this->isSupervisor) { |
| 198 |
$styleArray = $this->getColor()->getStyleArray(['argb' => $color->getARGB()]); |
| 199 |
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
| 200 |
} else { |
| 201 |
$this->color = $color; |
| 202 |
} |
| 203 |
|
| 204 |
return $this; |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Get hash code. |
| 209 |
* |
| 210 |
* @return string Hash code |
| 211 |
*/ |
| 212 |
public function getHashCode(): string |
| 213 |
{ |
| 214 |
if ($this->isSupervisor) { |
| 215 |
return $this->getSharedComponent()->getHashCode(); |
| 216 |
} |
| 217 |
|
| 218 |
return md5( |
| 219 |
$this->borderStyle |
| 220 |
. $this->color->getHashCode() |
| 221 |
. __CLASS__ |
| 222 |
); |
| 223 |
} |
| 224 |
|
| 225 |
/** @return mixed[] */ |
| 226 |
protected function exportArray1(): array |
| 227 |
{ |
| 228 |
$exportedArray = []; |
| 229 |
$this->exportArray2($exportedArray, 'borderStyle', $this->getBorderStyle()); |
| 230 |
$this->exportArray2($exportedArray, 'color', $this->getColor()); |
| 231 |
|
| 232 |
return $exportedArray; |
| 233 |
} |
| 234 |
} |
| 235 |
|