diff --git a/src/Column.php b/src/Column.php index 101a71e..35e1cb7 100644 --- a/src/Column.php +++ b/src/Column.php @@ -146,4 +146,9 @@ public function __toString(): string { return 'Field is '.$this->field.' and type is '.$this->type.'.'; } + + public function cast(): ?string + { + return ResolverSelector::of($this)->cast(); + } } diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index 1b8a316..a5b745a 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -37,14 +37,31 @@ private function __construct( */ public function run(bool $force = false): void { + // Check if having casts + $casts = []; + foreach ($this->table->getColumns() as $column) { + if (! is_null($column->cast())) { + $casts[] = "'{$column->field}' => '{$column->cast()}'"; + } + } + + $castString = ''; + if (! empty($casts)) { + $castString = PHP_EOL.PHP_EOL.' '.'protected function casts(): array'.PHP_EOL.' {'.PHP_EOL.' return ['.PHP_EOL.' '; + $castString .= implode(','.PHP_EOL.' ', $casts).PHP_EOL; + $castString .= ' ];'.PHP_EOL.' }'; + } + $seederClass = str_replace( [ '{model}', '{timestamps}', + '{casts}', ], [ $this->table->model(), $this->table->hasTimestamps() ? PHP_EOL.PHP_EOL.' public $timestamps = false;' : '', + $castString, ], $this->stub ); diff --git a/src/Interfaces/ResolverInterface.php b/src/Interfaces/ResolverInterface.php index e0a44fc..6115b2b 100644 --- a/src/Interfaces/ResolverInterface.php +++ b/src/Interfaces/ResolverInterface.php @@ -27,4 +27,11 @@ public function migration(): string; * @return string The method returns a Nova resource file row field string */ public function nova(): ?string; + + /** + * Get the row string for cast file, then return the string for cast class. + * + * @return string The method returns a cast file row field string + */ + public function cast(): ?string; } diff --git a/src/ResolverSelector.php b/src/ResolverSelector.php index cac03b9..45bd29c 100644 --- a/src/ResolverSelector.php +++ b/src/ResolverSelector.php @@ -63,7 +63,7 @@ public static function of(Column $column): ResolverInterface return new IdResolver($column); } - if ($column->type == 'bigint') { + if ($column->type == 'bigint' || $column->type == 'biginteger' || $column->type == 'bigInteger') { return new BigintResolver($column); } diff --git a/src/Resolvers/BitResolver.php b/src/Resolvers/BitResolver.php index 50cd5c0..72047d1 100644 --- a/src/Resolvers/BitResolver.php +++ b/src/Resolvers/BitResolver.php @@ -30,4 +30,9 @@ public function nova(): ?string { return 'Boolean::make(\''.Inflector::title($this->column->field).'\'),'; } + + public function cast(): ?string + { + return 'boolean'; + } } diff --git a/src/Resolvers/BoolResolver.php b/src/Resolvers/BoolResolver.php index 1c2b42b..abe24f5 100644 --- a/src/Resolvers/BoolResolver.php +++ b/src/Resolvers/BoolResolver.php @@ -27,4 +27,9 @@ public function nova(): ?string { return 'Boolean::make(\''.Inflector::title($this->column->field).'\'),'; } + + public function cast(): ?string + { + return 'boolean'; + } } diff --git a/src/Resolvers/DateResolver.php b/src/Resolvers/DateResolver.php index 26aa715..acee7f1 100644 --- a/src/Resolvers/DateResolver.php +++ b/src/Resolvers/DateResolver.php @@ -28,4 +28,9 @@ public function nova(): ?string { return 'Date::make(\''.Inflector::title($this->column->field).'\'),'; } + + public function cast(): ?string + { + return 'date'; + } } diff --git a/src/Resolvers/DateTimeTzResolver.php b/src/Resolvers/DateTimeTzResolver.php index 265bdb7..c6b7221 100644 --- a/src/Resolvers/DateTimeTzResolver.php +++ b/src/Resolvers/DateTimeTzResolver.php @@ -27,4 +27,9 @@ public function nova(): ?string { return 'DateTime::make(\''.Inflector::title($this->column->field).'\'),'; } + + public function cast(): ?string + { + return 'datetime'; + } } diff --git a/src/Resolvers/DatetimeResolver.php b/src/Resolvers/DatetimeResolver.php index cecaed1..88a37c8 100644 --- a/src/Resolvers/DatetimeResolver.php +++ b/src/Resolvers/DatetimeResolver.php @@ -35,4 +35,9 @@ public function nova(): ?string { return 'DateTime::make(\''.Inflector::title($this->column->field).'\'),'; } + + public function cast(): ?string + { + return 'datetime'; + } } diff --git a/src/Resolvers/DecimalResolver.php b/src/Resolvers/DecimalResolver.php index d07d915..90655d5 100644 --- a/src/Resolvers/DecimalResolver.php +++ b/src/Resolvers/DecimalResolver.php @@ -46,4 +46,9 @@ public function nova(): ?string { return 'Number::make(\''.Inflector::title($this->column->field).'\')->step(\'any\'),'; } + + public function cast(): ?string + { + return 'decimal:'.Bracket::of($this->column->bracket)->escape(); + } } diff --git a/src/Resolvers/DoubleResolver.php b/src/Resolvers/DoubleResolver.php index b6d0d35..12e2070 100644 --- a/src/Resolvers/DoubleResolver.php +++ b/src/Resolvers/DoubleResolver.php @@ -39,4 +39,9 @@ public function nova(): ?string { return 'Number::make(\''.Inflector::title($this->column->field).'\')->step(\'any\'),'; } + + public function cast(): ?string + { + return 'double'; + } } diff --git a/src/Resolvers/FloatResolver.php b/src/Resolvers/FloatResolver.php index da6d461..1653d31 100644 --- a/src/Resolvers/FloatResolver.php +++ b/src/Resolvers/FloatResolver.php @@ -47,4 +47,9 @@ public function nova(): ?string { return 'Number::make(\''.Inflector::title($this->column->field).'\')->step(\'any\'),'; } + + public function cast(): ?string + { + return 'float'; + } } diff --git a/src/Resolvers/IntResolver.php b/src/Resolvers/IntResolver.php index 94b218a..6d10dce 100644 --- a/src/Resolvers/IntResolver.php +++ b/src/Resolvers/IntResolver.php @@ -39,4 +39,9 @@ public function nova(): ?string return $novaResourceField; } + + public function cast(): ?string + { + return 'integer'; + } } diff --git a/src/Resolvers/JsonResolver.php b/src/Resolvers/JsonResolver.php index df2335a..0b1fde7 100644 --- a/src/Resolvers/JsonResolver.php +++ b/src/Resolvers/JsonResolver.php @@ -25,4 +25,9 @@ public function nova(): ?string { return 'KeyValue::make(\''.Inflector::title($this->column->field).'\')->rules(\'json\'),'; } + + public function cast(): ?string + { + return 'object'; + } } diff --git a/src/Resolvers/Resolver.php b/src/Resolvers/Resolver.php index ac7ff3a..5d591e1 100644 --- a/src/Resolvers/Resolver.php +++ b/src/Resolvers/Resolver.php @@ -66,6 +66,16 @@ public function last(string $migration): string return $migration.';'; } + /** + * Get the attribute casting + * + * @return string|null The method returns the attribute casting + */ + public function cast(): ?string + { + return null; + } + /** * Reading data from inaccessible (protected or private) or non-existing properties. * diff --git a/src/Resolvers/SmallintResolver.php b/src/Resolvers/SmallintResolver.php index 5ac84fa..19e4208 100644 --- a/src/Resolvers/SmallintResolver.php +++ b/src/Resolvers/SmallintResolver.php @@ -33,4 +33,9 @@ public function nova(): ?string { return 'Number::make(\''.Inflector::title($this->column->field).'\'),'; } + + public function cast(): ?string + { + return 'integer'; + } } diff --git a/src/Resolvers/TimestampResolver.php b/src/Resolvers/TimestampResolver.php index 2b6753b..701dbb0 100644 --- a/src/Resolvers/TimestampResolver.php +++ b/src/Resolvers/TimestampResolver.php @@ -48,4 +48,9 @@ public function nova(): ?string { return 'DateTime::make(\''.Inflector::title($this->column->field).'\'),'; } + + public function cast(): ?string + { + return $this->column->field == 'created_at' || $this->column->field == 'updated_at' ? null : 'timestamp'; + } } diff --git a/src/Resolvers/TinyintResolver.php b/src/Resolvers/TinyintResolver.php index b1c9829..bf7cb63 100644 --- a/src/Resolvers/TinyintResolver.php +++ b/src/Resolvers/TinyintResolver.php @@ -38,4 +38,9 @@ public function nova(): ?string 'Number::make(\''.Inflector::title($this->column->field).'\')->min(0)->max(255),' : 'Number::make(\''.Inflector::title($this->column->field).'\')->min(-128)->max(127),'; } + + public function cast(): ?string + { + return 'integer'; + } } diff --git a/src/Resolvers/VarcharResolver.php b/src/Resolvers/VarcharResolver.php index 6f4be37..e713f01 100644 --- a/src/Resolvers/VarcharResolver.php +++ b/src/Resolvers/VarcharResolver.php @@ -41,4 +41,9 @@ public function nova(): ?string return 'Text::make(\''.Inflector::title($this->column->field).'\')->maxlength('.$bracket.'),'; } + + public function cast(): ?string + { + return 'string'; + } } diff --git a/stubs/Model.stub b/stubs/Model.stub index f841aeb..42e0147 100644 --- a/stubs/Model.stub +++ b/stubs/Model.stub @@ -7,5 +7,5 @@ use Illuminate\Database\Eloquent\Model; class {model} extends Model { - use HasFactory;{timestamps} + use HasFactory;{timestamps}{casts} } diff --git a/tests/Unit/Generators/ModelGeneratorTest.php b/tests/Unit/Generators/ModelGeneratorTest.php index f58ba73..dd8963c 100644 --- a/tests/Unit/Generators/ModelGeneratorTest.php +++ b/tests/Unit/Generators/ModelGeneratorTest.php @@ -16,6 +16,7 @@ protected function setUp(): void ModelGenerator::make( new Table('samples', [ Column::make('id', 'bigint'), + Column::make('timestamp', 'timestamp'), Column::make('created_at', 'timestamp'), Column::make('updated_at', 'timestamp'), ]), @@ -55,4 +56,20 @@ public function test_it_can_generate_without_timestamps(): void $this->assertStringContainsString('timestamps', $file); } + + public function test_it_is_needed_to_properties(): void + { + File::system()->delete(Path::testgen().DIRECTORY_SEPARATOR.'Sample.php'); + + ModelGenerator::make( + new Table('samples', [ + Column::make('id', 'bigint'), + ]), + destination: Path::testgen() + )->run(); + + $file = File::system()->read(Path::testgen().DIRECTORY_SEPARATOR.'Sample.php'); + + $this->assertStringContainsString('timestamps', $file); + } }