| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Chart; |
| 4 |
|
| 5 |
use TablePress\PhpOffice\PhpSpreadsheet\RichText\RichText; |
| 6 |
use TablePress\PhpOffice\PhpSpreadsheet\Spreadsheet; |
| 7 |
use TablePress\PhpOffice\PhpSpreadsheet\Style\Font; |
| 8 |
|
| 9 |
class Title |
| 10 |
{ |
| 11 |
public const TITLE_CELL_REFERENCE |
| 12 |
= '/^(.*)!' // beginning of string, everything up to ! is match[1] |
| 13 |
. '[$]([A-Z]{1,3})' // absolute column string match[2] |
| 14 |
. '[$](\d{1,7})$/i'; // absolute row string match[3] |
| 15 |
|
| 16 |
/** |
| 17 |
* Title Caption. |
| 18 |
* |
| 19 |
* @var array<RichText|string>|RichText|string |
| 20 |
*/ |
| 21 |
private $caption; |
| 22 |
|
| 23 |
/** |
| 24 |
* Allow overlay of other elements? |
| 25 |
*/ |
| 26 |
private bool $overlay = true; |
| 27 |
|
| 28 |
/** |
| 29 |
* Title Layout. |
| 30 |
*/ |
| 31 |
private ?Layout $layout; |
| 32 |
|
| 33 |
private string $cellReference = ''; |
| 34 |
|
| 35 |
private ?Font $font = null; |
| 36 |
|
| 37 |
/** |
| 38 |
* Create a new Title. |
| 39 |
* |
| 40 |
* @param array<RichText|string>|RichText|string $caption |
| 41 |
*/ |
| 42 |
public function __construct($caption = '', ?Layout $layout = null, bool $overlay = false) |
| 43 |
{ |
| 44 |
$this->caption = $caption; |
| 45 |
$this->layout = $layout; |
| 46 |
$this->setOverlay($overlay); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Get caption. |
| 51 |
* |
| 52 |
* @return array<RichText|string>|RichText|string |
| 53 |
*/ |
| 54 |
public function getCaption() |
| 55 |
{ |
| 56 |
return $this->caption; |
| 57 |
} |
| 58 |
|
| 59 |
public function getCaptionText(?Spreadsheet $spreadsheet = null): string |
| 60 |
{ |
| 61 |
if ($spreadsheet !== null) { |
| 62 |
$caption = $this->getCalculatedTitle($spreadsheet); |
| 63 |
if ($caption !== null) { |
| 64 |
return $caption; |
| 65 |
} |
| 66 |
} |
| 67 |
$caption = $this->caption; |
| 68 |
if (is_string($caption)) { |
| 69 |
return $caption; |
| 70 |
} |
| 71 |
if ($caption instanceof RichText) { |
| 72 |
return $caption->getPlainText(); |
| 73 |
} |
| 74 |
$retVal = ''; |
| 75 |
foreach ($caption as $textx) { |
| 76 |
/** @var RichText|string $text */ |
| 77 |
$text = $textx; |
| 78 |
if ($text instanceof RichText) { |
| 79 |
$retVal .= $text->getPlainText(); |
| 80 |
} else { |
| 81 |
$retVal .= $text; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
return $retVal; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Set caption. |
| 90 |
* |
| 91 |
* @param array<RichText|string>|RichText|string $caption |
| 92 |
* |
| 93 |
* @return $this |
| 94 |
*/ |
| 95 |
public function setCaption($caption) |
| 96 |
{ |
| 97 |
$this->caption = $caption; |
| 98 |
|
| 99 |
return $this; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Get allow overlay of other elements? |
| 104 |
*/ |
| 105 |
public function getOverlay(): bool |
| 106 |
{ |
| 107 |
return $this->overlay; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Set allow overlay of other elements? |
| 112 |
*/ |
| 113 |
public function setOverlay(bool $overlay): self |
| 114 |
{ |
| 115 |
$this->overlay = $overlay; |
| 116 |
|
| 117 |
return $this; |
| 118 |
} |
| 119 |
|
| 120 |
public function getLayout(): ?Layout |
| 121 |
{ |
| 122 |
return $this->layout; |
| 123 |
} |
| 124 |
|
| 125 |
public function setCellReference(string $cellReference): self |
| 126 |
{ |
| 127 |
$this->cellReference = $cellReference; |
| 128 |
|
| 129 |
return $this; |
| 130 |
} |
| 131 |
|
| 132 |
public function getCellReference(): string |
| 133 |
{ |
| 134 |
return $this->cellReference; |
| 135 |
} |
| 136 |
|
| 137 |
public function getCalculatedTitle(?Spreadsheet $spreadsheet): ?string |
| 138 |
{ |
| 139 |
preg_match(self::TITLE_CELL_REFERENCE, $this->cellReference, $matches); |
| 140 |
if (count($matches) === 0 || $spreadsheet === null) { |
| 141 |
return null; |
| 142 |
} |
| 143 |
$sheetName = preg_replace("/^'(.*)'$/", '$1', $matches[1]) ?? ''; |
| 144 |
|
| 145 |
return ($nullsafeVariable1 = ($nullsafeVariable2 = $spreadsheet->getSheetByName($sheetName)) ? $nullsafeVariable2->getCell($matches[2] . $matches[3]) : null) ? $nullsafeVariable1->getFormattedValue() : null; |
| 146 |
} |
| 147 |
|
| 148 |
public function getFont(): ?Font |
| 149 |
{ |
| 150 |
return $this->font; |
| 151 |
} |
| 152 |
|
| 153 |
public function setFont(?Font $font): self |
| 154 |
{ |
| 155 |
$this->font = $font; |
| 156 |
|
| 157 |
return $this; |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Implement PHP __clone to create a deep clone, not just a shallow copy. |
| 162 |
*/ |
| 163 |
public function __clone() |
| 164 |
{ |
| 165 |
$this->layout = ($this->layout === null) ? null : clone $this->layout; |
| 166 |
$this->font = ($this->font === null) ? null : clone $this->font; |
| 167 |
if (is_array($this->caption)) { |
| 168 |
$captions = []; |
| 169 |
foreach ($this->caption as $caption) { |
| 170 |
$captions[] = is_object($caption) ? (clone $caption) : $caption; |
| 171 |
} |
| 172 |
$this->caption = $captions; |
| 173 |
} else { |
| 174 |
$this->caption = is_object($this->caption) ? (clone $this->caption) : $this->caption; |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
|