| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Worksheet\PivotTable; |
| 4 |
|
| 5 |
use TablePress\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; |
| 6 |
use Stringable; |
| 7 |
|
| 8 |
/** |
| 9 |
* Read-only representation of a pivot table. |
| 10 |
* |
| 11 |
* This models the metadata of a pivot table that already exists in a loaded |
| 12 |
* spreadsheet: its name, where it is placed on the sheet, the cache it draws |
| 13 |
* its data from, and the fields on each axis. It does not (yet) recalculate or |
| 14 |
* render the pivot, and it does not modify the underlying file - existing pivot |
| 15 |
* tables are still written back verbatim from the preserved source XML. |
| 16 |
* |
| 17 |
* @see PivotCacheDefinition |
| 18 |
* @see PivotField |
| 19 |
*/ |
| 20 |
class PivotTable |
| 21 |
{ |
| 22 |
/** |
| 23 |
* Pivot table name (the "name" attribute of the pivotTableDefinition). |
| 24 |
*/ |
| 25 |
private string $name; |
| 26 |
/** |
| 27 |
* The worksheet this pivot table is placed on. |
| 28 |
*/ |
| 29 |
private ?Worksheet $worksheet = null; |
| 30 |
/** |
| 31 |
* Target range on the worksheet occupied by the rendered pivot table, |
| 32 |
* taken from location@ref, e.g. "A3:D20". |
| 33 |
*/ |
| 34 |
private string $location = ''; |
| 35 |
/** |
| 36 |
* The cache definition this pivot table draws its data from. |
| 37 |
*/ |
| 38 |
private ?PivotCacheDefinition $cacheDefinition = null; |
| 39 |
/** |
| 40 |
* The pivot fields, in field-index order. |
| 41 |
* |
| 42 |
* @var PivotField[] |
| 43 |
*/ |
| 44 |
private array $fields = []; |
| 45 |
/** |
| 46 |
* True when this pivot table was built in memory (rather than loaded from a |
| 47 |
* file) and therefore must have its OOXML parts generated on save. Loaded |
| 48 |
* pivot tables keep their original XML and are written back verbatim. |
| 49 |
*/ |
| 50 |
private bool $generated = false; |
| 51 |
public function __construct(string $name = '') |
| 52 |
{ |
| 53 |
$this->name = $name; |
| 54 |
} |
| 55 |
public function isGenerated(): bool |
| 56 |
{ |
| 57 |
return $this->generated; |
| 58 |
} |
| 59 |
public function setGenerated(bool $generated): self |
| 60 |
{ |
| 61 |
$this->generated = $generated; |
| 62 |
|
| 63 |
return $this; |
| 64 |
} |
| 65 |
/** |
| 66 |
* Code to execute when this pivot table is unset(). |
| 67 |
*/ |
| 68 |
public function __destruct() |
| 69 |
{ |
| 70 |
$this->worksheet = null; |
| 71 |
} |
| 72 |
public function getName(): string |
| 73 |
{ |
| 74 |
return $this->name; |
| 75 |
} |
| 76 |
public function setName(string $name): self |
| 77 |
{ |
| 78 |
$this->name = $name; |
| 79 |
|
| 80 |
return $this; |
| 81 |
} |
| 82 |
public function getWorksheet(): ?Worksheet |
| 83 |
{ |
| 84 |
return $this->worksheet; |
| 85 |
} |
| 86 |
public function setWorksheet(?Worksheet $worksheet): self |
| 87 |
{ |
| 88 |
$this->worksheet = $worksheet; |
| 89 |
|
| 90 |
return $this; |
| 91 |
} |
| 92 |
public function getLocation(): string |
| 93 |
{ |
| 94 |
return $this->location; |
| 95 |
} |
| 96 |
public function setLocation(string $location): self |
| 97 |
{ |
| 98 |
$this->location = $location; |
| 99 |
|
| 100 |
return $this; |
| 101 |
} |
| 102 |
public function getCacheDefinition(): ?PivotCacheDefinition |
| 103 |
{ |
| 104 |
return $this->cacheDefinition; |
| 105 |
} |
| 106 |
public function setCacheDefinition(?PivotCacheDefinition $cacheDefinition): self |
| 107 |
{ |
| 108 |
$this->cacheDefinition = $cacheDefinition; |
| 109 |
|
| 110 |
return $this; |
| 111 |
} |
| 112 |
/** |
| 113 |
* @return PivotField[] |
| 114 |
*/ |
| 115 |
public function getFields(): array |
| 116 |
{ |
| 117 |
return $this->fields; |
| 118 |
} |
| 119 |
public function addField(PivotField $field): self |
| 120 |
{ |
| 121 |
$this->fields[] = $field; |
| 122 |
|
| 123 |
return $this; |
| 124 |
} |
| 125 |
/** |
| 126 |
* Return only the fields placed on the row axis, in order. |
| 127 |
* |
| 128 |
* @return PivotField[] |
| 129 |
*/ |
| 130 |
public function getRowFields(): array |
| 131 |
{ |
| 132 |
return $this->getFieldsOnAxis(PivotField::AXIS_ROW); |
| 133 |
} |
| 134 |
/** |
| 135 |
* Return only the fields placed on the column axis, in order. |
| 136 |
* |
| 137 |
* @return PivotField[] |
| 138 |
*/ |
| 139 |
public function getColumnFields(): array |
| 140 |
{ |
| 141 |
return $this->getFieldsOnAxis(PivotField::AXIS_COLUMN); |
| 142 |
} |
| 143 |
/** |
| 144 |
* Return only the fields placed on the page/filter axis, in order. |
| 145 |
* |
| 146 |
* @return PivotField[] |
| 147 |
*/ |
| 148 |
public function getPageFields(): array |
| 149 |
{ |
| 150 |
return $this->getFieldsOnAxis(PivotField::AXIS_PAGE); |
| 151 |
} |
| 152 |
/** |
| 153 |
* Return only the value/data fields, in order. |
| 154 |
* |
| 155 |
* @return PivotField[] |
| 156 |
*/ |
| 157 |
public function getDataFields(): array |
| 158 |
{ |
| 159 |
return array_values( |
| 160 |
array_filter( |
| 161 |
$this->fields, |
| 162 |
static fn (PivotField $field): bool => $field->isDataField() |
| 163 |
) |
| 164 |
); |
| 165 |
} |
| 166 |
/** |
| 167 |
* @return PivotField[] |
| 168 |
*/ |
| 169 |
private function getFieldsOnAxis(string $axis): array |
| 170 |
{ |
| 171 |
return array_values( |
| 172 |
array_filter( |
| 173 |
$this->fields, |
| 174 |
static fn (PivotField $field): bool => $field->getAxis() === $axis |
| 175 |
) |
| 176 |
); |
| 177 |
} |
| 178 |
public function __toString(): string |
| 179 |
{ |
| 180 |
return $this->name; |
| 181 |
} |
| 182 |
} |
| 183 |
|