diff --git a/src/FieldTypes/Configs/Attributes/DbType.php b/src/FieldTypes/Configs/Attributes/DbType.php new file mode 100644 index 0000000..1c834c4 --- /dev/null +++ b/src/FieldTypes/Configs/Attributes/DbType.php @@ -0,0 +1,19 @@ +> */ public static function getConfigNames(): array; + + /** + * Get the database type mapping for the field type. + * + * @return array | array> + */ + public static function getDbTypeMapping(string $drive = null): array; } diff --git a/src/FieldTypes/Configs/Email.php b/src/FieldTypes/Configs/Email.php index b177e29..2a29a72 100644 --- a/src/FieldTypes/Configs/Email.php +++ b/src/FieldTypes/Configs/Email.php @@ -7,10 +7,13 @@ use Filament\Forms\Components\Concerns\HasAffixes; use Filament\Forms\Components\Concerns\HasPlaceholder; use SolutionForest\FilamentFieldGroup\FieldTypes\Configs\Attributes\ConfigName; +use SolutionForest\FilamentFieldGroup\FieldTypes\Configs\Attributes\DbType; use SolutionForest\FilamentFieldGroup\FieldTypes\Configs\Attributes\FormComponent; #[ConfigName('email', 'Email', 'General')] #[FormComponent(Forms\Components\TextInput::class)] +#[DbType('mysql', 'varchar')] +#[DbType('sqlite', 'text')] class Email extends FieldTypeBaseConfig implements Contracts\FieldTypeConfig { use Concerns\HasAffixes; diff --git a/src/FieldTypes/Configs/FieldTypeBaseConfig.php b/src/FieldTypes/Configs/FieldTypeBaseConfig.php index eef37a0..14f4a5f 100644 --- a/src/FieldTypes/Configs/FieldTypeBaseConfig.php +++ b/src/FieldTypes/Configs/FieldTypeBaseConfig.php @@ -6,6 +6,7 @@ use Illuminate\Support\Arr; use ReflectionClass; use SolutionForest\FilamentFieldGroup\FieldTypes\Configs\Attributes\ConfigName; +use SolutionForest\FilamentFieldGroup\FieldTypes\Configs\Attributes\DbType; use SolutionForest\FilamentFieldGroup\FieldTypes\Configs\Attributes\FormComponent; use SolutionForest\FilamentFieldGroup\FieldTypes\Configs\Contracts\FieldTypeConfig; @@ -100,6 +101,34 @@ public static function getConfigNames(): array return $result; } + /** {@inheritDoc} */ + public static function getDbTypeMapping(string $drive = null): array + { + $attributes = static::findFieldConfigAttribute(static::class, DbType::class); + + // Map the attributes to array + foreach ($attributes as $attribute) { + + $attributeInstance = $attribute->newInstance(); + + // throw exception if drive is duplicated in mapping + if (isset($mapping[$attributeInstance->drive])) { + throw new \Exception("The drive {$attributeInstance->drive} is duplicated in the mapping."); + } + + $mapping[$attributeInstance->drive] = [ + 'type' => $attributeInstance->type, + 'length' => $attributeInstance->length, + ]; + } + + if ($drive) { + return $mapping[$drive] ?? []; + } + + return $mapping; + } + public function __toArray(): array { return get_object_vars($this); diff --git a/src/FieldTypes/Configs/Number.php b/src/FieldTypes/Configs/Number.php index deef48d..952e025 100644 --- a/src/FieldTypes/Configs/Number.php +++ b/src/FieldTypes/Configs/Number.php @@ -7,10 +7,13 @@ use Filament\Forms\Components\Concerns\HasAffixes; use Filament\Forms\Components\Concerns\HasPlaceholder; use SolutionForest\FilamentFieldGroup\FieldTypes\Configs\Attributes\ConfigName; +use SolutionForest\FilamentFieldGroup\FieldTypes\Configs\Attributes\DbType; use SolutionForest\FilamentFieldGroup\FieldTypes\Configs\Attributes\FormComponent; #[ConfigName('number', 'Number', 'General')] #[FormComponent(Forms\Components\TextInput::class)] +#[DbType('mysql', 'int')] +#[DbType('sqlite', 'integer')] class Number extends FieldTypeBaseConfig implements Contracts\FieldTypeConfig { use Concerns\HasAffixes; diff --git a/src/FieldTypes/Configs/Password.php b/src/FieldTypes/Configs/Password.php index 4e1d5f3..34ff808 100644 --- a/src/FieldTypes/Configs/Password.php +++ b/src/FieldTypes/Configs/Password.php @@ -6,10 +6,13 @@ use Filament\Forms\Components\Concerns\CanBeValidated; use Filament\Forms\Components\Concerns\HasPlaceholder; use SolutionForest\FilamentFieldGroup\FieldTypes\Configs\Attributes\ConfigName; +use SolutionForest\FilamentFieldGroup\FieldTypes\Configs\Attributes\DbType; use SolutionForest\FilamentFieldGroup\FieldTypes\Configs\Attributes\FormComponent; #[ConfigName('password', 'Password', 'General')] #[FormComponent(Forms\Components\TextInput::class)] +#[DbType('mysql', 'varchar')] +#[DbType('sqlite', 'text')] class Password extends FieldTypeBaseConfig implements Contracts\FieldTypeConfig { use Concerns\HasPlaceholder; diff --git a/src/FieldTypes/Configs/Select.php b/src/FieldTypes/Configs/Select.php index d7d1ac0..fd49a17 100644 --- a/src/FieldTypes/Configs/Select.php +++ b/src/FieldTypes/Configs/Select.php @@ -5,10 +5,13 @@ use Filament\Forms; use Filament\Forms\Components\Concerns\CanBeValidated; use SolutionForest\FilamentFieldGroup\FieldTypes\Configs\Attributes\ConfigName; +use SolutionForest\FilamentFieldGroup\FieldTypes\Configs\Attributes\DbType; use SolutionForest\FilamentFieldGroup\FieldTypes\Configs\Attributes\FormComponent; #[ConfigName('select', 'Select', 'Choices')] #[FormComponent(Forms\Components\Select::class)] +#[DbType('mysql', 'varchar')] +#[DbType('sqlite', 'text')] class Select extends FieldTypeBaseConfig implements Contracts\FieldTypeConfig { use Concerns\HasDefaultValue; diff --git a/src/FieldTypes/Configs/Text.php b/src/FieldTypes/Configs/Text.php index 362933d..1f5c5d5 100644 --- a/src/FieldTypes/Configs/Text.php +++ b/src/FieldTypes/Configs/Text.php @@ -8,10 +8,13 @@ use Filament\Forms\Components\Concerns\HasAffixes; use Filament\Forms\Components\Concerns\HasPlaceholder; use SolutionForest\FilamentFieldGroup\FieldTypes\Configs\Attributes\ConfigName; +use SolutionForest\FilamentFieldGroup\FieldTypes\Configs\Attributes\DbType; use SolutionForest\FilamentFieldGroup\FieldTypes\Configs\Attributes\FormComponent; #[ConfigName('text', 'Text', 'General')] #[FormComponent(Forms\Components\TextInput::class)] +#[DbType('mysql', 'varchar')] +#[DbType('sqlite', 'text')] class Text extends FieldTypeBaseConfig implements Contracts\FieldTypeConfig { use Concerns\CanBeLengthConstrained; diff --git a/src/FieldTypes/Configs/TextArea.php b/src/FieldTypes/Configs/TextArea.php index 11fccc7..3d68443 100644 --- a/src/FieldTypes/Configs/TextArea.php +++ b/src/FieldTypes/Configs/TextArea.php @@ -6,10 +6,13 @@ use Filament\Forms\Components\Concerns\CanBeValidated; use Filament\Forms\Components\Concerns\HasPlaceholder; use SolutionForest\FilamentFieldGroup\FieldTypes\Configs\Attributes\ConfigName; +use SolutionForest\FilamentFieldGroup\FieldTypes\Configs\Attributes\DbType; use SolutionForest\FilamentFieldGroup\FieldTypes\Configs\Attributes\FormComponent; #[ConfigName('textArea', 'Text Area', 'General')] #[FormComponent(Forms\Components\Textarea::class)] +#[DbType('mysql', 'text')] +#[DbType('sqlite', 'text')] class TextArea extends FieldTypeBaseConfig implements Contracts\FieldTypeConfig { use Concerns\HasDefaultValue; diff --git a/src/FieldTypes/Configs/Url.php b/src/FieldTypes/Configs/Url.php index 0b50e5c..a211a98 100644 --- a/src/FieldTypes/Configs/Url.php +++ b/src/FieldTypes/Configs/Url.php @@ -7,10 +7,12 @@ use Filament\Forms\Components\Concerns\HasAffixes; use Filament\Forms\Components\Concerns\HasPlaceholder; use SolutionForest\FilamentFieldGroup\FieldTypes\Configs\Attributes\ConfigName; +use SolutionForest\FilamentFieldGroup\FieldTypes\Configs\Attributes\DbType; use SolutionForest\FilamentFieldGroup\FieldTypes\Configs\Attributes\FormComponent; #[ConfigName('url', 'URL', 'General')] #[FormComponent(Forms\Components\TextInput::class)] +#[DbType('mysql', 'varchar')] class Url extends FieldTypeBaseConfig implements Contracts\FieldTypeConfig { use Concerns\HasAffixes;