Repository navigation
Expand file tree
/
Copy pathGroupPartitions.php
More file actions
129 lines (115 loc) · 4.49 KB
/
Copy pathGroupPartitions.php
File metadata and controls
129 lines (115 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
declare(strict_types=1);
namespace NyonCode\WireTable\Support;
use Closure;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
/**
* One page of records, split into its groups, in page order.
*
* Group subtotals render once per group, and each of them needs that group's
* rows. Filtering the whole page per group is O(groups × page size) for an
* answer that never changes within a render, so the page is split once and
* looked up by value.
*
* ## The key is normalised, and that is the whole trick
*
* The grouping value is read through the table's comparison key rather than off
* the model, because a cast column hands back a **fresh object per record** — a
* `date` cast is a new Carbon every time, and a strict compare of two of them is
* false even for the same day. Group by such a column with the raw value and
* every single row forms its own group, each with its own one-row subtotal. The
* caller is handed the same normalised key by the view, so the lookup matches.
*
* ## It remembers which page it split
*
* A partitioning is only true of the record set it was built from. That used to
* be maintained by hand — five places drop the page memo, and three of them
* remembered to drop the partitions too. The two that did not were `setPage()`
* and `setTableCursor()`, so paging inside one request left every group subtotal
* describing the previous page: the group on screen totalled zero, and a group
* that was no longer there still showed its figure.
*
* So the identity of the source is carried here instead of being asserted by
* callers. {@see describes()} answers whether this partitioning is still about a
* given record set, and the reference is held rather than an id compared — a
* freed collection's `spl_object_id` can be handed to its successor, and the
* records are already referenced by the partitions anyway.
*/
final class GroupPartitions
{
/**
* @param array<int, array{value: mixed, records: Collection<int, Model>}> $partitions
*/
private function __construct(
private readonly mixed $source,
private readonly array $partitions,
) {}
/**
* Split a page by its normalised group key, preserving page order both
* within a group and between groups.
*
* @param iterable<int, Model> $records the page, in the order it renders
* @param Closure(Model): mixed $key the table's normalised comparison key
* @param mixed $source the record set this page came from, for {@see describes()}
*/
public static function of(iterable $records, Closure $key, mixed $source = null): self
{
/** @var array<int, array{value: mixed, records: Collection<int, Model>}> $partitions */
$partitions = [];
foreach ($records as $record) {
$value = $key($record);
$matched = false;
// 'records' is a Collection object, so push() mutates it in place.
foreach ($partitions as $partition) {
if ($partition['value'] === $value) {
$partition['records']->push($record);
$matched = true;
break;
}
}
if (! $matched) {
$partitions[] = ['value' => $value, 'records' => collect([$record])];
}
}
return new self($source, $partitions);
}
/**
* Whether this partitioning is still about `$records` — the same object the
* page memo is currently holding, not merely an equal one.
*/
public function describes(mixed $records): bool
{
return $this->source === $records;
}
/**
* The rows of one group, by its normalised key.
*
* A value no group on this page carries returns nothing rather than
* guessing: a subtotal for an absent group is an empty subtotal.
*
* @return Collection<int, Model>
*/
public function get(mixed $groupValue): Collection
{
foreach ($this->partitions as $partition) {
if ($partition['value'] === $groupValue) {
return $partition['records'];
}
}
return collect();
}
/**
* The group keys on this page, in the order they render.
*
* @return array<int, mixed>
*/
public function values(): array
{
return array_map(fn (array $partition): mixed => $partition['value'], $this->partitions);
}
public function count(): int
{
return count($this->partitions);
}
}