Skip to content

Commit

Permalink
Literal added to generate the Enum path
Browse files Browse the repository at this point in the history
  • Loading branch information
achyutkneupane committed Jan 2, 2025
1 parent 4aee913 commit 8cdefd3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Support\Str;
use ReflectionClass;
use ReflectionException;
use function Illuminate\Support\enum_value;

trait CanReadModelSchemas
{
Expand Down Expand Up @@ -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));
}
}

0 comments on commit 8cdefd3

Please sign in to comment.