Skip to content

Commit

Permalink
Add casts() into model class
Browse files Browse the repository at this point in the history
  • Loading branch information
cable8mm committed Jan 6, 2025
1 parent c926a87 commit 3f61e6d
Show file tree
Hide file tree
Showing 21 changed files with 128 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
17 changes: 17 additions & 0 deletions src/Generators/ModelGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down
7 changes: 7 additions & 0 deletions src/Interfaces/ResolverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion src/ResolverSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
5 changes: 5 additions & 0 deletions src/Resolvers/BitResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@ public function nova(): ?string
{
return 'Boolean::make(\''.Inflector::title($this->column->field).'\'),';
}

public function cast(): ?string
{
return 'boolean';
}
}
5 changes: 5 additions & 0 deletions src/Resolvers/BoolResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ public function nova(): ?string
{
return 'Boolean::make(\''.Inflector::title($this->column->field).'\'),';
}

public function cast(): ?string
{
return 'boolean';
}
}
5 changes: 5 additions & 0 deletions src/Resolvers/DateResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ public function nova(): ?string
{
return 'Date::make(\''.Inflector::title($this->column->field).'\'),';
}

public function cast(): ?string
{
return 'date';
}
}
5 changes: 5 additions & 0 deletions src/Resolvers/DateTimeTzResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ public function nova(): ?string
{
return 'DateTime::make(\''.Inflector::title($this->column->field).'\'),';
}

public function cast(): ?string
{
return 'datetime';
}
}
5 changes: 5 additions & 0 deletions src/Resolvers/DatetimeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,9 @@ public function nova(): ?string
{
return 'DateTime::make(\''.Inflector::title($this->column->field).'\'),';
}

public function cast(): ?string
{
return 'datetime';
}
}
5 changes: 5 additions & 0 deletions src/Resolvers/DecimalResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
5 changes: 5 additions & 0 deletions src/Resolvers/DoubleResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,9 @@ public function nova(): ?string
{
return 'Number::make(\''.Inflector::title($this->column->field).'\')->step(\'any\'),';
}

public function cast(): ?string
{
return 'double';
}
}
5 changes: 5 additions & 0 deletions src/Resolvers/FloatResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,9 @@ public function nova(): ?string
{
return 'Number::make(\''.Inflector::title($this->column->field).'\')->step(\'any\'),';
}

public function cast(): ?string
{
return 'float';
}
}
5 changes: 5 additions & 0 deletions src/Resolvers/IntResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,9 @@ public function nova(): ?string

return $novaResourceField;
}

public function cast(): ?string
{
return 'integer';
}
}
5 changes: 5 additions & 0 deletions src/Resolvers/JsonResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ public function nova(): ?string
{
return 'KeyValue::make(\''.Inflector::title($this->column->field).'\')->rules(\'json\'),';
}

public function cast(): ?string
{
return 'object';
}
}
10 changes: 10 additions & 0 deletions src/Resolvers/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
5 changes: 5 additions & 0 deletions src/Resolvers/SmallintResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@ public function nova(): ?string
{
return 'Number::make(\''.Inflector::title($this->column->field).'\'),';
}

public function cast(): ?string
{
return 'integer';
}
}
5 changes: 5 additions & 0 deletions src/Resolvers/TimestampResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
}
5 changes: 5 additions & 0 deletions src/Resolvers/TinyintResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
}
5 changes: 5 additions & 0 deletions src/Resolvers/VarcharResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ public function nova(): ?string

return 'Text::make(\''.Inflector::title($this->column->field).'\')->maxlength('.$bracket.'),';
}

public function cast(): ?string
{
return 'string';
}
}
2 changes: 1 addition & 1 deletion stubs/Model.stub
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ use Illuminate\Database\Eloquent\Model;

class {model} extends Model
{
use HasFactory;{timestamps}
use HasFactory;{timestamps}{casts}
}
17 changes: 17 additions & 0 deletions tests/Unit/Generators/ModelGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
]),
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 3f61e6d

Please sign in to comment.