Skip to content

Commit

Permalink
Merge pull request #22 from Laravel-Lang/1.x
Browse files Browse the repository at this point in the history
Template for creating models switched to Laravel 11 style
  • Loading branch information
andrey-helldar authored Jun 22, 2024
2 parents 3398611 + 511c83b commit b4ad8f3
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 13 deletions.
20 changes: 16 additions & 4 deletions src/Generators/ModelGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace LaravelLang\Models\Generators;

use DragonCode\Support\Facades\Filesystem\Path;
use Illuminate\Support\Str;
use LaravelLang\Models\Services\ClassMap;

use function array_map;
Expand All @@ -15,9 +16,13 @@ class ModelGenerator extends Generator
{
protected string $stub = __DIR__ . '/../../stubs/model.stub';

protected string $fillables = ' \'%s\',';
protected string $fillables = '\'%s\',';

protected string $casts = ' \'%s\' => TrimCast::class,';
protected int $fillablePad = 8;

protected string $casts = '\'%s\' => TrimCast::class,';

protected int $castsPad = 12;

protected function data(): array
{
Expand All @@ -40,14 +45,14 @@ protected function filename(): string
protected function getFillable(): array
{
return array_map(function (string $attribute) {
return sprintf($this->fillables, $attribute);
return $this->pad(sprintf($this->fillables, $attribute), $this->fillablePad);
}, $this->columns);
}

protected function getCasts(): array
{
return array_map(function (string $attribute) {
return sprintf($this->casts, $attribute);
return $this->pad(sprintf($this->casts, $attribute), $this->castsPad);
}, $this->columns);
}

Expand All @@ -60,4 +65,11 @@ protected function extension(string $path): string
{
return Path::extension($path);
}

protected function pad(string $value, int $length): string
{
return Str::of($value)
->padLeft(Str::length($value) + $length, ' ')
->toString();
}
}
7 changes: 5 additions & 2 deletions stubs/model.stub
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ class {{model}}{{suffix}} extends Translation
{{fillable}}
];

protected $casts = [
protected function casts(): array
{
return [
{{casts}}
];
];
}
}
11 changes: 8 additions & 3 deletions tests/Unit/Console/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,23 @@
expect($migrations)->toHaveCount(1);

$fillableMatches = collect(['locale', 'title', 'description'])
->map(fn (string $column) => sprintf('(\s+\'%s\',\r?\n?)', $column))
->map(fn (string $column) => sprintf('(\s{8}\'%s\',\r?\n?)', $column))
->implode('');

$castsMatches = collect(['title', 'description'])
->map(fn (string $column) => sprintf('(\s+\'%s\'\s=>\sTrimCast::class,\r?\n?)', $column))
->map(fn (string $column) => sprintf('(\s{12}\'%s\'\s=>\sTrimCast::class,\r?\n?)', $column))
->implode('');

expect(file_get_contents($model))
->toContain('App\Models')
->toContain('class TestTranslation extends Translation')
->toMatch(sprintf('/protected\s\$fillable\s=\s\[\r?\n?%s\s+];/', $fillableMatches))
->toMatch(sprintf('/protected\s\$casts\s=\s\[\n?\r?%s\s+];/', $castsMatches));
->toMatch(
sprintf(
'/\s{4}protected\s+function\s+casts\(\):\sarray\r?\n?\s{4}\{\r?\n?\s{8}return\s\[\r?\n?%s\s{8}\];\r?\n?\s{4}\}/',
$castsMatches
)
);

expect(file_get_contents($migrations[0]))
->toContain('Schema::create(\'test_translations\'')
Expand Down
11 changes: 7 additions & 4 deletions workbench/app/Models/TestModelTranslation.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ class TestModelTranslation extends Translation
'description',
];

protected $casts = [
'title' => TrimCast::class,
'description' => TrimCast::class,
];
protected function casts(): array
{
return [
'title' => TrimCast::class,
'description' => TrimCast::class,
];
}
}

0 comments on commit b4ad8f3

Please sign in to comment.