• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PHPOffice / PhpSpreadsheet / 30316250040

28 Jul 2026 12:05AM UTC coverage: 97.119% (-0.05%) from 97.169%
30316250040

Pull #4942

github

web-flow
Merge 4e0bce88d into 540dfb463
Pull Request #4942: Feature/pivot table read

787 of 836 new or added lines in 12 files covered. (94.14%)

49176 of 50635 relevant lines covered (97.12%)

382.11 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.5
/src/PhpSpreadsheet/Worksheet/PivotTable/PivotFieldGroup.php
1
<?php
2

3
namespace 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)
3✔
57
    {
58
        $this->type = $type;
3✔
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
2✔
65
    {
66
        $group = new self(self::TYPE_NUMERIC);
2✔
67
        $group->interval = $interval;
2✔
68
        $group->startNum = $startNum;
2✔
69
        $group->endNum = $endNum;
2✔
70

71
        return $group;
2✔
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(array|string $groupBy, ?string $startDate = null, ?string $endDate = null): self
2✔
80
    {
81
        $group = new self(self::TYPE_DATE);
2✔
82
        $group->groupBy = is_array($groupBy) ? array_values($groupBy) : [$groupBy];
2✔
83
        $group->startDate = $startDate;
2✔
84
        $group->endDate = $endDate;
2✔
85

86
        return $group;
2✔
87
    }
88

NEW
89
    public function getType(): string
×
90
    {
NEW
91
        return $this->type;
×
92
    }
93

NEW
94
    public function isNumeric(): bool
×
95
    {
NEW
96
        return $this->type === self::TYPE_NUMERIC;
×
97
    }
98

99
    public function isDate(): bool
3✔
100
    {
101
        return $this->type === self::TYPE_DATE;
3✔
102
    }
103

104
    public function getInterval(): float
2✔
105
    {
106
        return $this->interval;
2✔
107
    }
108

109
    public function getStartNum(): ?float
2✔
110
    {
111
        return $this->startNum;
2✔
112
    }
113

114
    public function getEndNum(): ?float
2✔
115
    {
116
        return $this->endNum;
2✔
117
    }
118

119
    /**
120
     * @return string[]
121
     */
122
    public function getGroupBy(): array
2✔
123
    {
124
        return $this->groupBy;
2✔
125
    }
126

127
    public function getStartDate(): ?string
2✔
128
    {
129
        return $this->startDate;
2✔
130
    }
131

132
    public function getEndDate(): ?string
2✔
133
    {
134
        return $this->endDate;
2✔
135
    }
136
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc