Skip to content

Commit 4805454

Browse files
authored
feat: check on empty array key values (#12)
* Check on empty array key values * PHPStan issues * Fix styling --------- Co-authored-by: Baspa <[email protected]>
1 parent c7d48ab commit 4805454

File tree

7 files changed

+42
-63
lines changed

7 files changed

+42
-63
lines changed

src/Concerns/CanMapDynamicFields.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,11 @@ protected function mutateBeforeSave(array $data): array
7979
return $data;
8080
}
8181

82-
$values = $data[$this->record?->valueColumn];
82+
$values = isset($data[$this->record?->valueColumn]) ? $data[$this->record?->valueColumn] : [];
83+
84+
if (empty($values)) {
85+
return $data;
86+
}
8387

8488
$fieldsFromValues = array_keys($values);
8589

src/Fields.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class Fields
88
{
9-
private static array $fields = [];
9+
protected static array $fields = [];
1010

1111
public static function registerField(string $className): void
1212
{

src/Fields/Repeater.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Backstage\Fields\Contracts\FieldContract;
99
use Backstage\Fields\Enums\Field as FieldEnum;
1010
use Backstage\Fields\Facades\Fields;
11-
use Backstage\Fields\Fields\Select as FieldsSelect;
1211
use Backstage\Fields\Models\Field;
1312
use Filament\Forms;
1413
use Filament\Forms\Components\Hidden;
@@ -28,21 +27,6 @@ class Repeater extends Base implements FieldContract
2827
use HasFieldTypeResolver;
2928
use HasOptions;
3029

31-
private const FIELD_TYPE_MAP = [
32-
'text' => Text::class,
33-
'textarea' => Textarea::class,
34-
'rich-editor' => RichEditor::class,
35-
'repeater' => Repeater::class,
36-
'select' => FieldsSelect::class,
37-
'checkbox' => Checkbox::class,
38-
'checkbox-list' => CheckboxList::class,
39-
'key-value' => KeyValue::class,
40-
'radio' => Radio::class,
41-
'toggle' => Toggle::class,
42-
'color' => Color::class,
43-
'date-time' => DateTime::class,
44-
];
45-
4630
public static function getDefaultConfig(): array
4731
{
4832
return [

src/Fields/RichEditor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace Backstage\Fields\Fields;
44

5-
use Backstage\Enums\ToolbarButton;
65
use Backstage\Fields\Contracts\FieldContract;
6+
use Backstage\Fields\Enums\ToolbarButton;
77
use Backstage\Fields\Models\Field;
88
use Backstage\Fields\Services\ContentCleaningService;
99
use Filament\Forms;

src/Filament/RelationManagers/FieldsRelationManager.php

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -118,43 +118,6 @@ public function form(Form $form): Form
118118
]);
119119
}
120120

121-
private function formatCustomFields(array $fields): array
122-
{
123-
return collect($fields)->mapWithKeys(function ($field, $key) {
124-
$parts = explode('\\', $field);
125-
$lastPart = end($parts);
126-
$formattedName = Str::title(Str::snake($lastPart, ' '));
127-
128-
return [$key => $formattedName];
129-
})->toArray();
130-
}
131-
132-
private function initializeDefaultConfig(string $fieldType): array
133-
{
134-
$className = 'Backstage\\Fields\\Fields\\' . Str::studly($fieldType);
135-
136-
if (! class_exists($className)) {
137-
return [];
138-
}
139-
140-
$fieldInstance = app($className);
141-
142-
return $fieldInstance::getDefaultConfig();
143-
}
144-
145-
private function initializeCustomConfig(string $fieldType): array
146-
{
147-
$className = Fields::getFields()[$fieldType] ?? null;
148-
149-
if (! class_exists($className)) {
150-
return [];
151-
}
152-
153-
$fieldInstance = app($className);
154-
155-
return $fieldInstance::getDefaultConfig();
156-
}
157-
158121
public function table(Table $table): Table
159122
{
160123
return $table
@@ -185,13 +148,13 @@ public function table(Table $table): Table
185148
->slideOver()
186149
->mutateFormDataUsing(function (array $data) {
187150

188-
$key = $this->ownerRecord->getKeyName() ?? 'id';
151+
$key = $this->ownerRecord->getKeyName();
189152

190153
return [
191154
...$data,
192155
'position' => Field::where('model_key', $key)->get()->max('position') + 1,
193156
'model_type' => 'setting',
194-
'model_key' => $this->ownerRecord->slug,
157+
'model_key' => $this->ownerRecord->getKey(),
195158
];
196159
})
197160
->after(function (Component $livewire) {
@@ -203,7 +166,7 @@ public function table(Table $table): Table
203166
->slideOver()
204167
->mutateRecordDataUsing(function (array $data) {
205168

206-
$key = $this->ownerRecord->getKeyName() ?? 'id';
169+
$key = $this->ownerRecord->getKeyName();
207170

208171
return [
209172
...$data,
@@ -222,7 +185,7 @@ public function table(Table $table): Table
222185
->hasColumn($this->ownerRecord->getTable(), $record->valueColumn)
223186
) {
224187

225-
$key = $this->ownerRecord->getKeyName() ?? 'id';
188+
$key = $this->ownerRecord->getKeyName();
226189

227190
$this->ownerRecord->update([
228191
$record->valueColumn => collect($this->ownerRecord->{$record->valueColumn})->forget($record->{$key})->toArray(),

src/Models/Field.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,23 @@
1111
use Illuminate\Support\Facades\Config;
1212
use Staudenmeir\LaravelAdjacencyList\Eloquent\HasRecursiveRelationships;
1313

14+
/**
15+
* @property string $ulid
16+
* @property string|null $parent_ulid
17+
* @property string $model_type
18+
* @property string $model_key
19+
* @property string $slug
20+
* @property string $name
21+
* @property string $field_type
22+
* @property array<string, mixed>|null $config
23+
* @property int $position
24+
* @property string|null $group
25+
* @property \Carbon\Carbon $created_at
26+
* @property \Carbon\Carbon $updated_at
27+
* @property-read \Illuminate\Database\Eloquent\Model|null $model
28+
* @property-read \Illuminate\Database\Eloquent\Collection<int, Field> $children
29+
* @property-read \Illuminate\Database\Eloquent\Model|null $tenant
30+
*/
1431
class Field extends Model
1532
{
1633
use HasPackageFactory;

src/Services/FieldInspectionService.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
use Illuminate\Support\Str;
88
use ReflectionClass;
99
use ReflectionMethod;
10+
use ReflectionNamedType;
1011
use ReflectionProperty;
12+
use ReflectionType;
1113

1214
class FieldInspectionService implements FieldInspector
1315
{
@@ -63,7 +65,7 @@ private function getMethodsDetails(ReflectionClass $reflection): array
6365
'visibility' => $this->getVisibility($method),
6466
'static' => $method->isStatic(),
6567
'parameters' => $this->getParametersDetails($method),
66-
'returnType' => $method->getReturnType() ? $method->getReturnType()->getName() : null,
68+
'returnType' => $this->getTypeName($method->getReturnType()),
6769
'docComment' => $method->getDocComment() ?: null,
6870
];
6971
}
@@ -78,7 +80,7 @@ private function getPropertiesDetails(ReflectionClass $reflection): array
7880
$properties[$property->getName()] = [
7981
'visibility' => $this->getVisibility($property),
8082
'static' => $property->isStatic(),
81-
'type' => $property->getType() ? $property->getType()->getName() : null,
83+
'type' => $this->getTypeName($property->getType()),
8284
'docComment' => $property->getDocComment() ?: null,
8385
'defaultValue' => $this->getPropertyDefaultValue($property),
8486
];
@@ -92,7 +94,7 @@ private function getParametersDetails(ReflectionMethod $method): array
9294
$parameters = [];
9395
foreach ($method->getParameters() as $param) {
9496
$parameters[$param->getName()] = [
95-
'type' => $param->getType() ? $param->getType()->getName() : null,
97+
'type' => $this->getTypeName($param->getType()),
9698
'hasDefaultValue' => $param->isDefaultValueAvailable(),
9799
'defaultValue' => $param->isDefaultValueAvailable() ? $param->getDefaultValue() : null,
98100
'isVariadic' => $param->isVariadic(),
@@ -103,6 +105,15 @@ private function getParametersDetails(ReflectionMethod $method): array
103105
return $parameters;
104106
}
105107

108+
private function getTypeName(?ReflectionType $type): ?string
109+
{
110+
if ($type instanceof ReflectionNamedType) {
111+
return $type->getName();
112+
}
113+
114+
return null;
115+
}
116+
106117
private function getVisibility($reflection): string
107118
{
108119
if ($reflection->isPrivate()) {

0 commit comments

Comments
 (0)