Repository navigation
Expand file tree
/
Copy pathSettingsGroupValues.php
More file actions
148 lines (128 loc) · 5.16 KB
/
Copy pathSettingsGroupValues.php
File metadata and controls
148 lines (128 loc) · 5.16 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
declare(strict_types=1);
namespace NyonCode\WireModuleSettings\Support;
use Illuminate\Contracts\Support\Htmlable;
use NyonCode\WireModuleSettings\Contracts\ConfirmsSettingsSave;
use NyonCode\WireModuleSettings\Contracts\ExtendsSettingsScreen;
use NyonCode\WireModuleSettings\Contracts\RendersSettingsScreen;
use NyonCode\WireModuleSettings\Contracts\SettingsGroup;
use NyonCode\WireModuleSettings\Contracts\SpansSettingsGroups;
use NyonCode\WireModuleSettings\Contracts\TransformsSettings;
use NyonCode\WireModuleSettings\Contracts\ValidatesSettings;
/**
* A settings group's values on their way between storage and its form.
*
* The one owner of what a group's optional contracts change about that trip —
* where its keys are stored ({@see SpansSettingsGroups}), how they are shaped
* for the form and back ({@see TransformsSettings}), what is checked across
* fields ({@see ValidatesSettings}) — so the screen calls three methods and does
* not grow a branch per contract, and a group without any of them is exactly the
* group it was before they existed.
*/
final class SettingsGroupValues
{
/**
* The form's state for a group: stored values over declared defaults,
* gathered from every storage group it spans, shaped for the form.
*
* @param class-string<SettingsGroup> $class
* @return array<string, mixed>
*/
public static function load(string $class): array
{
$values = Settings::all($class::group());
foreach (self::elsewhere($class) as $storage => $keys) {
$stored = Settings::all($storage);
// Only what is stored there moves over the group's own answer: a key
// nobody wrote yet keeps the default the group declared for it,
// rather than turning into null because its storage group has none.
foreach ($keys as $key) {
if (array_key_exists($key, $stored)) {
$values[$key] = $stored[$key];
}
}
}
return is_a($class, TransformsSettings::class, true) ? $class::fromStorage($values) : $values;
}
/**
* Messages for the fields that fail the group's own cross-field rule.
*
* @param class-string<SettingsGroup> $class
* @param array<string, mixed> $data
* @return array<string, string>
*/
public static function problems(string $class, array $data): array
{
return is_a($class, ValidatesSettings::class, true) ? $class::validateSettings($data) : [];
}
/**
* Store a group's validated form state: shaped for storage, split across
* the storage groups it spans, written in one transaction.
*
* @param class-string<SettingsGroup> $class
* @param array<string, mixed> $data
* @return array<string, array<string, mixed>> What was written, by storage group.
*/
public static function save(string $class, array $data): array
{
$values = is_a($class, TransformsSettings::class, true) ? $class::toStorage($data) : $data;
$groups = [$class::group() => $values];
foreach (self::elsewhere($class) as $storage => $keys) {
foreach ($keys as $key) {
if (array_key_exists($key, $values)) {
$groups[$storage][$key] = $values[$key];
unset($groups[$class::group()][$key]);
}
}
}
Settings::fillMany($groups);
return $groups;
}
/**
* What the group shows under its form, from the form's current state.
*
* @param class-string<SettingsGroup> $class
* @param array<string, mixed> $data
*/
public static function extension(string $class, array $data): ?Htmlable
{
return is_a($class, ExtendsSettingsScreen::class, true) ? $class::screenExtension($data) : null;
}
/**
* The Livewire component drawn in place of the form, when the group has one.
*
* @param class-string<SettingsGroup> $class
*/
public static function component(string $class): ?string
{
return is_a($class, RendersSettingsScreen::class, true) ? $class::component() : null;
}
/**
* The group's own confirmation, when it words one.
*
* @param class-string<SettingsGroup> $class
*/
public static function savedMessage(string $class): ?string
{
return is_a($class, ConfirmsSettingsSave::class, true) ? $class::savedMessage() : null;
}
/**
* Keys the group keeps outside its own storage, by the storage group holding them.
*
* Its own storage name listed here is ignored rather than honoured: the
* keys are in it already, and treating it as "elsewhere" would move them out
* of the group and back in again.
*
* @param class-string<SettingsGroup> $class
* @return array<string, list<string>>
*/
private static function elsewhere(string $class): array
{
if (! is_a($class, SpansSettingsGroups::class, true)) {
return [];
}
$storage = $class::storage();
unset($storage[$class::group()]);
return array_map(static fn (array $keys): array => array_map('strval', $keys), $storage);
}
}