diff --git a/CHANGELOG.md b/CHANGELOG.md index f8a2643d0..f473a14fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,8 @@ Add support for multiple pivot types when using the same accessor. ### Added +- Add support for AsCollection::using and AsEnumCollection::of casts [#1577 / uno-sw](https://github.com/barryvdh/laravel-ide-helper/pull/1577) + 2024-07-12, 3.1.0 ------------------ diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index cab439243..b915e03cc 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -410,8 +410,6 @@ public function castPropertiesType($model) case 'immutable_datetime': $realType = '\Carbon\CarbonImmutable'; break; - case AsCollection::class: - case AsEnumCollection::class: case 'collection': $realType = '\Illuminate\Support\Collection'; break; @@ -437,6 +435,18 @@ public function castPropertiesType($model) continue; } + if (Str::startsWith($type, AsCollection::class)) { + $realType = $this->getTypeInModel($model, $params[0] ?? null) ?? '\Illuminate\Support\Collection'; + } + + if (Str::startsWith($type, AsEnumCollection::class)) { + $realType = '\Illuminate\Support\Collection'; + $relatedModel = $this->getTypeInModel($model, $params[0] ?? null); + if ($relatedModel) { + $realType = $this->getCollectionTypeHint($realType, $relatedModel); + } + } + $realType = $this->checkForCastableCasts($realType, $params); $realType = $this->checkForCustomLaravelCasts($realType); $realType = $this->getTypeOverride($realType); diff --git a/tests/Console/ModelsCommand/AdvancedCasts/Collections/AdvancedCastCollection.php b/tests/Console/ModelsCommand/AdvancedCasts/Collections/AdvancedCastCollection.php new file mode 100644 index 000000000..0ce995ed5 --- /dev/null +++ b/tests/Console/ModelsCommand/AdvancedCasts/Collections/AdvancedCastCollection.php @@ -0,0 +1,11 @@ + 'date:Y-m-d', - 'cast_to_datetime_serialization' => 'datetime:Y-m-d H:i:s', - 'cast_to_custom_datetime' => 'custom_datetime:Y-m-d H:i:s', - 'cast_to_immutable_date' => 'immutable_date', - 'cast_to_immutable_custom_datetime' => 'immutable_custom_datetime:Y-m-d H:i:s', - 'cast_to_immutable_datetime' => 'immutable_datetime', - 'cast_to_timestamp' => 'timestamp', - 'cast_to_encrypted' => 'encrypted', - 'cast_to_encrypted_array' => 'encrypted:array', - 'cast_to_encrypted_collection' => 'encrypted:collection', - 'cast_to_encrypted_json' => 'encrypted:json', - 'cast_to_encrypted_object' => 'encrypted:object', - 'cast_to_as_collection' => AsCollection::class, - 'cast_to_as_enum_collection' => AsEnumCollection::class, - 'cast_to_as_array_object' => AsArrayObject::class, - ]; + protected function casts(): array + { + return [ + 'cast_to_date_serialization' => 'date:Y-m-d', + 'cast_to_datetime_serialization' => 'datetime:Y-m-d H:i:s', + 'cast_to_custom_datetime' => 'custom_datetime:Y-m-d H:i:s', + 'cast_to_immutable_date' => 'immutable_date', + 'cast_to_immutable_custom_datetime' => 'immutable_custom_datetime:Y-m-d H:i:s', + 'cast_to_immutable_datetime' => 'immutable_datetime', + 'cast_to_timestamp' => 'timestamp', + 'cast_to_encrypted' => 'encrypted', + 'cast_to_encrypted_array' => 'encrypted:array', + 'cast_to_encrypted_collection' => 'encrypted:collection', + 'cast_to_encrypted_json' => 'encrypted:json', + 'cast_to_encrypted_object' => 'encrypted:object', + 'cast_to_as_collection' => AsCollection::class, + 'cast_to_as_collection_using' => AsCollection::using(AdvancedCastCollection::class), + 'cast_to_as_enum_collection' => AsEnumCollection::class, + 'cast_to_as_enum_collection_of' => AsEnumCollection::of(AdvancedCastEnum::class), + 'cast_to_as_array_object' => AsArrayObject::class, + ]; + } } diff --git a/tests/Console/ModelsCommand/AdvancedCasts/__snapshots__/Test__test__1.php b/tests/Console/ModelsCommand/AdvancedCasts/__snapshots__/Test__test__1.php index db6d4a55a..2281cda88 100644 --- a/tests/Console/ModelsCommand/AdvancedCasts/__snapshots__/Test__test__1.php +++ b/tests/Console/ModelsCommand/AdvancedCasts/__snapshots__/Test__test__1.php @@ -4,6 +4,8 @@ namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\AdvancedCasts\Models; +use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\AdvancedCasts\Collections\AdvancedCastCollection; +use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\AdvancedCasts\Enums\AdvancedCastEnum; use Illuminate\Database\Eloquent\Casts\AsArrayObject; use Illuminate\Database\Eloquent\Casts\AsCollection; use Illuminate\Database\Eloquent\Casts\AsEnumCollection; @@ -27,6 +29,8 @@ * @property \Illuminate\Support\Collection $cast_to_as_collection * @property \Illuminate\Support\Collection $cast_to_as_enum_collection * @property \ArrayObject $cast_to_as_array_object + * @property AdvancedCastCollection $cast_to_as_collection_using + * @property \Illuminate\Support\Collection $cast_to_as_enum_collection_of * @method static \Illuminate\Database\Eloquent\Builder|AdvancedCast newModelQuery() * @method static \Illuminate\Database\Eloquent\Builder|AdvancedCast newQuery() * @method static \Illuminate\Database\Eloquent\Builder|AdvancedCast query() @@ -49,21 +53,26 @@ */ class AdvancedCast extends Model { - protected $casts = [ - 'cast_to_date_serialization' => 'date:Y-m-d', - 'cast_to_datetime_serialization' => 'datetime:Y-m-d H:i:s', - 'cast_to_custom_datetime' => 'custom_datetime:Y-m-d H:i:s', - 'cast_to_immutable_date' => 'immutable_date', - 'cast_to_immutable_custom_datetime' => 'immutable_custom_datetime:Y-m-d H:i:s', - 'cast_to_immutable_datetime' => 'immutable_datetime', - 'cast_to_timestamp' => 'timestamp', - 'cast_to_encrypted' => 'encrypted', - 'cast_to_encrypted_array' => 'encrypted:array', - 'cast_to_encrypted_collection' => 'encrypted:collection', - 'cast_to_encrypted_json' => 'encrypted:json', - 'cast_to_encrypted_object' => 'encrypted:object', - 'cast_to_as_collection' => AsCollection::class, - 'cast_to_as_enum_collection' => AsEnumCollection::class, - 'cast_to_as_array_object' => AsArrayObject::class, - ]; + protected function casts(): array + { + return [ + 'cast_to_date_serialization' => 'date:Y-m-d', + 'cast_to_datetime_serialization' => 'datetime:Y-m-d H:i:s', + 'cast_to_custom_datetime' => 'custom_datetime:Y-m-d H:i:s', + 'cast_to_immutable_date' => 'immutable_date', + 'cast_to_immutable_custom_datetime' => 'immutable_custom_datetime:Y-m-d H:i:s', + 'cast_to_immutable_datetime' => 'immutable_datetime', + 'cast_to_timestamp' => 'timestamp', + 'cast_to_encrypted' => 'encrypted', + 'cast_to_encrypted_array' => 'encrypted:array', + 'cast_to_encrypted_collection' => 'encrypted:collection', + 'cast_to_encrypted_json' => 'encrypted:json', + 'cast_to_encrypted_object' => 'encrypted:object', + 'cast_to_as_collection' => AsCollection::class, + 'cast_to_as_collection_using' => AsCollection::using(AdvancedCastCollection::class), + 'cast_to_as_enum_collection' => AsEnumCollection::class, + 'cast_to_as_enum_collection_of' => AsEnumCollection::of(AdvancedCastEnum::class), + 'cast_to_as_array_object' => AsArrayObject::class, + ]; + } }