-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathAbstractFormComponent.php
More file actions
202 lines (178 loc) · 6.74 KB
/
AbstractFormComponent.php
File metadata and controls
202 lines (178 loc) · 6.74 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
declare(strict_types=1);
namespace Relaticle\CustomFields\Filament\Integration\Base;
use Filament\Forms\Components\Field;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Relaticle\CustomFields\Contracts\FormComponentInterface;
use Relaticle\CustomFields\Enums\CustomFieldsFeature;
use Relaticle\CustomFields\FeatureSystem\FeatureManager;
use Relaticle\CustomFields\Models\CustomField;
use Relaticle\CustomFields\Services\ValidationService;
use Relaticle\CustomFields\Services\Visibility\CoreVisibilityLogicService;
use Relaticle\CustomFields\Services\Visibility\FrontendVisibilityService;
/**
* Abstract base class for form field components.
*
* Eliminates duplication across 18+ component classes by providing
* common structure and delegating to FieldConfigurator for shared logic.
*
* Each concrete component only needs to implement createField() to specify
* the Filament component type and its basic configuration.
*/
abstract readonly class AbstractFormComponent implements FormComponentInterface
{
public function __construct(
protected ValidationService $validationService,
protected CoreVisibilityLogicService $coreVisibilityLogic,
protected FrontendVisibilityService $frontendVisibilityService
) {}
/**
* Create and configure a field component.
*
* @param array<string> $dependentFieldCodes
* @param Collection<int, CustomField>|null $allFields
*/
public function make(CustomField $customField, array $dependentFieldCodes = [], ?Collection $allFields = null): Field
{
$field = $this->create($customField);
$allFields ??= collect();
return $this->configure($field, $customField, $allFields, $dependentFieldCodes);
}
protected function configure(
Field $field,
CustomField $customField,
Collection $allFields,
array $dependentFieldCodes
): Field {
return $field
->name($customField->getFieldName())
->label($customField->name)
->afterStateHydrated(
fn (mixed $component, mixed $state, mixed $record): mixed => $component->state(
$this->transformHydratedValue(
$this->getFieldValue($customField, $state, $record),
$customField
)
)
)
->dehydrated(
fn (mixed $state): bool => ! FeatureManager::isEnabled(CustomFieldsFeature::FIELD_CONDITIONAL_VISIBILITY) ||
$this->coreVisibilityLogic->shouldAlwaysSave($customField) ||
filled($state)
)
->required($this->validationService->isRequired($customField))
->rules(fn (Field $component): array => $this->getFieldValidationRules(
$customField,
$component->getRecord()?->getKey()
))
->columnSpan(
FeatureManager::isEnabled(CustomFieldsFeature::UI_FIELD_WIDTH_CONTROL)
? $customField->width->getSpanValue()
: 12
)
->when(
FeatureManager::isEnabled(CustomFieldsFeature::FIELD_CONDITIONAL_VISIBILITY) &&
$this->hasVisibilityConditions($customField),
fn (Field $field): Field => $this->applyVisibility(
$field,
$customField,
$allFields
)
)
->when(
FeatureManager::isEnabled(CustomFieldsFeature::FIELD_CONDITIONAL_VISIBILITY) &&
filled($dependentFieldCodes),
fn (Field $field): Field => $field->live()
);
}
private function getFieldValue(
CustomField $customField,
mixed $state,
mixed $record
): mixed {
$recordValue = $record?->getCustomFieldValue($customField);
if ($recordValue !== null) {
$value = $recordValue;
} elseif ($state !== null) {
$value = $state;
} else {
$value = $customField->isMultiChoiceField() ? [] : null;
}
if ($value instanceof Carbon) {
return $value->format($customField->isDateField() ? 'Y-m-d' : 'Y-m-d H:i:s');
}
return $value;
}
/**
* Transform the hydrated value before setting component state.
*
* Override this method in subclasses to transform stored values
* into the format expected by the component (e.g., E.164 to objects).
*/
protected function transformHydratedValue(mixed $value, CustomField $customField): mixed
{
return $value;
}
private function hasVisibilityConditions(CustomField $customField): bool
{
return $this->coreVisibilityLogic->hasVisibilityConditions($customField);
}
private function applyVisibility(
Field $field,
CustomField $customField,
Collection $allFields
): Field {
$jsExpression = $this->frontendVisibilityService->buildVisibilityExpression(
$customField,
$allFields
);
if (blank($jsExpression) || $jsExpression === '0') {
return $field;
}
return $field->live()->visibleJs($jsExpression);
}
/** @return array<int, mixed> */
protected function getFieldValidationRules(CustomField $customField, string|int|null $ignoreEntityId = null): array
{
return $this->validationService->getValidationRules($customField, $ignoreEntityId);
}
/**
* Apply settings dynamically to any Filament component
*/
protected function applySettingsToComponent(Field $component, array $settings): Field
{
foreach ($settings as $method => $value) {
if ($value === null) {
continue;
}
if (! method_exists($component, $method)) {
continue;
}
// For boolean methods, only call if true
if (is_bool($value) && ! $value) {
continue;
}
$component->$method($value);
}
return $component;
}
/**
* Get options from custom field's configured options.
*
* @return array<int|string, string>
*/
protected function getCustomFieldOptions(CustomField $customField): array
{
return $customField->options->pluck('name', 'id')->all();
}
/**
* Create the specific Filament field component.
*
* Concrete implementations should create the appropriate Filament component
* (TextInput, Select, etc.) with field-specific configuration.
*
* Made public to allow composition patterns (like MultiSelectComponent).
*/
abstract public function create(CustomField $customField): Field;
}