From 51fb97b8fb6d31683ba8557c6975647a35910cef Mon Sep 17 00:00:00 2001 From: uno-sw Date: Fri, 2 Aug 2024 13:08:55 +0900 Subject: [PATCH 1/3] Add support for AsCollection::using and AsEnumCollection::of casts --- src/Console/ModelsCommand.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index 8bdd55af1..7548288db 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -409,8 +409,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; @@ -436,6 +434,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); From 7d2ea97f25a612f32efa6893c1c3b1309158bdda Mon Sep 17 00:00:00 2001 From: uno-sw Date: Fri, 2 Aug 2024 13:21:45 +0900 Subject: [PATCH 2/3] Update test --- .../Collections/AdvancedCastCollection.php | 11 +++++ .../AdvancedCasts/Enums/AdvancedCastEnum.php | 12 ++++++ .../AdvancedCasts/Models/AdvancedCast.php | 41 ++++++++++-------- .../__snapshots__/Test__test__1.php | 43 +++++++++++-------- 4 files changed, 73 insertions(+), 34 deletions(-) create mode 100644 tests/Console/ModelsCommand/AdvancedCasts/Collections/AdvancedCastCollection.php create mode 100644 tests/Console/ModelsCommand/AdvancedCasts/Enums/AdvancedCastEnum.php 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 9c9972abd..4613c7af5 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, + ]; + } } From 07719015b3c4a647d1343c2d8897a965fefc53fc Mon Sep 17 00:00:00 2001 From: uno-sw Date: Fri, 2 Aug 2024 13:28:07 +0900 Subject: [PATCH 3/3] Update CHANGELOG.md --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 532de8a11..127290ce3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ All notable changes to this project will be documented in this file. ### 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 ------------------