From 5fd1a7fce527d013d051ea36c64b8a6b34e48783 Mon Sep 17 00:00:00 2001 From: Achyut Neupane Date: Tue, 31 Dec 2024 22:53:01 +0545 Subject: [PATCH 1/9] Enum form component updated --- .../FileGenerators/Concerns/CanGenerateModelForms.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/forms/src/Commands/FileGenerators/Concerns/CanGenerateModelForms.php b/packages/forms/src/Commands/FileGenerators/Concerns/CanGenerateModelForms.php index 51781f5ac7d..63318c4cf66 100644 --- a/packages/forms/src/Commands/FileGenerators/Concerns/CanGenerateModelForms.php +++ b/packages/forms/src/Commands/FileGenerators/Concerns/CanGenerateModelForms.php @@ -95,6 +95,12 @@ public function getFormComponents(?string $model = null, array $exceptColumns = default => TextInput::class, }; + if (isset($type['name']) && $type['name'] === 'enum') { + dd($componentData); + $componentData['type'] = Select::class; + $componentData['options'] = $type['values']; + } + if (str($componentName)->endsWith('_id')) { $guessedRelationshipName = $this->guessBelongsToRelationshipName($componentName, $model); From 23e95ecde66a3cb855f399a034eb870ed68ede31 Mon Sep 17 00:00:00 2001 From: Achyut Neupane Date: Tue, 31 Dec 2024 23:27:57 +0545 Subject: [PATCH 2/9] Options populated according to allowed options --- .../Concerns/CanGenerateModelForms.php | 13 +++++++++++-- .../src/Commands/Concerns/CanReadModelSchemas.php | 1 + 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/forms/src/Commands/FileGenerators/Concerns/CanGenerateModelForms.php b/packages/forms/src/Commands/FileGenerators/Concerns/CanGenerateModelForms.php index 63318c4cf66..c7bc72c08f2 100644 --- a/packages/forms/src/Commands/FileGenerators/Concerns/CanGenerateModelForms.php +++ b/packages/forms/src/Commands/FileGenerators/Concerns/CanGenerateModelForms.php @@ -96,9 +96,18 @@ public function getFormComponents(?string $model = null, array $exceptColumns = }; if (isset($type['name']) && $type['name'] === 'enum') { - dd($componentData); $componentData['type'] = Select::class; - $componentData['options'] = $type['values']; + $options = array_combine( + $type['values'], + array_map( + fn ($value) => (string) str($value) + ->kebab() + ->replace(['-', '_'], ' ') + ->ucfirst(), + $type['values'] + ) + ); + $componentData['options'] = [$options]; } if (str($componentName)->endsWith('_id')) { diff --git a/packages/support/src/Commands/Concerns/CanReadModelSchemas.php b/packages/support/src/Commands/Concerns/CanReadModelSchemas.php index 5b21cc91963..3e5e28b2a09 100644 --- a/packages/support/src/Commands/Concerns/CanReadModelSchemas.php +++ b/packages/support/src/Commands/Concerns/CanReadModelSchemas.php @@ -152,6 +152,7 @@ protected function parseColumnType(array $column): array $values = is_null($values) ? [] : match ($type) { 'string', 'char', 'binary', 'bit' => ['length' => (int) $values[0]], + 'enum' => ['values' => array_map(fn ($value) => trim($value, "'"), $values)], default => [], }; From c9e7835dff29cb03412a8c73109c7b09bef36b01 Mon Sep 17 00:00:00 2001 From: Achyut Neupane Date: Tue, 31 Dec 2024 23:37:36 +0545 Subject: [PATCH 3/9] Default value added in enum column --- .../Commands/FileGenerators/Concerns/CanGenerateModelForms.php | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/forms/src/Commands/FileGenerators/Concerns/CanGenerateModelForms.php b/packages/forms/src/Commands/FileGenerators/Concerns/CanGenerateModelForms.php index c7bc72c08f2..6e267046550 100644 --- a/packages/forms/src/Commands/FileGenerators/Concerns/CanGenerateModelForms.php +++ b/packages/forms/src/Commands/FileGenerators/Concerns/CanGenerateModelForms.php @@ -108,6 +108,7 @@ public function getFormComponents(?string $model = null, array $exceptColumns = ) ); $componentData['options'] = [$options]; + $componentData['default'] = [$this->parseDefaultExpression($column, $model)]; } if (str($componentName)->endsWith('_id')) { From 4aee913b7317d74d7592d8fbba8062cea7f23ac5 Mon Sep 17 00:00:00 2001 From: Achyut Neupane Date: Tue, 31 Dec 2024 23:50:13 +0545 Subject: [PATCH 4/9] Check if default exists or not --- .../FileGenerators/Concerns/CanGenerateModelForms.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/forms/src/Commands/FileGenerators/Concerns/CanGenerateModelForms.php b/packages/forms/src/Commands/FileGenerators/Concerns/CanGenerateModelForms.php index 6e267046550..9d0766e9fd5 100644 --- a/packages/forms/src/Commands/FileGenerators/Concerns/CanGenerateModelForms.php +++ b/packages/forms/src/Commands/FileGenerators/Concerns/CanGenerateModelForms.php @@ -108,7 +108,9 @@ public function getFormComponents(?string $model = null, array $exceptColumns = ) ); $componentData['options'] = [$options]; - $componentData['default'] = [$this->parseDefaultExpression($column, $model)]; + if ($column['default']) { + $componentData['default'] = [$this->parseDefaultExpression($column, $model)]; + } } if (str($componentName)->endsWith('_id')) { From 8cdefd3550427c24780e776404b4bfd615b9d7da Mon Sep 17 00:00:00 2001 From: Achyut Neupane Date: Thu, 2 Jan 2025 23:12:18 +0545 Subject: [PATCH 5/9] Literal added to generate the Enum path --- .../Concerns/CanGenerateModelForms.php | 29 ++++++++++++------- .../Commands/Concerns/CanReadModelSchemas.php | 7 +++++ 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/packages/forms/src/Commands/FileGenerators/Concerns/CanGenerateModelForms.php b/packages/forms/src/Commands/FileGenerators/Concerns/CanGenerateModelForms.php index 9d0766e9fd5..71f5277b2b8 100644 --- a/packages/forms/src/Commands/FileGenerators/Concerns/CanGenerateModelForms.php +++ b/packages/forms/src/Commands/FileGenerators/Concerns/CanGenerateModelForms.php @@ -95,19 +95,28 @@ public function getFormComponents(?string $model = null, array $exceptColumns = default => TextInput::class, }; - if (isset($type['name']) && $type['name'] === 'enum') { + $enumCasts = $this->getEnumCasts($model); + + if (isset($type['name']) && ($type['name'] === 'enum' || array_key_exists($componentName, $enumCasts))) { $componentData['type'] = Select::class; - $options = array_combine( - $type['values'], - array_map( - fn ($value) => (string) str($value) - ->kebab() - ->replace(['-', '_'], ' ') - ->ucfirst(), - $type['values'] + + if (array_key_exists($componentName, $enumCasts)) { + $options = $enumCasts[$componentName]; + $componentData['options'] = [new Literal("\\{$options}::class")]; + } else { + $options = array_combine( + $type['values'], + array_map( + fn($value) => (string)str($value) + ->kebab() + ->replace(['-', '_'], ' ') + ->ucfirst(), + $type['values'] ) ); - $componentData['options'] = [$options]; + $componentData['options'] = [$options]; + } + if ($column['default']) { $componentData['default'] = [$this->parseDefaultExpression($column, $model)]; } diff --git a/packages/support/src/Commands/Concerns/CanReadModelSchemas.php b/packages/support/src/Commands/Concerns/CanReadModelSchemas.php index 3e5e28b2a09..955d8eafba9 100644 --- a/packages/support/src/Commands/Concerns/CanReadModelSchemas.php +++ b/packages/support/src/Commands/Concerns/CanReadModelSchemas.php @@ -9,6 +9,7 @@ use Illuminate\Support\Str; use ReflectionClass; use ReflectionException; +use function Illuminate\Support\enum_value; trait CanReadModelSchemas { @@ -229,4 +230,10 @@ public function getRecordTitleAttribute(): ?string { return null; } + + protected function getEnumCasts($model) + { + $casts = app($model)->getCasts(); + return array_filter($casts, fn ($cast) => enum_exists($cast)); + } } From 85e94eb124bbfbbcc2abe2948dfb6f174f892963 Mon Sep 17 00:00:00 2001 From: Achyut Neupane Date: Thu, 2 Jan 2025 23:22:52 +0545 Subject: [PATCH 6/9] Unused import removed --- packages/support/src/Commands/Concerns/CanReadModelSchemas.php | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/support/src/Commands/Concerns/CanReadModelSchemas.php b/packages/support/src/Commands/Concerns/CanReadModelSchemas.php index 955d8eafba9..b8486b475fa 100644 --- a/packages/support/src/Commands/Concerns/CanReadModelSchemas.php +++ b/packages/support/src/Commands/Concerns/CanReadModelSchemas.php @@ -9,7 +9,6 @@ use Illuminate\Support\Str; use ReflectionClass; use ReflectionException; -use function Illuminate\Support\enum_value; trait CanReadModelSchemas { From f065435d9e324b1f6765196e302ab0158dcafe89 Mon Sep 17 00:00:00 2001 From: Achyut Neupane Date: Thu, 2 Jan 2025 23:23:04 +0545 Subject: [PATCH 7/9] Return type added --- packages/support/src/Commands/Concerns/CanReadModelSchemas.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/support/src/Commands/Concerns/CanReadModelSchemas.php b/packages/support/src/Commands/Concerns/CanReadModelSchemas.php index b8486b475fa..7e76d056d35 100644 --- a/packages/support/src/Commands/Concerns/CanReadModelSchemas.php +++ b/packages/support/src/Commands/Concerns/CanReadModelSchemas.php @@ -230,7 +230,7 @@ public function getRecordTitleAttribute(): ?string return null; } - protected function getEnumCasts($model) + protected function getEnumCasts($model): array { $casts = app($model)->getCasts(); return array_filter($casts, fn ($cast) => enum_exists($cast)); From 71ee66f1dcf6ea38519840d1e80eb36cfc91f4c3 Mon Sep 17 00:00:00 2001 From: Achyut Neupane Date: Thu, 2 Jan 2025 23:27:50 +0545 Subject: [PATCH 8/9] Stan error fixed --- .../support/src/Commands/Concerns/CanReadModelSchemas.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/support/src/Commands/Concerns/CanReadModelSchemas.php b/packages/support/src/Commands/Concerns/CanReadModelSchemas.php index 7e76d056d35..d15a49c84bb 100644 --- a/packages/support/src/Commands/Concerns/CanReadModelSchemas.php +++ b/packages/support/src/Commands/Concerns/CanReadModelSchemas.php @@ -230,7 +230,11 @@ public function getRecordTitleAttribute(): ?string return null; } - protected function getEnumCasts($model): array + /** + * @param string $model + * @return array + */ + protected function getEnumCasts(string $model): array { $casts = app($model)->getCasts(); return array_filter($casts, fn ($cast) => enum_exists($cast)); From 7ab3b83c359df6a862d8c689203d33a9bec38b8d Mon Sep 17 00:00:00 2001 From: Achyut Neupane <30431426+achyutkneupane@users.noreply.github.com> Date: Mon, 6 Jan 2025 09:37:26 +0545 Subject: [PATCH 9/9] Update CanGenerateModelForms.php --- .../FileGenerators/Concerns/CanGenerateModelForms.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/forms/src/Commands/FileGenerators/Concerns/CanGenerateModelForms.php b/packages/forms/src/Commands/FileGenerators/Concerns/CanGenerateModelForms.php index 71f5277b2b8..af4fc372bec 100644 --- a/packages/forms/src/Commands/FileGenerators/Concerns/CanGenerateModelForms.php +++ b/packages/forms/src/Commands/FileGenerators/Concerns/CanGenerateModelForms.php @@ -101,8 +101,8 @@ public function getFormComponents(?string $model = null, array $exceptColumns = $componentData['type'] = Select::class; if (array_key_exists($componentName, $enumCasts)) { - $options = $enumCasts[$componentName]; - $componentData['options'] = [new Literal("\\{$options}::class")]; + $enumClass = $enumCasts[$componentName]; + $componentData['options'] = [new Literal("\\{$enumClass}::class")]; } else { $options = array_combine( $type['values'],