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

feat: handle native enum casts #1494

Merged
merged 2 commits into from
Jan 15, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Properties/ModelCastHelper.php
Expand Up @@ -60,6 +60,10 @@ public function getReadableType(string $cast, Type $originalType): Type

$classReflection = $this->reflectionProvider->getClass($cast);

if ($classReflection->isEnum()) {
return new ObjectType($cast);
}

if ($classReflection->isSubclassOf(Castable::class)) {
$methodReflection = $classReflection->getNativeMethod('castUsing');
$castUsingReturn = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
Expand Down Expand Up @@ -111,6 +115,10 @@ public function getWriteableType(string $cast, Type $originalType): Type

$classReflection = $this->reflectionProvider->getClass($cast);

if ($classReflection->isEnum()) {
return new ObjectType($cast);
}

if ($classReflection->isSubclassOf(Castable::class)) {
$methodReflection = $classReflection->getNativeMethod('castUsing');
$castUsingReturn = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
Expand Down
7 changes: 7 additions & 0 deletions tests/Application/app/Casts/BackedEnumeration.php
@@ -0,0 +1,7 @@
<?php

namespace App\Casts;

enum BackedEnumeration : string
{
}
7 changes: 7 additions & 0 deletions tests/Application/app/Casts/BasicEnumeration.php
@@ -0,0 +1,7 @@
<?php

namespace App\Casts;

enum BasicEnumeration
{
}
4 changes: 4 additions & 0 deletions tests/Application/app/User.php
Expand Up @@ -2,6 +2,8 @@

namespace App;

use App\Casts\BackedEnumeration;
use App\Casts\BasicEnumeration;
use App\Casts\Favorites;
use App\Casts\Hash;
use function get_class;
Expand Down Expand Up @@ -54,6 +56,8 @@ class User extends Authenticatable
'properties' => AsCollection::class,
'favorites' => Favorites::class,
'secret' => Hash::class.':sha256',
'basic_enum' => BasicEnumeration::class,
'backed_enum' => BackedEnumeration::class,

'int' => 'int',
'integer' => 'integer',
Expand Down
Expand Up @@ -32,6 +32,8 @@ public function up(): void
$table->unknownColumnType('unknown_column');
$table->rememberToken();
$table->enum('enum_status', ['active', 'inactive']);
$table->string('basic_enum');
$table->string('backed_enum');

// Testing property casts
$table->integer('int');
Expand Down
4 changes: 4 additions & 0 deletions tests/Type/data/model-properties.php
Expand Up @@ -4,6 +4,8 @@

use App\Account;
use App\Address;
use App\Casts\BackedEnumeration;
use App\Casts\BasicEnumeration;
use App\Group;
use App\GuardedModel;
use App\Role;
Expand Down Expand Up @@ -43,6 +45,8 @@ function foo(User $user, Account $account, Role $role, Group $group, Team $team,
assertType(CarbonImmutable::class, $user->immutable_datetime);
assertType('int', $user->timestamp);
assertType('\'active\'|\'inactive\'', $user->enum_status);
assertType(BasicEnumeration::class, $user->basic_enum);
assertType(BackedEnumeration::class, $user->backed_enum);

// CastsAttributes
assertType('App\ValueObjects\Favorites', $user->favorites);
Expand Down