| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Style; |
| 4 |
|
| 5 |
class Fill extends Supervisor |
| 6 |
{ |
| 7 |
// Fill types |
| 8 |
const FILL_NONE = 'none'; |
| 9 |
const FILL_SOLID = 'solid'; |
| 10 |
const FILL_GRADIENT_LINEAR = 'linear'; |
| 11 |
const FILL_GRADIENT_PATH = 'path'; |
| 12 |
const FILL_PATTERN_DARKDOWN = 'darkDown'; |
| 13 |
const FILL_PATTERN_DARKGRAY = 'darkGray'; |
| 14 |
const FILL_PATTERN_DARKGRID = 'darkGrid'; |
| 15 |
const FILL_PATTERN_DARKHORIZONTAL = 'darkHorizontal'; |
| 16 |
const FILL_PATTERN_DARKTRELLIS = 'darkTrellis'; |
| 17 |
const FILL_PATTERN_DARKUP = 'darkUp'; |
| 18 |
const FILL_PATTERN_DARKVERTICAL = 'darkVertical'; |
| 19 |
const FILL_PATTERN_GRAY0625 = 'gray0625'; |
| 20 |
const FILL_PATTERN_GRAY125 = 'gray125'; |
| 21 |
const FILL_PATTERN_LIGHTDOWN = 'lightDown'; |
| 22 |
const FILL_PATTERN_LIGHTGRAY = 'lightGray'; |
| 23 |
const FILL_PATTERN_LIGHTGRID = 'lightGrid'; |
| 24 |
const FILL_PATTERN_LIGHTHORIZONTAL = 'lightHorizontal'; |
| 25 |
const FILL_PATTERN_LIGHTTRELLIS = 'lightTrellis'; |
| 26 |
const FILL_PATTERN_LIGHTUP = 'lightUp'; |
| 27 |
const FILL_PATTERN_LIGHTVERTICAL = 'lightVertical'; |
| 28 |
const FILL_PATTERN_MEDIUMGRAY = 'mediumGray'; |
| 29 |
|
| 30 |
public ?int $startcolorIndex = null; |
| 31 |
|
| 32 |
public ?int $endcolorIndex = null; |
| 33 |
|
| 34 |
/** |
| 35 |
* Fill type. |
| 36 |
*/ |
| 37 |
protected ?string $fillType = self::FILL_NONE; |
| 38 |
|
| 39 |
/** |
| 40 |
* Rotation. |
| 41 |
*/ |
| 42 |
protected float $rotation = 0.0; |
| 43 |
|
| 44 |
/** |
| 45 |
* Start color. |
| 46 |
*/ |
| 47 |
protected Color $startColor; |
| 48 |
|
| 49 |
/** |
| 50 |
* End color. |
| 51 |
*/ |
| 52 |
protected Color $endColor; |
| 53 |
|
| 54 |
private bool $colorChanged = false; |
| 55 |
|
| 56 |
/** |
| 57 |
* Create a new Fill. |
| 58 |
* |
| 59 |
* @param bool $isSupervisor Flag indicating if this is a supervisor or not |
| 60 |
* Leave this value at default unless you understand exactly what |
| 61 |
* its ramifications are |
| 62 |
* @param bool $isConditional Flag indicating if this is a conditional style or not |
| 63 |
* Leave this value at default unless you understand exactly what |
| 64 |
* its ramifications are |
| 65 |
*/ |
| 66 |
public function __construct(bool $isSupervisor = false, bool $isConditional = false) |
| 67 |
{ |
| 68 |
// Supervisor? |
| 69 |
parent::__construct($isSupervisor); |
| 70 |
|
| 71 |
// Initialise values |
| 72 |
if ($isConditional) { |
| 73 |
$this->fillType = null; |
| 74 |
} |
| 75 |
$this->startColor = new Color(Color::COLOR_WHITE, $isSupervisor, $isConditional); |
| 76 |
$this->endColor = new Color(Color::COLOR_BLACK, $isSupervisor, $isConditional); |
| 77 |
|
| 78 |
// bind parent if we are a supervisor |
| 79 |
if ($isSupervisor) { |
| 80 |
$this->startColor->bindParent($this, 'startColor'); |
| 81 |
$this->endColor->bindParent($this, 'endColor'); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Get the shared style component for the currently active cell in currently active sheet. |
| 87 |
* Only used for style supervisor. |
| 88 |
*/ |
| 89 |
public function getSharedComponent(): self |
| 90 |
{ |
| 91 |
/** @var Style $parent */ |
| 92 |
$parent = $this->parent; |
| 93 |
|
| 94 |
return $parent->getSharedComponent()->getFill(); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Build style array from subcomponents. |
| 99 |
* |
| 100 |
* @param mixed[] $array |
| 101 |
* |
| 102 |
* @return array{fill: mixed[]} |
| 103 |
*/ |
| 104 |
public function getStyleArray(array $array): array |
| 105 |
{ |
| 106 |
return ['fill' => $array]; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Apply styles from array. |
| 111 |
* |
| 112 |
* <code> |
| 113 |
* $spreadsheet->getActiveSheet()->getStyle('B2')->getFill()->applyFromArray( |
| 114 |
* [ |
| 115 |
* 'fillType' => Fill::FILL_GRADIENT_LINEAR, |
| 116 |
* 'rotation' => 0.0, |
| 117 |
* 'startColor' => [ |
| 118 |
* 'rgb' => '000000' |
| 119 |
* ], |
| 120 |
* 'endColor' => [ |
| 121 |
* 'argb' => 'FFFFFFFF' |
| 122 |
* ] |
| 123 |
* ] |
| 124 |
* ); |
| 125 |
* </code> |
| 126 |
* |
| 127 |
* @param array{fillType?: string, rotation?: float, startColor?: array{rgb?: string, argb?: string, theme?: int}, endColor?: array{rgb?: string, argb?: string, theme?: int}, color?: array{rgb?: string, argb?: string, theme?: int}} $styleArray Array containing style information |
| 128 |
* |
| 129 |
* @return $this |
| 130 |
*/ |
| 131 |
public function applyFromArray(array $styleArray) |
| 132 |
{ |
| 133 |
if ($this->isSupervisor) { |
| 134 |
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($styleArray)); |
| 135 |
} else { |
| 136 |
if (isset($styleArray['fillType'])) { |
| 137 |
$this->setFillType($styleArray['fillType']); |
| 138 |
} |
| 139 |
if (isset($styleArray['rotation'])) { |
| 140 |
$this->setRotation($styleArray['rotation']); |
| 141 |
} |
| 142 |
if (isset($styleArray['startColor'])) { |
| 143 |
$this->getStartColor() |
| 144 |
->applyFromArray($styleArray['startColor']); |
| 145 |
} |
| 146 |
if (isset($styleArray['endColor'])) { |
| 147 |
$this->getEndColor() |
| 148 |
->applyFromArray($styleArray['endColor']); |
| 149 |
} |
| 150 |
if (isset($styleArray['color'])) { |
| 151 |
$this->getStartColor() |
| 152 |
->applyFromArray($styleArray['color']); |
| 153 |
$this->getEndColor() |
| 154 |
->applyFromArray($styleArray['color']); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
return $this; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Get Fill Type. |
| 163 |
*/ |
| 164 |
public function getFillType(): ?string |
| 165 |
{ |
| 166 |
if ($this->isSupervisor) { |
| 167 |
return $this->getSharedComponent()->getFillType(); |
| 168 |
} |
| 169 |
|
| 170 |
return $this->fillType; |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Set Fill Type. |
| 175 |
* |
| 176 |
* @param string $fillType Fill type, see self::FILL_* |
| 177 |
* |
| 178 |
* @return $this |
| 179 |
*/ |
| 180 |
public function setFillType(string $fillType) |
| 181 |
{ |
| 182 |
if ($this->isSupervisor) { |
| 183 |
$styleArray = $this->getStyleArray(['fillType' => $fillType]); |
| 184 |
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
| 185 |
} else { |
| 186 |
$this->fillType = $fillType; |
| 187 |
} |
| 188 |
|
| 189 |
return $this; |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Get Rotation. |
| 194 |
*/ |
| 195 |
public function getRotation(): float |
| 196 |
{ |
| 197 |
if ($this->isSupervisor) { |
| 198 |
return $this->getSharedComponent()->getRotation(); |
| 199 |
} |
| 200 |
|
| 201 |
return $this->rotation; |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Set Rotation. |
| 206 |
* |
| 207 |
* @return $this |
| 208 |
*/ |
| 209 |
public function setRotation(float $angleInDegrees) |
| 210 |
{ |
| 211 |
if ($this->isSupervisor) { |
| 212 |
$styleArray = $this->getStyleArray(['rotation' => $angleInDegrees]); |
| 213 |
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
| 214 |
} else { |
| 215 |
$this->rotation = $angleInDegrees; |
| 216 |
} |
| 217 |
|
| 218 |
return $this; |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Get Start Color. |
| 223 |
*/ |
| 224 |
public function getStartColor(): Color |
| 225 |
{ |
| 226 |
return $this->startColor; |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Set Start Color. |
| 231 |
* |
| 232 |
* @return $this |
| 233 |
*/ |
| 234 |
public function setStartColor(Color $color) |
| 235 |
{ |
| 236 |
$this->colorChanged = true; |
| 237 |
// make sure parameter is a real color and not a supervisor |
| 238 |
$color = $color->getIsSupervisor() ? $color->getSharedComponent() : $color; |
| 239 |
|
| 240 |
if ($this->isSupervisor) { |
| 241 |
$styleArray = $this->getStartColor()->getStyleArray(['argb' => $color->getARGB()]); |
| 242 |
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
| 243 |
} else { |
| 244 |
$this->startColor = $color; |
| 245 |
} |
| 246 |
|
| 247 |
return $this; |
| 248 |
} |
| 249 |
|
| 250 |
/** |
| 251 |
* Get End Color. |
| 252 |
*/ |
| 253 |
public function getEndColor(): Color |
| 254 |
{ |
| 255 |
return $this->endColor; |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* Set End Color. |
| 260 |
* |
| 261 |
* @return $this |
| 262 |
*/ |
| 263 |
public function setEndColor(Color $color) |
| 264 |
{ |
| 265 |
$this->colorChanged = true; |
| 266 |
// make sure parameter is a real color and not a supervisor |
| 267 |
$color = $color->getIsSupervisor() ? $color->getSharedComponent() : $color; |
| 268 |
|
| 269 |
if ($this->isSupervisor) { |
| 270 |
$styleArray = $this->getEndColor()->getStyleArray(['argb' => $color->getARGB()]); |
| 271 |
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
| 272 |
} else { |
| 273 |
$this->endColor = $color; |
| 274 |
} |
| 275 |
|
| 276 |
return $this; |
| 277 |
} |
| 278 |
|
| 279 |
public function getColorsChanged(): bool |
| 280 |
{ |
| 281 |
if ($this->isSupervisor) { |
| 282 |
$changed = $this->getSharedComponent()->colorChanged; |
| 283 |
} else { |
| 284 |
$changed = $this->colorChanged; |
| 285 |
} |
| 286 |
|
| 287 |
return $changed || $this->startColor->getHasChanged() || $this->endColor->getHasChanged(); |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* Get hash code. |
| 292 |
* |
| 293 |
* @return string Hash code |
| 294 |
*/ |
| 295 |
public function getHashCode(): string |
| 296 |
{ |
| 297 |
if ($this->isSupervisor) { |
| 298 |
return $this->getSharedComponent()->getHashCode(); |
| 299 |
} |
| 300 |
|
| 301 |
// Note that we don't care about colours for fill type NONE, but could have duplicate NONEs with |
| 302 |
// different hashes if we don't explicitly prevent this |
| 303 |
return md5( |
| 304 |
$this->getFillType() |
| 305 |
. $this->getRotation() |
| 306 |
. ($this->getFillType() !== self::FILL_NONE ? $this->getStartColor()->getHashCode() : '') |
| 307 |
. ($this->getFillType() !== self::FILL_NONE ? $this->getEndColor()->getHashCode() : '') |
| 308 |
. ((string) $this->getColorsChanged()) |
| 309 |
. __CLASS__ |
| 310 |
); |
| 311 |
} |
| 312 |
|
| 313 |
/** @return mixed[] */ |
| 314 |
protected function exportArray1(): array |
| 315 |
{ |
| 316 |
$exportedArray = []; |
| 317 |
$this->exportArray2($exportedArray, 'fillType', $this->getFillType()); |
| 318 |
$this->exportArray2($exportedArray, 'rotation', $this->getRotation()); |
| 319 |
if ($this->getColorsChanged()) { |
| 320 |
$this->exportArray2($exportedArray, 'endColor', $this->getEndColor()); |
| 321 |
$this->exportArray2($exportedArray, 'startColor', $this->getStartColor()); |
| 322 |
} |
| 323 |
|
| 324 |
return $exportedArray; |
| 325 |
} |
| 326 |
} |
| 327 |
|