| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Worksheet\PivotTable; |
| 4 |
|
| 5 |
/** |
| 6 |
* Describes how a single source (cache) field is grouped in a pivot table. |
| 7 |
* |
| 8 |
* Two kinds of grouping are supported: |
| 9 |
* |
| 10 |
* - Numeric range grouping: values are bucketed into fixed-width intervals |
| 11 |
* between a start and end number (e.g. 0-100, 100-200, ...). |
| 12 |
* - Date grouping: date/time values are grouped by one or more calendar parts |
| 13 |
* (years, quarters, months, ...). |
| 14 |
* |
| 15 |
* The grouping is emitted into the pivot cache definition; when the workbook is |
| 16 |
* opened, the spreadsheet application applies it while refreshing the pivot. |
| 17 |
* |
| 18 |
* @see PivotTableBuilder |
| 19 |
*/ |
| 20 |
class PivotFieldGroup |
| 21 |
{ |
| 22 |
const TYPE_NUMERIC = 'numeric'; |
| 23 |
const TYPE_DATE = 'date'; |
| 24 |
|
| 25 |
// Date grouping units, matching the rangePr@groupBy attribute values. |
| 26 |
const GROUP_BY_SECONDS = 'seconds'; |
| 27 |
const GROUP_BY_MINUTES = 'minutes'; |
| 28 |
const GROUP_BY_HOURS = 'hours'; |
| 29 |
const GROUP_BY_DAYS = 'days'; |
| 30 |
const GROUP_BY_MONTHS = 'months'; |
| 31 |
const GROUP_BY_QUARTERS = 'quarters'; |
| 32 |
const GROUP_BY_YEARS = 'years'; |
| 33 |
|
| 34 |
private string $type; |
| 35 |
|
| 36 |
/** |
| 37 |
* Numeric grouping: the width of each bucket. |
| 38 |
*/ |
| 39 |
private float $interval = 1.0; |
| 40 |
|
| 41 |
private ?float $startNum = null; |
| 42 |
|
| 43 |
private ?float $endNum = null; |
| 44 |
|
| 45 |
/** |
| 46 |
* Date grouping: the calendar units to group by, in coarse-to-fine order. |
| 47 |
* |
| 48 |
* @var string[] |
| 49 |
*/ |
| 50 |
private array $groupBy = []; |
| 51 |
|
| 52 |
private ?string $startDate = null; |
| 53 |
|
| 54 |
private ?string $endDate = null; |
| 55 |
|
| 56 |
private function __construct(string $type) |
| 57 |
{ |
| 58 |
$this->type = $type; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Create a numeric range grouping (buckets of $interval width). |
| 63 |
*/ |
| 64 |
public static function numeric(float $interval, ?float $startNum = null, ?float $endNum = null): self |
| 65 |
{ |
| 66 |
$group = new self(self::TYPE_NUMERIC); |
| 67 |
$group->interval = $interval; |
| 68 |
$group->startNum = $startNum; |
| 69 |
$group->endNum = $endNum; |
| 70 |
|
| 71 |
return $group; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Create a date grouping by one or more calendar units. |
| 76 |
* |
| 77 |
* @param string|string[] $groupBy one or more GROUP_BY_* constants |
| 78 |
*/ |
| 79 |
public static function date($groupBy, ?string $startDate = null, ?string $endDate = null): self |
| 80 |
{ |
| 81 |
$group = new self(self::TYPE_DATE); |
| 82 |
$group->groupBy = is_array($groupBy) ? array_values($groupBy) : [$groupBy]; |
| 83 |
$group->startDate = $startDate; |
| 84 |
$group->endDate = $endDate; |
| 85 |
|
| 86 |
return $group; |
| 87 |
} |
| 88 |
|
| 89 |
public function getType(): string |
| 90 |
{ |
| 91 |
return $this->type; |
| 92 |
} |
| 93 |
|
| 94 |
public function isNumeric(): bool |
| 95 |
{ |
| 96 |
return $this->type === self::TYPE_NUMERIC; |
| 97 |
} |
| 98 |
|
| 99 |
public function isDate(): bool |
| 100 |
{ |
| 101 |
return $this->type === self::TYPE_DATE; |
| 102 |
} |
| 103 |
|
| 104 |
public function getInterval(): float |
| 105 |
{ |
| 106 |
return $this->interval; |
| 107 |
} |
| 108 |
|
| 109 |
public function getStartNum(): ?float |
| 110 |
{ |
| 111 |
return $this->startNum; |
| 112 |
} |
| 113 |
|
| 114 |
public function getEndNum(): ?float |
| 115 |
{ |
| 116 |
return $this->endNum; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* @return string[] |
| 121 |
*/ |
| 122 |
public function getGroupBy(): array |
| 123 |
{ |
| 124 |
return $this->groupBy; |
| 125 |
} |
| 126 |
|
| 127 |
public function getStartDate(): ?string |
| 128 |
{ |
| 129 |
return $this->startDate; |
| 130 |
} |
| 131 |
|
| 132 |
public function getEndDate(): ?string |
| 133 |
{ |
| 134 |
return $this->endDate; |
| 135 |
} |
| 136 |
} |
| 137 |
|