Skip to content

Commit

Permalink
Add DbType Attribute on the field type config
Browse files Browse the repository at this point in the history
  • Loading branch information
cklei-carly committed Aug 15, 2024
1 parent 12d456f commit 39506f7
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/FieldTypes/Configs/Attributes/DbType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace SolutionForest\FilamentFieldGroup\FieldTypes\Configs\Attributes;

use Attribute;
/**
* @property string $drive The drive of the database.
* @property string $type The type of the field in database.
* @property int|null $length The length of the field in database.
*/
#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
class DbType
{
public function __construct(
public string $drive,
public string $type,
public ?int $length = null,
) {}
}
7 changes: 7 additions & 0 deletions src/FieldTypes/Configs/Contracts/FieldTypeConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,11 @@ public static function getFormComponents(): array;
* @return array<int,array<string,string>>
*/
public static function getConfigNames(): array;

/**
* Get the database type mapping for the field type.
*
* @return array<string,mixed> | array<string,array<string,mixed>>
*/
public static function getDbTypeMapping(string $drive = null): array;
}
3 changes: 3 additions & 0 deletions src/FieldTypes/Configs/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
29 changes: 29 additions & 0 deletions src/FieldTypes/Configs/FieldTypeBaseConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions src/FieldTypes/Configs/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions src/FieldTypes/Configs/Password.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions src/FieldTypes/Configs/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions src/FieldTypes/Configs/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions src/FieldTypes/Configs/TextArea.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/FieldTypes/Configs/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 39506f7

Please sign in to comment.