Skip to content
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
23 changes: 22 additions & 1 deletion src/Actions/WriteColumnAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,28 @@ public function __invoke(ReflectionClass $reflectionModel, array $attribute, arr
}
}
} else {
if ($attribute['type'] !== null) {
// No get callback: reads go through the model's cast,
// so fall back to it before the database column type
$cast = $reflectionModel->newInstance()->getCasts()[$attribute['name']] ?? null;

if (! is_null($cast)) {
if (Str::contains($cast, '\\')) {
$castReflection = new ReflectionClass($cast);

if ($castReflection->isEnum()) {
$type = $this->getClassName($cast);
$enumRef = $castReflection;
}
} else {
$cleanStr = Str::of($cast)->before(':')->lower()->toString();

if (isset($mappings[$cleanStr])) {
$type = $returnType($cleanStr, $mappings);
}
}
}

if ($type === 'unknown' && $attribute['type'] !== null) {
$type = $returnType($attribute['type'], $mappings);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/Actions/WriteEnumConst.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public function __invoke(ReflectionClass $reflection, string $indent = '', bool
$comments = array_map(fn ($match) => trim(str_replace('@property', '', $match)), $matches[0]);
}

$cases = collect($reflection->getConstants());
$cases = collect($reflection->getConstants())
->filter(fn ($case) => $case instanceof \BackedEnum);

if ($cases->isNotEmpty()) {
if ($useEnums) {
Expand Down
12 changes: 12 additions & 0 deletions test/input/expectations/complex-model-camel-case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface Complex {
string: string
castedUppercaseString: unknown
stringWithMutatorAndNoAccessor: string
enumWithMutatorAndNoAccessor: Roles
text: string
time: string
timestamp: string
Expand All @@ -42,3 +43,14 @@ export interface Complex {
// exists
complexRelationshipsExists: boolean
}

const Roles = {
/** Can do anything */
ADMIN: 'admin',
/** Standard readonly */
USER: 'user',
/** Value that needs string escaping */
USERCLASS: 'App\\Models\\User',
} as const;

export type Roles = typeof Roles[keyof typeof Roles]
12 changes: 12 additions & 0 deletions test/input/expectations/complex-model-pascal-case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface Complex {
String: string
CastedUppercaseString: unknown
StringWithMutatorAndNoAccessor: string
EnumWithMutatorAndNoAccessor: Roles
Text: string
Time: string
Timestamp: string
Expand All @@ -42,3 +43,14 @@ export interface Complex {
// exists
ComplexRelationshipsExists: boolean
}

const Roles = {
/** Can do anything */
ADMIN: 'admin',
/** Standard readonly */
USER: 'user',
/** Value that needs string escaping */
USERCLASS: 'App\\Models\\User',
} as const;

export type Roles = typeof Roles[keyof typeof Roles]
12 changes: 12 additions & 0 deletions test/input/expectations/complex-model-with-cast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface Complex {
string: string
casted_uppercase_string: string
string_with_mutator_and_no_accessor: string
enum_with_mutator_and_no_accessor: Roles
text: string
time: string
timestamp: string
Expand All @@ -42,3 +43,14 @@ export interface Complex {
// exists
complex_relationships_exists: boolean
}

const Roles = {
/** Can do anything */
ADMIN: 'admin',
/** Standard readonly */
USER: 'user',
/** Value that needs string escaping */
USERCLASS: 'App\\Models\\User',
} as const;

export type Roles = typeof Roles[keyof typeof Roles]
12 changes: 12 additions & 0 deletions test/input/expectations/complex-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface Complex {
string: string
casted_uppercase_string: unknown
string_with_mutator_and_no_accessor: string
enum_with_mutator_and_no_accessor: Roles
text: string
time: string
timestamp: string
Expand All @@ -42,3 +43,14 @@ export interface Complex {
// exists
complex_relationships_exists: boolean
}

const Roles = {
/** Can do anything */
ADMIN: 'admin',
/** Standard readonly */
USER: 'user',
/** Value that needs string escaping */
USERCLASS: 'App\\Models\\User',
} as const;

export type Roles = typeof Roles[keyof typeof Roles]
5 changes: 5 additions & 0 deletions test/laravel-skeleton/app/Enums/Roles.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ enum Roles: string
case USER = 'user';
case USERCLASS = User::class;

public const NON_ADMIN_ROLES = [
self::USER,
self::USERCLASS,
];

public static function fromValue(string $value): self
{
return match ($value) {
Expand Down
10 changes: 10 additions & 0 deletions test/laravel-skeleton/app/Models/Complex.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace App\Models;

use App\Casts\UpperCast;
use App\Enums\Roles;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

Expand All @@ -15,6 +17,7 @@ class Complex extends Model
'jsonb' => 'json',
'year' => 'int',
'casted_uppercase_string' => UpperCast::class,
'enum_with_mutator_and_no_accessor' => Roles::class,
'immutableDateTime' => 'immutable_date',
'immutableDate' => 'immutable_datetime',
'immutableCustomDateTime' => 'immutable_custom_datetime',
Expand All @@ -31,4 +34,11 @@ protected function stringWithMutatorAndNoAccessor(): Attribute
set: fn (string $value): string => strtolower($value),
);
}

protected function enumWithMutatorAndNoAccessor(): Attribute
{
return Attribute::make(
set: fn (Roles|string $value): string => $value instanceof Roles ? $value->value : $value,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function up(): void
$table->string('string');
$table->string('casted_uppercase_string');
$table->string('string_with_mutator_and_no_accessor');
$table->string('enum_with_mutator_and_no_accessor');
$table->text('text');
$table->time('time');
$table->timestamp('timestamp');
Expand Down
Loading