• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

luttje / filament-user-attributes / 7240605279

17 Dec 2023 07:38PM UTC coverage: 76.074% (-0.4%) from 76.49%
7240605279

push

github

luttje
fix class name mistake

6 of 7 new or added lines in 1 file covered. (85.71%)

25 existing lines in 3 files now uncovered.

1151 of 1513 relevant lines covered (76.07%)

31.05 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.89
/src/Filament/UserAttributeComponentFactoryRegistry.php
1
<?php
2

3
namespace Luttje\FilamentUserAttributes\Filament;
4

5
use Closure;
6
use Filament\Forms;
7
use Filament\Forms\Get;
8
use Luttje\FilamentUserAttributes\Models\UserAttributeConfig;
9

10
class UserAttributeComponentFactoryRegistry
11
{
12
    protected static $factories = [];
13

14
    public static function register(string $type, string $factory): void
292✔
15
    {
16
        static::$factories[$type] = $factory;
292✔
17
    }
18

19
    public static function getFactory(string $type): UserAttributeComponentFactoryInterface
16✔
20
    {
21
        if (!isset(static::$factories[$type])) {
16✔
22
            throw new \Exception("Factory for type {$type} not registered.");
×
23
        }
24

25
        return new static::$factories[$type]();
16✔
26
    }
27

28
    public static function getRegisteredTypes(): array
36✔
29
    {
30
        return array_keys(static::$factories);
36✔
31
    }
32

33
    public static function getConfigurationSchemas(UserAttributeConfig $configModel): array
36✔
34
    {
35
        $schemas = [];
36✔
36

37
        $schemas[] = Forms\Components\Fieldset::make('common')
36✔
38
            ->label(ucfirst(__('filament-user-attributes::user-attributes.common')))
36✔
39
            ->schema(function () {
36✔
40
                return [
36✔
41
                    // TODO: Make configs for these
42
                    Forms\Components\TextInput::make('name')
36✔
43
                        ->label(ucfirst(__('filament-user-attributes::user-attributes.attributes.name')))
36✔
44
                        ->required()
36✔
45
                        ->rules([
36✔
46
                            function (Get $get) {
36✔
47
                                $otherNames = $get('../../config.*.name');
36✔
48

49
                                return function (string $attribute, $value, Closure $fail) use ($otherNames) {
36✔
50
                                    $userAttributeConfigs = collect($otherNames)->filter(function ($item) use ($value) {
36✔
51
                                        return $item === $value;
36✔
52
                                    });
36✔
53

54
                                    if ($userAttributeConfigs->count() > 1) {
36✔
55
                                        $fail(__('filament-user-attributes::user-attributes.name_already_exists'));
×
56
                                    }
57
                                };
36✔
58
                            },
36✔
59
                        ])
36✔
60
                        ->readOnlyOn('edit')
36✔
61
                        ->helperText(ucfirst(__('filament-user-attributes::user-attributes.name_help')))
36✔
62
                        ->maxLength(255),
36✔
63
                    Forms\Components\Checkbox::make('required')
36✔
64
                        ->label(ucfirst(__('filament-user-attributes::user-attributes.attributes.required'))),
36✔
65
                    Forms\Components\TextInput::make('label')
36✔
66
                        ->label(ucfirst(__('filament-user-attributes::user-attributes.attributes.label')))
36✔
67
                        ->required()
36✔
68
                        ->maxLength(255),
36✔
69
                    Forms\Components\Select::make('type')
36✔
70
                        ->options(array_combine(static::getRegisteredTypes(), static::getRegisteredTypes()))
36✔
71
                        ->label(ucfirst(__('filament-user-attributes::user-attributes.attributes.type')))
36✔
72
                        ->required()
36✔
73
                        ->live(),
36✔
74
                ];
36✔
75
            });
36✔
76

77
        foreach (static::$factories as $type => $factoryClass) {
36✔
78
            /** @var UserAttributeComponentFactoryInterface */
79
            $factory = new $factoryClass();
36✔
80
            $factorySchema = $factory->makeConfigurationSchema();
36✔
81

82
            $schemas[] = Forms\Components\Fieldset::make('customizations_for_' . $type)
36✔
83
                ->label(ucfirst(__('filament-user-attributes::user-attributes.customizations_for', ['type' => $type])))
36✔
84
                ->statePath('customizations')
36✔
85
                ->schema($factorySchema)
36✔
86
                ->mutateDehydratedStateUsing(function (Get $get, $state) use ($type, $factorySchema) {
36✔
87
                    if ($get('type') !== $type) {
32✔
UNCOV
88
                        return null;
×
89
                    }
90

91
                    // Unset all names that are not in the schema
92
                    // TODO: Why doesn't filament just ignore things that are hidden or disabled?
93
                    $names = collect($factorySchema)->map(function ($item) {
32✔
94
                        return $item->getName();
32✔
95
                    });
32✔
96
                    $state = collect($state)
32✔
97
                        ->filter(function ($value, $name) use ($names) {
32✔
98
                            return $names->contains($name);
32✔
99
                        })
32✔
100
                        ->toArray();
32✔
101

102
                    return $state;
32✔
103
                })
36✔
104
                ->hidden(fn (Get $get) => $get('type') !== $type || count($factorySchema) === 0)
36✔
105
                ->disabled(fn (Get $get) => $get('type') !== $type || count($factorySchema) === 0);
36✔
106
        }
107

108
        $schemas[] = Forms\Components\Fieldset::make('ordering')
36✔
109
            ->label(ucfirst(__('filament-user-attributes::user-attributes.ordering')))
36✔
110
            ->schema([
36✔
111
                Forms\Components\Fieldset::make('ordering_form')
36✔
112
                    ->label(ucfirst(__('filament-user-attributes::user-attributes.ordering_form')))
36✔
113
                    ->schema(function () use ($configModel) {
36✔
114
                        return [
36✔
115
                            Forms\Components\Select::make('order_position_form')
36✔
116
                                ->options([
36✔
117
                                    'before' => __('filament-user-attributes::user-attributes.attributes.order_position_before'),
36✔
118
                                    'after' => __('filament-user-attributes::user-attributes.attributes.order_position_after'),
36✔
119
                                    'hidden' => __('filament-user-attributes::user-attributes.attributes.order_position_hidden'),
36✔
120
                                ])
36✔
121
                                ->placeholder(ucfirst(__('filament-user-attributes::user-attributes.attributes.order_sibling_at_end')))
36✔
122
                                ->selectablePlaceholder()
36✔
123
                                ->live()
36✔
124
                                ->label(ucfirst(__('filament-user-attributes::user-attributes.attributes.order_position')))
36✔
125
                                ->required(function (Get $get) {
36✔
126
                                    $sibling = $get('order_sibling_form');
36✔
127
                                    return $sibling !== null && $sibling !== '';
36✔
128
                                }),
36✔
129
                            Forms\Components\Select::make('order_sibling_form')
36✔
130
                                ->selectablePlaceholder()
36✔
131
                                ->live()
36✔
132
                                ->disabled(function (Get $get) {
36✔
133
                                    return $get('order_position_form') === 'hidden'
36✔
134
                                        || $get('order_position_form') == null;
36✔
135
                                })
36✔
136
                                ->placeholder(ucfirst(__('filament-user-attributes::user-attributes.select_sibling')))
36✔
137
                                ->options(function () use ($configModel) {
36✔
138
                                    $fields = $configModel->resource_type::getFieldsForOrdering();
36✔
139
                                    $fields = array_combine(array_column($fields, 'label'), array_column($fields, 'label'));
36✔
140
                                    return $fields;
36✔
141
                                })
36✔
142
                                ->helperText(ucfirst(__('filament-user-attributes::user-attributes.order_sibling_help')))
36✔
143
                                ->label(ucfirst(__('filament-user-attributes::user-attributes.attributes.order_sibling'))),
36✔
144
                        ];
36✔
145
                    }),
36✔
146
                Forms\Components\Fieldset::make('ordering_table')
36✔
147
                    ->label(ucfirst(__('filament-user-attributes::user-attributes.ordering_table')))
36✔
148
                    ->schema(function () use ($configModel) {
36✔
149
                        return [
36✔
150
                            Forms\Components\Select::make('order_position_table')
36✔
151
                                ->options([
36✔
152
                                    'before' => __('filament-user-attributes::user-attributes.attributes.order_position_before'),
36✔
153
                                    'after' => __('filament-user-attributes::user-attributes.attributes.order_position_after'),
36✔
154
                                    'hidden' => __('filament-user-attributes::user-attributes.attributes.order_position_hidden'),
36✔
155
                                ])
36✔
156
                                ->placeholder(ucfirst(__('filament-user-attributes::user-attributes.attributes.order_sibling_at_end')))
36✔
157
                                ->selectablePlaceholder()
36✔
158
                                ->live()
36✔
159
                                ->label(ucfirst(__('filament-user-attributes::user-attributes.attributes.order_position')))
36✔
160
                                ->required(function (Get $get) {
36✔
161
                                    $sibling = $get('order_sibling_table');
36✔
162
                                    return $sibling !== null && $sibling !== '';
36✔
163
                                }),
36✔
164
                            Forms\Components\Select::make('order_sibling_table')
36✔
165
                                ->selectablePlaceholder()
36✔
166
                                ->live()
36✔
167
                                ->disabled(function (Get $get) {
36✔
168
                                    return $get('order_position_table') === 'hidden'
36✔
169
                                        || $get('order_position_table') == null;
36✔
170
                                })
36✔
171
                                ->placeholder(ucfirst(__('filament-user-attributes::user-attributes.select_sibling')))
36✔
172
                                ->options(function () use ($configModel) {
36✔
173
                                    $columns = $configModel->resource_type::getColumnsForOrdering();
36✔
174
                                    $columns = array_combine(array_column($columns, 'label'), array_column($columns, 'label'));
36✔
175
                                    return $columns;
36✔
176
                                })
36✔
177
                                ->helperText(ucfirst(__('filament-user-attributes::user-attributes.order_sibling_help')))
36✔
178
                                ->label(ucfirst(__('filament-user-attributes::user-attributes.attributes.order_sibling'))),
36✔
179
                        ];
36✔
180
                    }),
36✔
181
            ]);
36✔
182

183
        return $schemas;
36✔
184
    }
185
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc