Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template for creating models switched to Laravel 11 style #22

Merged
merged 1 commit into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
2 changes: 1 addition & 1 deletion src/Services/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Validator
/**
* @param Attribute|\LaravelLang\Models\HasTranslations $model
*
* @throws \LaravelLang\Models\Exceptions\AttributeIsNotTranslatableException
* @throws AttributeIsNotTranslatableException
*/
public static function column(Model $model, string $column): string
{
Expand Down
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,
];
}
}