| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Style; |
| 4 |
|
| 5 |
use TablePress\PhpOffice\PhpSpreadsheet\Chart\ChartColor; |
| 6 |
use TablePress\PhpOffice\PhpSpreadsheet\Theme; |
| 7 |
|
| 8 |
class Font extends Supervisor |
| 9 |
{ |
| 10 |
// Underline types |
| 11 |
const UNDERLINE_NONE = 'none'; |
| 12 |
const UNDERLINE_DOUBLE = 'double'; |
| 13 |
const UNDERLINE_DOUBLEACCOUNTING = 'doubleAccounting'; |
| 14 |
const UNDERLINE_SINGLE = 'single'; |
| 15 |
const UNDERLINE_SINGLEACCOUNTING = 'singleAccounting'; |
| 16 |
|
| 17 |
const CAP_ALL = 'all'; |
| 18 |
const CAP_SMALL = 'small'; |
| 19 |
const CAP_NONE = 'none'; |
| 20 |
private const VALID_CAPS = [self::CAP_ALL, self::CAP_SMALL, self::CAP_NONE]; |
| 21 |
|
| 22 |
protected ?string $cap = null; |
| 23 |
|
| 24 |
public const DEFAULT_FONT_NAME = 'Calibri'; |
| 25 |
|
| 26 |
/** |
| 27 |
* Font Name. |
| 28 |
*/ |
| 29 |
protected ?string $name = self::DEFAULT_FONT_NAME; |
| 30 |
|
| 31 |
/** |
| 32 |
* The following 7 are used only for chart titles, I think. |
| 33 |
*/ |
| 34 |
private string $latin = ''; |
| 35 |
|
| 36 |
private string $eastAsian = ''; |
| 37 |
|
| 38 |
private string $complexScript = ''; |
| 39 |
|
| 40 |
private int $baseLine = 0; |
| 41 |
|
| 42 |
private string $strikeType = ''; |
| 43 |
|
| 44 |
private ?ChartColor $underlineColor = null; |
| 45 |
|
| 46 |
private ?ChartColor $chartColor = null; |
| 47 |
// end of chart title items |
| 48 |
|
| 49 |
/** |
| 50 |
* Font Size. |
| 51 |
*/ |
| 52 |
protected ?float $size = 11; |
| 53 |
|
| 54 |
/** |
| 55 |
* Bold. |
| 56 |
*/ |
| 57 |
protected ?bool $bold = false; |
| 58 |
|
| 59 |
/** |
| 60 |
* Italic. |
| 61 |
*/ |
| 62 |
protected ?bool $italic = false; |
| 63 |
|
| 64 |
/** |
| 65 |
* Superscript. |
| 66 |
*/ |
| 67 |
protected ?bool $superscript = false; |
| 68 |
|
| 69 |
/** |
| 70 |
* Subscript. |
| 71 |
*/ |
| 72 |
protected ?bool $subscript = false; |
| 73 |
|
| 74 |
/** |
| 75 |
* Underline. |
| 76 |
*/ |
| 77 |
protected ?string $underline = self::UNDERLINE_NONE; |
| 78 |
|
| 79 |
/** |
| 80 |
* Strikethrough. |
| 81 |
*/ |
| 82 |
protected ?bool $strikethrough = false; |
| 83 |
|
| 84 |
/** |
| 85 |
* Foreground color. |
| 86 |
*/ |
| 87 |
protected Color $color; |
| 88 |
|
| 89 |
protected bool $autoColor = false; |
| 90 |
|
| 91 |
public ?int $colorIndex = null; |
| 92 |
|
| 93 |
protected string $scheme = ''; |
| 94 |
|
| 95 |
/** |
| 96 |
* Create a new Font. |
| 97 |
* |
| 98 |
* @param bool $isSupervisor Flag indicating if this is a supervisor or not |
| 99 |
* Leave this value at default unless you understand exactly what |
| 100 |
* its ramifications are |
| 101 |
* @param bool $isConditional Flag indicating if this is a conditional style or not |
| 102 |
* Leave this value at default unless you understand exactly what |
| 103 |
* its ramifications are |
| 104 |
*/ |
| 105 |
public function __construct(bool $isSupervisor = false, bool $isConditional = false) |
| 106 |
{ |
| 107 |
// Supervisor? |
| 108 |
parent::__construct($isSupervisor); |
| 109 |
|
| 110 |
// Initialise values |
| 111 |
if ($isConditional) { |
| 112 |
$this->name = null; |
| 113 |
$this->size = null; |
| 114 |
$this->bold = null; |
| 115 |
$this->italic = null; |
| 116 |
$this->superscript = null; |
| 117 |
$this->subscript = null; |
| 118 |
$this->underline = null; |
| 119 |
$this->strikethrough = null; |
| 120 |
$this->color = new Color(Color::COLOR_BLACK, $isSupervisor, $isConditional); |
| 121 |
} else { |
| 122 |
$this->color = new Color(Color::COLOR_BLACK, $isSupervisor); |
| 123 |
} |
| 124 |
// bind parent if we are a supervisor |
| 125 |
if ($isSupervisor) { |
| 126 |
$this->color->bindParent($this, 'color'); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
public function applyThemeFonts(Theme $theme): void |
| 131 |
{ |
| 132 |
$this->setName($theme->getMinorFontLatin()); |
| 133 |
$this->setLatin($theme->getMinorFontLatin()); |
| 134 |
$this->setEastAsian($theme->getMinorFontEastAsian()); |
| 135 |
$this->setComplexScript($theme->getMinorFontComplexScript()); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Get the shared style component for the currently active cell in currently active sheet. |
| 140 |
* Only used for style supervisor. |
| 141 |
*/ |
| 142 |
public function getSharedComponent(): self |
| 143 |
{ |
| 144 |
/** @var Style $parent */ |
| 145 |
$parent = $this->parent; |
| 146 |
|
| 147 |
return $parent->getSharedComponent()->getFont(); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Build style array from subcomponents. |
| 152 |
* |
| 153 |
* @param mixed[] $array |
| 154 |
* |
| 155 |
* @return array{font: mixed[]} |
| 156 |
*/ |
| 157 |
public function getStyleArray(array $array): array |
| 158 |
{ |
| 159 |
return ['font' => $array]; |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Apply styles from array. |
| 164 |
* |
| 165 |
* <code> |
| 166 |
* $spreadsheet->getActiveSheet()->getStyle('B2')->getFont()->applyFromArray( |
| 167 |
* [ |
| 168 |
* 'name' => 'Arial', |
| 169 |
* 'bold' => TRUE, |
| 170 |
* 'italic' => FALSE, |
| 171 |
* 'underline' => \TablePress\PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_DOUBLE, |
| 172 |
* 'strikethrough' => FALSE, |
| 173 |
* 'color' => [ |
| 174 |
* 'rgb' => '808080' |
| 175 |
* ] |
| 176 |
* ] |
| 177 |
* ); |
| 178 |
* </code> |
| 179 |
* |
| 180 |
* @param array{ |
| 181 |
* autoColor?: bool, |
| 182 |
* bold?: bool, |
| 183 |
* cap?: string, |
| 184 |
* chartColor?: ChartColor, |
| 185 |
* color?: string[], |
| 186 |
* complexScript?: string, |
| 187 |
* eastAsian?: string, |
| 188 |
* italic?: bool, |
| 189 |
* latin?: string, |
| 190 |
* name?: string, |
| 191 |
* scheme?: string, |
| 192 |
* size?: null|float|int, |
| 193 |
* strikethrough?: bool, |
| 194 |
* superscript?: bool, |
| 195 |
* subscript?: bool, |
| 196 |
* underline?: bool|string, |
| 197 |
* } $styleArray Array containing style information |
| 198 |
* |
| 199 |
* @return $this |
| 200 |
*/ |
| 201 |
public function applyFromArray(array $styleArray) |
| 202 |
{ |
| 203 |
if ($this->isSupervisor) { |
| 204 |
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($styleArray)); |
| 205 |
} else { |
| 206 |
if (isset($styleArray['name'])) { |
| 207 |
$this->setName($styleArray['name']); |
| 208 |
} |
| 209 |
if (isset($styleArray['latin'])) { |
| 210 |
$this->setLatin($styleArray['latin']); |
| 211 |
} |
| 212 |
if (isset($styleArray['eastAsian'])) { |
| 213 |
$this->setEastAsian($styleArray['eastAsian']); |
| 214 |
} |
| 215 |
if (isset($styleArray['complexScript'])) { |
| 216 |
$this->setComplexScript( |
| 217 |
$styleArray['complexScript'] |
| 218 |
); |
| 219 |
} |
| 220 |
if (isset($styleArray['bold'])) { |
| 221 |
$this->setBold($styleArray['bold']); |
| 222 |
} |
| 223 |
if (isset($styleArray['italic'])) { |
| 224 |
$this->setItalic($styleArray['italic']); |
| 225 |
} |
| 226 |
if (isset($styleArray['superscript'])) { |
| 227 |
$this->setSuperscript($styleArray['superscript']); |
| 228 |
} |
| 229 |
if (isset($styleArray['subscript'])) { |
| 230 |
$this->setSubscript($styleArray['subscript']); |
| 231 |
} |
| 232 |
if (isset($styleArray['underline'])) { |
| 233 |
$this->setUnderline($styleArray['underline']); |
| 234 |
} |
| 235 |
if (isset($styleArray['strikethrough'])) { |
| 236 |
$this->setStrikethrough( |
| 237 |
$styleArray['strikethrough'] |
| 238 |
); |
| 239 |
} |
| 240 |
if (isset($styleArray['color'])) { |
| 241 |
/** @var array{rgb?: string, argb?: string, theme?: int} */ |
| 242 |
$temp = $styleArray['color']; |
| 243 |
$this->getColor() |
| 244 |
->applyFromArray($temp); |
| 245 |
} |
| 246 |
if (isset($styleArray['size'])) { |
| 247 |
$this->setSize($styleArray['size']); |
| 248 |
} |
| 249 |
if (isset($styleArray['chartColor'])) { |
| 250 |
$this->chartColor = $styleArray['chartColor']; |
| 251 |
} |
| 252 |
if (isset($styleArray['scheme'])) { |
| 253 |
$this->setScheme($styleArray['scheme']); |
| 254 |
} |
| 255 |
if (isset($styleArray['cap'])) { |
| 256 |
$this->setCap($styleArray['cap']); |
| 257 |
} |
| 258 |
if (isset($styleArray['autoColor'])) { |
| 259 |
$this->setAutoColor($styleArray['autoColor']); |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
return $this; |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Get Name. |
| 268 |
*/ |
| 269 |
public function getName(): ?string |
| 270 |
{ |
| 271 |
if ($this->isSupervisor) { |
| 272 |
return $this->getSharedComponent()->getName(); |
| 273 |
} |
| 274 |
|
| 275 |
return $this->name; |
| 276 |
} |
| 277 |
|
| 278 |
public function getLatin(): string |
| 279 |
{ |
| 280 |
if ($this->isSupervisor) { |
| 281 |
return $this->getSharedComponent()->getLatin(); |
| 282 |
} |
| 283 |
|
| 284 |
return $this->latin; |
| 285 |
} |
| 286 |
|
| 287 |
public function getEastAsian(): string |
| 288 |
{ |
| 289 |
if ($this->isSupervisor) { |
| 290 |
return $this->getSharedComponent()->getEastAsian(); |
| 291 |
} |
| 292 |
|
| 293 |
return $this->eastAsian; |
| 294 |
} |
| 295 |
|
| 296 |
public function getComplexScript(): string |
| 297 |
{ |
| 298 |
if ($this->isSupervisor) { |
| 299 |
return $this->getSharedComponent()->getComplexScript(); |
| 300 |
} |
| 301 |
|
| 302 |
return $this->complexScript; |
| 303 |
} |
| 304 |
|
| 305 |
/** |
| 306 |
* Set Name and turn off Scheme. |
| 307 |
*/ |
| 308 |
public function setName(string $fontname): self |
| 309 |
{ |
| 310 |
if ($fontname == '') { |
| 311 |
$fontname = 'Calibri'; |
| 312 |
} |
| 313 |
if ($this->isSupervisor) { |
| 314 |
$styleArray = $this->getStyleArray(['name' => $fontname]); |
| 315 |
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
| 316 |
} else { |
| 317 |
$this->name = $fontname; |
| 318 |
} |
| 319 |
|
| 320 |
return $this->setScheme(''); |
| 321 |
} |
| 322 |
|
| 323 |
public function setLatin(string $fontname): self |
| 324 |
{ |
| 325 |
if ($fontname == '') { |
| 326 |
$fontname = 'Calibri'; |
| 327 |
} |
| 328 |
if (!$this->isSupervisor) { |
| 329 |
$this->latin = $fontname; |
| 330 |
} else { |
| 331 |
// should never be true |
| 332 |
// @codeCoverageIgnoreStart |
| 333 |
$styleArray = $this->getStyleArray(['latin' => $fontname]); |
| 334 |
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
| 335 |
// @codeCoverageIgnoreEnd |
| 336 |
} |
| 337 |
|
| 338 |
return $this; |
| 339 |
} |
| 340 |
|
| 341 |
public function setEastAsian(string $fontname): self |
| 342 |
{ |
| 343 |
if ($fontname == '') { |
| 344 |
$fontname = 'Calibri'; |
| 345 |
} |
| 346 |
if (!$this->isSupervisor) { |
| 347 |
$this->eastAsian = $fontname; |
| 348 |
} else { |
| 349 |
// should never be true |
| 350 |
// @codeCoverageIgnoreStart |
| 351 |
$styleArray = $this->getStyleArray(['eastAsian' => $fontname]); |
| 352 |
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
| 353 |
// @codeCoverageIgnoreEnd |
| 354 |
} |
| 355 |
|
| 356 |
return $this; |
| 357 |
} |
| 358 |
|
| 359 |
public function setComplexScript(string $fontname): self |
| 360 |
{ |
| 361 |
if ($fontname == '') { |
| 362 |
$fontname = 'Calibri'; |
| 363 |
} |
| 364 |
if (!$this->isSupervisor) { |
| 365 |
$this->complexScript = $fontname; |
| 366 |
} else { |
| 367 |
// should never be true |
| 368 |
// @codeCoverageIgnoreStart |
| 369 |
$styleArray = $this->getStyleArray(['complexScript' => $fontname]); |
| 370 |
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
| 371 |
// @codeCoverageIgnoreEnd |
| 372 |
} |
| 373 |
|
| 374 |
return $this; |
| 375 |
} |
| 376 |
|
| 377 |
/** |
| 378 |
* Get Size. |
| 379 |
*/ |
| 380 |
public function getSize(): ?float |
| 381 |
{ |
| 382 |
if ($this->isSupervisor) { |
| 383 |
return $this->getSharedComponent()->getSize(); |
| 384 |
} |
| 385 |
|
| 386 |
return $this->size; |
| 387 |
} |
| 388 |
|
| 389 |
/** |
| 390 |
* Set Size. |
| 391 |
* |
| 392 |
* @param mixed $sizeInPoints A float representing the value of a positive measurement in points (1/72 of an inch) |
| 393 |
* |
| 394 |
* @return $this |
| 395 |
*/ |
| 396 |
public function setSize($sizeInPoints, bool $nullOk = false) |
| 397 |
{ |
| 398 |
if (is_string($sizeInPoints) || is_int($sizeInPoints)) { |
| 399 |
$sizeInPoints = (float) $sizeInPoints; // $pValue = 0 if given string is not numeric |
| 400 |
} |
| 401 |
|
| 402 |
// Size must be a positive floating point number |
| 403 |
// ECMA-376-1:2016, part 1, chapter 18.4.11 sz (Font Size), p. 1536 |
| 404 |
if (!is_float($sizeInPoints) || !($sizeInPoints > 0)) { |
| 405 |
if (!$nullOk || $sizeInPoints !== null) { |
| 406 |
$sizeInPoints = 10.0; |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
if ($this->isSupervisor) { |
| 411 |
$styleArray = $this->getStyleArray(['size' => $sizeInPoints]); |
| 412 |
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
| 413 |
} else { |
| 414 |
$this->size = $sizeInPoints; |
| 415 |
} |
| 416 |
|
| 417 |
return $this; |
| 418 |
} |
| 419 |
|
| 420 |
/** |
| 421 |
* Get Bold. |
| 422 |
*/ |
| 423 |
public function getBold(): ?bool |
| 424 |
{ |
| 425 |
if ($this->isSupervisor) { |
| 426 |
return $this->getSharedComponent()->getBold(); |
| 427 |
} |
| 428 |
|
| 429 |
return $this->bold; |
| 430 |
} |
| 431 |
|
| 432 |
/** |
| 433 |
* Set Bold. |
| 434 |
* |
| 435 |
* @return $this |
| 436 |
*/ |
| 437 |
public function setBold(bool $bold) |
| 438 |
{ |
| 439 |
if ($this->isSupervisor) { |
| 440 |
$styleArray = $this->getStyleArray(['bold' => $bold]); |
| 441 |
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
| 442 |
} else { |
| 443 |
$this->bold = $bold; |
| 444 |
} |
| 445 |
|
| 446 |
return $this; |
| 447 |
} |
| 448 |
|
| 449 |
/** |
| 450 |
* Get Italic. |
| 451 |
*/ |
| 452 |
public function getItalic(): ?bool |
| 453 |
{ |
| 454 |
if ($this->isSupervisor) { |
| 455 |
return $this->getSharedComponent()->getItalic(); |
| 456 |
} |
| 457 |
|
| 458 |
return $this->italic; |
| 459 |
} |
| 460 |
|
| 461 |
/** |
| 462 |
* Set Italic. |
| 463 |
* |
| 464 |
* @return $this |
| 465 |
*/ |
| 466 |
public function setItalic(bool $italic) |
| 467 |
{ |
| 468 |
if ($this->isSupervisor) { |
| 469 |
$styleArray = $this->getStyleArray(['italic' => $italic]); |
| 470 |
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
| 471 |
} else { |
| 472 |
$this->italic = $italic; |
| 473 |
} |
| 474 |
|
| 475 |
return $this; |
| 476 |
} |
| 477 |
|
| 478 |
/** |
| 479 |
* Get Superscript. |
| 480 |
*/ |
| 481 |
public function getSuperscript(): ?bool |
| 482 |
{ |
| 483 |
if ($this->isSupervisor) { |
| 484 |
return $this->getSharedComponent()->getSuperscript(); |
| 485 |
} |
| 486 |
|
| 487 |
return $this->superscript; |
| 488 |
} |
| 489 |
|
| 490 |
/** |
| 491 |
* Set Superscript. |
| 492 |
* |
| 493 |
* @return $this |
| 494 |
*/ |
| 495 |
public function setSuperscript(bool $superscript) |
| 496 |
{ |
| 497 |
if ($this->isSupervisor) { |
| 498 |
$styleArray = $this->getStyleArray(['superscript' => $superscript]); |
| 499 |
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
| 500 |
} else { |
| 501 |
$this->superscript = $superscript; |
| 502 |
if ($this->superscript) { |
| 503 |
$this->subscript = false; |
| 504 |
} |
| 505 |
} |
| 506 |
|
| 507 |
return $this; |
| 508 |
} |
| 509 |
|
| 510 |
/** |
| 511 |
* Get Subscript. |
| 512 |
*/ |
| 513 |
public function getSubscript(): ?bool |
| 514 |
{ |
| 515 |
if ($this->isSupervisor) { |
| 516 |
return $this->getSharedComponent()->getSubscript(); |
| 517 |
} |
| 518 |
|
| 519 |
return $this->subscript; |
| 520 |
} |
| 521 |
|
| 522 |
/** |
| 523 |
* Set Subscript. |
| 524 |
* |
| 525 |
* @return $this |
| 526 |
*/ |
| 527 |
public function setSubscript(bool $subscript) |
| 528 |
{ |
| 529 |
if ($this->isSupervisor) { |
| 530 |
$styleArray = $this->getStyleArray(['subscript' => $subscript]); |
| 531 |
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
| 532 |
} else { |
| 533 |
$this->subscript = $subscript; |
| 534 |
if ($this->subscript) { |
| 535 |
$this->superscript = false; |
| 536 |
} |
| 537 |
} |
| 538 |
|
| 539 |
return $this; |
| 540 |
} |
| 541 |
|
| 542 |
public function getBaseLine(): int |
| 543 |
{ |
| 544 |
if ($this->isSupervisor) { |
| 545 |
return $this->getSharedComponent()->getBaseLine(); |
| 546 |
} |
| 547 |
|
| 548 |
return $this->baseLine; |
| 549 |
} |
| 550 |
|
| 551 |
public function setBaseLine(int $baseLine): self |
| 552 |
{ |
| 553 |
if (!$this->isSupervisor) { |
| 554 |
$this->baseLine = $baseLine; |
| 555 |
} else { |
| 556 |
// should never be true |
| 557 |
// @codeCoverageIgnoreStart |
| 558 |
$styleArray = $this->getStyleArray(['baseLine' => $baseLine]); |
| 559 |
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
| 560 |
// @codeCoverageIgnoreEnd |
| 561 |
} |
| 562 |
|
| 563 |
return $this; |
| 564 |
} |
| 565 |
|
| 566 |
public function getStrikeType(): string |
| 567 |
{ |
| 568 |
if ($this->isSupervisor) { |
| 569 |
return $this->getSharedComponent()->getStrikeType(); |
| 570 |
} |
| 571 |
|
| 572 |
return $this->strikeType; |
| 573 |
} |
| 574 |
|
| 575 |
public function setStrikeType(string $strikeType): self |
| 576 |
{ |
| 577 |
if (!$this->isSupervisor) { |
| 578 |
$this->strikeType = $strikeType; |
| 579 |
} else { |
| 580 |
// should never be true |
| 581 |
// @codeCoverageIgnoreStart |
| 582 |
$styleArray = $this->getStyleArray(['strikeType' => $strikeType]); |
| 583 |
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
| 584 |
// @codeCoverageIgnoreEnd |
| 585 |
} |
| 586 |
|
| 587 |
return $this; |
| 588 |
} |
| 589 |
|
| 590 |
public function getUnderlineColor(): ?ChartColor |
| 591 |
{ |
| 592 |
if ($this->isSupervisor) { |
| 593 |
return $this->getSharedComponent()->getUnderlineColor(); |
| 594 |
} |
| 595 |
|
| 596 |
return $this->underlineColor; |
| 597 |
} |
| 598 |
|
| 599 |
/** @param array{value: null|string, alpha: null|int|string, brightness?: null|int|string, type: null|string} $colorArray */ |
| 600 |
public function setUnderlineColor(array $colorArray): self |
| 601 |
{ |
| 602 |
if (!$this->isSupervisor) { |
| 603 |
$this->underlineColor = new ChartColor($colorArray); |
| 604 |
} else { |
| 605 |
// should never be true |
| 606 |
// @codeCoverageIgnoreStart |
| 607 |
$styleArray = $this->getStyleArray(['underlineColor' => $colorArray]); |
| 608 |
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
| 609 |
// @codeCoverageIgnoreEnd |
| 610 |
} |
| 611 |
|
| 612 |
return $this; |
| 613 |
} |
| 614 |
|
| 615 |
public function getChartColor(): ?ChartColor |
| 616 |
{ |
| 617 |
if ($this->isSupervisor) { |
| 618 |
return $this->getSharedComponent()->getChartColor(); |
| 619 |
} |
| 620 |
|
| 621 |
return $this->chartColor; |
| 622 |
} |
| 623 |
|
| 624 |
/** @param array{value: null|string, alpha: null|int|string, brightness?: null|int|string, type: null|string} $colorArray */ |
| 625 |
public function setChartColor(array $colorArray): self |
| 626 |
{ |
| 627 |
if (!$this->isSupervisor) { |
| 628 |
$this->chartColor = new ChartColor($colorArray); |
| 629 |
} else { |
| 630 |
// should never be true |
| 631 |
// @codeCoverageIgnoreStart |
| 632 |
$styleArray = $this->getStyleArray(['chartColor' => $colorArray]); |
| 633 |
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
| 634 |
// @codeCoverageIgnoreEnd |
| 635 |
} |
| 636 |
|
| 637 |
return $this; |
| 638 |
} |
| 639 |
|
| 640 |
public function setChartColorFromObject(?ChartColor $chartColor): self |
| 641 |
{ |
| 642 |
$this->chartColor = $chartColor; |
| 643 |
|
| 644 |
return $this; |
| 645 |
} |
| 646 |
|
| 647 |
/** |
| 648 |
* Get Underline. |
| 649 |
*/ |
| 650 |
public function getUnderline(): ?string |
| 651 |
{ |
| 652 |
if ($this->isSupervisor) { |
| 653 |
return $this->getSharedComponent()->getUnderline(); |
| 654 |
} |
| 655 |
|
| 656 |
return $this->underline; |
| 657 |
} |
| 658 |
|
| 659 |
/** |
| 660 |
* Set Underline. |
| 661 |
* |
| 662 |
* @param bool|string $underlineStyle \PhpOffice\PhpSpreadsheet\Style\Font underline type |
| 663 |
* If a boolean is passed, then TRUE equates to UNDERLINE_SINGLE, |
| 664 |
* false equates to UNDERLINE_NONE |
| 665 |
* |
| 666 |
* @return $this |
| 667 |
*/ |
| 668 |
public function setUnderline($underlineStyle) |
| 669 |
{ |
| 670 |
if (is_bool($underlineStyle)) { |
| 671 |
$underlineStyle = ($underlineStyle) ? self::UNDERLINE_SINGLE : self::UNDERLINE_NONE; |
| 672 |
} elseif ($underlineStyle == '') { |
| 673 |
$underlineStyle = self::UNDERLINE_NONE; |
| 674 |
} |
| 675 |
if ($this->isSupervisor) { |
| 676 |
$styleArray = $this->getStyleArray(['underline' => $underlineStyle]); |
| 677 |
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
| 678 |
} else { |
| 679 |
$this->underline = $underlineStyle; |
| 680 |
} |
| 681 |
|
| 682 |
return $this; |
| 683 |
} |
| 684 |
|
| 685 |
/** |
| 686 |
* Get Strikethrough. |
| 687 |
*/ |
| 688 |
public function getStrikethrough(): ?bool |
| 689 |
{ |
| 690 |
if ($this->isSupervisor) { |
| 691 |
return $this->getSharedComponent()->getStrikethrough(); |
| 692 |
} |
| 693 |
|
| 694 |
return $this->strikethrough; |
| 695 |
} |
| 696 |
|
| 697 |
/** |
| 698 |
* Set Strikethrough. |
| 699 |
* |
| 700 |
* @return $this |
| 701 |
*/ |
| 702 |
public function setStrikethrough(bool $strikethru) |
| 703 |
{ |
| 704 |
if ($this->isSupervisor) { |
| 705 |
$styleArray = $this->getStyleArray(['strikethrough' => $strikethru]); |
| 706 |
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
| 707 |
} else { |
| 708 |
$this->strikethrough = $strikethru; |
| 709 |
} |
| 710 |
|
| 711 |
return $this; |
| 712 |
} |
| 713 |
|
| 714 |
/** |
| 715 |
* Get Color. |
| 716 |
*/ |
| 717 |
public function getColor(): Color |
| 718 |
{ |
| 719 |
return $this->color; |
| 720 |
} |
| 721 |
|
| 722 |
/** |
| 723 |
* Set Color. |
| 724 |
* |
| 725 |
* @return $this |
| 726 |
*/ |
| 727 |
public function setColor(Color $color) |
| 728 |
{ |
| 729 |
// make sure parameter is a real color and not a supervisor |
| 730 |
$color = $color->getIsSupervisor() ? $color->getSharedComponent() : $color; |
| 731 |
|
| 732 |
if ($this->isSupervisor) { |
| 733 |
$styleArray = $this->getColor()->getStyleArray(['argb' => $color->getARGB()]); |
| 734 |
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
| 735 |
} else { |
| 736 |
$this->color = $color; |
| 737 |
} |
| 738 |
|
| 739 |
return $this; |
| 740 |
} |
| 741 |
|
| 742 |
private function hashChartColor(?ChartColor $underlineColor): string |
| 743 |
{ |
| 744 |
if ($underlineColor === null) { |
| 745 |
return ''; |
| 746 |
} |
| 747 |
|
| 748 |
return |
| 749 |
$underlineColor->getValue() |
| 750 |
. $underlineColor->getType() |
| 751 |
. (string) $underlineColor->getAlpha(); |
| 752 |
} |
| 753 |
|
| 754 |
/** |
| 755 |
* Get hash code. |
| 756 |
* |
| 757 |
* @return string Hash code |
| 758 |
*/ |
| 759 |
public function getHashCode(): string |
| 760 |
{ |
| 761 |
if ($this->isSupervisor) { |
| 762 |
return $this->getSharedComponent()->getHashCode(); |
| 763 |
} |
| 764 |
|
| 765 |
return md5( |
| 766 |
$this->name |
| 767 |
. $this->size |
| 768 |
. ($this->bold ? 't' : 'f') |
| 769 |
. ($this->italic ? 't' : 'f') |
| 770 |
. ($this->superscript ? 't' : 'f') |
| 771 |
. ($this->subscript ? 't' : 'f') |
| 772 |
. $this->underline |
| 773 |
. ($this->strikethrough ? 't' : 'f') |
| 774 |
. ($this->autoColor ? 't' : 'f') |
| 775 |
. $this->color->getHashCode() |
| 776 |
. $this->scheme |
| 777 |
. implode( |
| 778 |
'*', |
| 779 |
[ |
| 780 |
$this->latin, |
| 781 |
$this->eastAsian, |
| 782 |
$this->complexScript, |
| 783 |
$this->strikeType, |
| 784 |
$this->hashChartColor($this->chartColor), |
| 785 |
$this->hashChartColor($this->underlineColor), |
| 786 |
(string) $this->baseLine, |
| 787 |
(string) $this->cap, |
| 788 |
] |
| 789 |
) |
| 790 |
. __CLASS__ |
| 791 |
); |
| 792 |
} |
| 793 |
|
| 794 |
/** @return mixed[] */ |
| 795 |
protected function exportArray1(): array |
| 796 |
{ |
| 797 |
$exportedArray = []; |
| 798 |
$this->exportArray2($exportedArray, 'baseLine', $this->getBaseLine()); |
| 799 |
$this->exportArray2($exportedArray, 'bold', $this->getBold()); |
| 800 |
$this->exportArray2($exportedArray, 'cap', $this->getCap()); |
| 801 |
$this->exportArray2($exportedArray, 'chartColor', $this->getChartColor()); |
| 802 |
$this->exportArray2($exportedArray, 'color', $this->getColor()); |
| 803 |
$this->exportArray2($exportedArray, 'complexScript', $this->getComplexScript()); |
| 804 |
$this->exportArray2($exportedArray, 'eastAsian', $this->getEastAsian()); |
| 805 |
$this->exportArray2($exportedArray, 'italic', $this->getItalic()); |
| 806 |
$this->exportArray2($exportedArray, 'latin', $this->getLatin()); |
| 807 |
$this->exportArray2($exportedArray, 'name', $this->getName()); |
| 808 |
$this->exportArray2($exportedArray, 'scheme', $this->getScheme()); |
| 809 |
$this->exportArray2($exportedArray, 'size', $this->getSize()); |
| 810 |
$this->exportArray2($exportedArray, 'strikethrough', $this->getStrikethrough()); |
| 811 |
$this->exportArray2($exportedArray, 'strikeType', $this->getStrikeType()); |
| 812 |
$this->exportArray2($exportedArray, 'subscript', $this->getSubscript()); |
| 813 |
$this->exportArray2($exportedArray, 'superscript', $this->getSuperscript()); |
| 814 |
$this->exportArray2($exportedArray, 'underline', $this->getUnderline()); |
| 815 |
$this->exportArray2($exportedArray, 'underlineColor', $this->getUnderlineColor()); |
| 816 |
$this->exportArray2($exportedArray, 'autoColor', $this->getAutoColor()); |
| 817 |
|
| 818 |
return $exportedArray; |
| 819 |
} |
| 820 |
|
| 821 |
public function getScheme(): string |
| 822 |
{ |
| 823 |
if ($this->isSupervisor) { |
| 824 |
return $this->getSharedComponent()->getScheme(); |
| 825 |
} |
| 826 |
|
| 827 |
return $this->scheme; |
| 828 |
} |
| 829 |
|
| 830 |
public function setScheme(string $scheme): self |
| 831 |
{ |
| 832 |
if ($scheme === '' || $scheme === 'major' || $scheme === 'minor') { |
| 833 |
if ($this->isSupervisor) { |
| 834 |
$styleArray = $this->getStyleArray(['scheme' => $scheme]); |
| 835 |
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); |
| 836 |
} else { |
| 837 |
$this->scheme = $scheme; |
| 838 |
} |
| 839 |
} |
| 840 |
|
| 841 |
return $this; |
| 842 |
} |
| 843 |
|
| 844 |
/** |
| 845 |
* Set capitalization attribute. If not one of the permitted |
| 846 |
* values (all, small, or none), set it to null. |
| 847 |
* This will be honored only for the font for chart titles. |
| 848 |
* None is distinguished from null because null will inherit |
| 849 |
* the current value, whereas 'none' will override it. |
| 850 |
*/ |
| 851 |
public function setCap(string $cap): self |
| 852 |
{ |
| 853 |
$this->cap = in_array($cap, self::VALID_CAPS, true) ? $cap : null; |
| 854 |
|
| 855 |
return $this; |
| 856 |
} |
| 857 |
|
| 858 |
public function getCap(): ?string |
| 859 |
{ |
| 860 |
return $this->cap; |
| 861 |
} |
| 862 |
|
| 863 |
public function setHyperlinkTheme(): self |
| 864 |
{ |
| 865 |
$this->color->setHyperlinkTheme(); |
| 866 |
$this->setUnderline(self::UNDERLINE_SINGLE); |
| 867 |
|
| 868 |
return $this; |
| 869 |
} |
| 870 |
|
| 871 |
public function setAutoColor(bool $autoColor): self |
| 872 |
{ |
| 873 |
if ($this->isSupervisor) { |
| 874 |
$styleArray = $this->getStyleArray(['autoColor' => $autoColor]); |
| 875 |
$this->getActiveSheet() |
| 876 |
->getStyle($this->getSelectedCells()) |
| 877 |
->applyFromArray($styleArray); |
| 878 |
} else { |
| 879 |
$this->autoColor = $autoColor; |
| 880 |
} |
| 881 |
|
| 882 |
return $this; |
| 883 |
} |
| 884 |
|
| 885 |
public function getAutoColor(): bool |
| 886 |
{ |
| 887 |
if ($this->isSupervisor) { |
| 888 |
return $this->getSharedComponent()->getAutoColor(); |
| 889 |
} |
| 890 |
|
| 891 |
return $this->autoColor; |
| 892 |
} |
| 893 |
|
| 894 |
/** |
| 895 |
* Implement PHP __clone to create a deep clone, not just a shallow copy. |
| 896 |
*/ |
| 897 |
public function __clone() |
| 898 |
{ |
| 899 |
$this->color = clone $this->color; |
| 900 |
$this->chartColor = ($this->chartColor === null) ? null : clone $this->chartColor; |
| 901 |
$this->underlineColor = ($this->underlineColor === null) ? null : clone $this->underlineColor; |
| 902 |
} |
| 903 |
} |
| 904 |
|