Skip to content

Commit

Permalink
feat: handle native enum casts (#1494)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikgaal committed Jan 15, 2023
1 parent 6337254 commit 92cd4f5
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Properties/ModelCastHelper.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace App\Casts;

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

namespace App\Casts;

enum BasicEnumeration
{
}
4 changes: 4 additions & 0 deletions tests/Application/app/User.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 92cd4f5

Please sign in to comment.