Repository navigation
Expand file tree
/
Copy pathMobileCardConfig.php
More file actions
89 lines (75 loc) · 2.24 KB
/
Copy pathMobileCardConfig.php
File metadata and controls
89 lines (75 loc) · 2.24 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
<?php
declare(strict_types=1);
namespace NyonCode\WireTable\Support;
/**
* The fluent form of a mobile card, naming columns by name:
*
* ->mobileCard(fn (MobileCardConfig $card) => $card
* ->title('number')
* ->subtitle('customer')
* ->metric('total')
* ->meta(['status', 'due_at']))
*
* Kept apart from {@see MobileCard} because that one is the resolved result —
* columns already picked — while this is the declaration a table writes. Any
* slot left unnamed is derived.
*/
final class MobileCardConfig
{
/** @var array<string, MobileSlot> column name => slot */
private array $assignments = [];
public function title(string $column): static
{
return $this->assign($column, MobileSlot::Title);
}
public function subtitle(string $column): static
{
return $this->assign($column, MobileSlot::Subtitle);
}
public function metric(string $column): static
{
return $this->assign($column, MobileSlot::Metric);
}
/**
* @param string|array<int, string> $columns
*/
public function meta(string|array $columns): static
{
foreach ((array) $columns as $column) {
$this->assign($column, MobileSlot::Meta);
}
return $this;
}
/**
* Push a column down into the label/value grid, out of the header.
*
* @param string|array<int, string> $columns
*/
public function detail(string|array $columns): static
{
foreach ((array) $columns as $column) {
$this->assign($column, MobileSlot::Detail);
}
return $this;
}
/**
* @return array<string, MobileSlot>
*/
public function assignments(): array
{
return $this->assignments;
}
private function assign(string $column, MobileSlot $slot): static
{
// A singular slot holds one column: naming a second one replaces the
// first rather than quietly keeping both.
if ($slot->isSingular()) {
$this->assignments = array_filter(
$this->assignments,
fn (MobileSlot $assigned): bool => $assigned !== $slot,
);
}
$this->assignments[$column] = $slot;
return $this;
}
}