Skip to content

Commit

Permalink
Adjustements, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
bastien-phi committed Oct 23, 2023
1 parent d02fb29 commit 2cfd001
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 17 deletions.
16 changes: 11 additions & 5 deletions src/Domain/Models/AttributeTypeCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ public function __construct(Model $model)
public function resolve(Attribute $attribute): Attribute
{
switch (true) {
case $this->isTimestamps($attribute):
$type = $this->dateClass();

break;
case $this->hasCast($attribute) && $this->isNotInboundCast($attribute):
$type = $this->resolveFromCast($attribute);

break;
case $this->isTimestamps($attribute):
$type = $this->dateClass();

break;
default:
$type = $this->resolveFromDatabaseType($attribute);
Expand Down Expand Up @@ -105,9 +105,10 @@ private function resolveFromCast(Attribute $attribute): string
case 'date':
case 'datetime':
case 'custom_datetime':
return $this->dateClass();
case 'immutable_date':
case 'immutable_datetime':
return $this->dateClass();
return $this->immutableDateClass();
}

if ($this->isCustomCast($castType)) {
Expand Down Expand Up @@ -176,6 +177,11 @@ private function dateClass(): string
return '\\' . get_class(Date::now());
}

private function immutableDateClass(): string
{
return '\\' . get_class(Date::now()->toImmutable());
}

private function isCustomCast(string $castType): bool
{
return class_exists($castType)
Expand Down
10 changes: 1 addition & 9 deletions tests/Fixtures/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,6 @@
/**
* This model is used for testing purposes.
* @generated
* @property int $id
* @property string $email
* @property string $password
* @property \Soyhuce\NextIdeHelper\Tests\Fixtures\Address $address
* @property string|null $remember_token
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property-read \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\PostCollection $laravelPosts
* @property-read \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\PostCollection $posts
*/
class User extends Model
{
Expand All @@ -36,6 +27,7 @@ class User extends Model
'nullable_name' => Uppercase::class,
'role' => Role::class,
'password' => 'hashed',
'updated_at' => 'immutable_datetime',
];

public function posts(): HasMany
Expand Down
3 changes: 2 additions & 1 deletion tests/expected/User.stub
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\Post;
* @property string|null $nullable_name
* @property string|null $remember_token
* @property \Illuminate\Support\Carbon $created_at
* @property \Illuminate\Support\Carbon $updated_at
* @property \Carbon\CarbonImmutable $updated_at
* @property string $screamed_email
* @property \Soyhuce\NextIdeHelper\Tests\Fixtures\Address|null $shipping_address
* @property-read string $city
Expand All @@ -45,6 +45,7 @@ class User extends Model
'nullable_name' => Uppercase::class,
'role' => Role::class,
'password' => 'hashed',
'updated_at' => 'immutable_datetime',
];

public function posts(): HasMany
Expand Down
2 changes: 1 addition & 1 deletion tests/expected/_ide_models.stub
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace IdeHelper\Soyhuce\NextIdeHelper\Tests\Fixtures
* @method \IdeHelper\Soyhuce\NextIdeHelper\Tests\Fixtures\__UserQuery whereNullableName(string|null $value)
* @method \IdeHelper\Soyhuce\NextIdeHelper\Tests\Fixtures\__UserQuery whereRememberToken(string|null $value)
* @method \IdeHelper\Soyhuce\NextIdeHelper\Tests\Fixtures\__UserQuery whereCreatedAt(\Illuminate\Support\Carbon|string $value)
* @method \IdeHelper\Soyhuce\NextIdeHelper\Tests\Fixtures\__UserQuery whereUpdatedAt(\Illuminate\Support\Carbon|string $value)
* @method \IdeHelper\Soyhuce\NextIdeHelper\Tests\Fixtures\__UserQuery whereUpdatedAt(\Carbon\CarbonImmutable|string $value)
* @method \IdeHelper\Soyhuce\NextIdeHelper\Tests\Fixtures\__UserQuery whereEmailDomain(string $domain, ?string $area = null)
* @method \Soyhuce\NextIdeHelper\Tests\Fixtures\User create(array $attributes = [])
* @method \Illuminate\Database\Eloquent\Collection|\Soyhuce\NextIdeHelper\Tests\Fixtures\User|null find($id, array $columns = ['*'])
Expand Down
2 changes: 1 addition & 1 deletion tests/expected/_ide_modelsLarastan.stub
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace IdeHelper\Soyhuce\NextIdeHelper\Tests\Fixtures
* @method \IdeHelper\Soyhuce\NextIdeHelper\Tests\Fixtures\__UserQuery whereNullableName(string|null $value)
* @method \IdeHelper\Soyhuce\NextIdeHelper\Tests\Fixtures\__UserQuery whereRememberToken(string|null $value)
* @method \IdeHelper\Soyhuce\NextIdeHelper\Tests\Fixtures\__UserQuery whereCreatedAt(\Illuminate\Support\Carbon|string $value)
* @method \IdeHelper\Soyhuce\NextIdeHelper\Tests\Fixtures\__UserQuery whereUpdatedAt(\Illuminate\Support\Carbon|string $value)
* @method \IdeHelper\Soyhuce\NextIdeHelper\Tests\Fixtures\__UserQuery whereUpdatedAt(\Carbon\CarbonImmutable|string $value)
* @method \IdeHelper\Soyhuce\NextIdeHelper\Tests\Fixtures\__UserQuery whereEmailDomain(string $domain, ?string $area = null)
* @method \Soyhuce\NextIdeHelper\Tests\Fixtures\User create(array $attributes = [])
* @method \Illuminate\Database\Eloquent\Collection<int, \Soyhuce\NextIdeHelper\Tests\Fixtures\User>|\Soyhuce\NextIdeHelper\Tests\Fixtures\User|null find($id, array $columns = ['*'])
Expand Down

0 comments on commit 2cfd001

Please sign in to comment.