diff --git a/CHANGELOG.md b/CHANGELOG.md index ca3bb91..d2a2e54 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ All notable changes to this project will be documented in this file. - Add support for real-time facades in the helper file. [#1455 / filipac](https://github.com/barryvdh/laravel-ide-helper/pull/1455) - Add support for relations with composite keys. [#1479 / calebdw](https://github.com/barryvdh/laravel-ide-helper/pull/1479) - Add support for attribute accessors with no backing field or type hinting [#1411 / pindab0ter](https://github.com/barryvdh/laravel-ide-helper/pull/1411). +- Add support for AsCollection and AsArrayObject casts [#1393 / pataar](https://github.com/barryvdh/laravel-ide-helper/pull/1393) 2024-02-05, 2.14.0 ------------------ diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index 8f6242c..e99848b 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -23,6 +23,8 @@ use Illuminate\Console\Command; use Illuminate\Contracts\Database\Eloquent\Castable; use Illuminate\Contracts\Database\Eloquent\CastsAttributes; use Illuminate\Contracts\Database\Eloquent\CastsInboundAttributes; +use Illuminate\Database\Eloquent\Casts\AsArrayObject; +use Illuminate\Database\Eloquent\Casts\AsCollection; use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Database\Eloquent\Model; @@ -419,9 +421,13 @@ class ModelsCommand extends Command case 'immutable_datetime': $realType = '\Carbon\CarbonImmutable'; break; + case AsCollection::class: case 'collection': $realType = '\Illuminate\Support\Collection'; break; + case AsArrayObject::class: + $realType = '\ArrayObject'; + break; default: // In case of an optional custom cast parameter , only evaluate // the `$type` until the `:` diff --git a/tests/Console/ModelsCommand/AdvancedCasts/Models/AdvancedCast.php b/tests/Console/ModelsCommand/AdvancedCasts/Models/AdvancedCast.php new file mode 100644 index 0000000..4613056 --- /dev/null +++ b/tests/Console/ModelsCommand/AdvancedCasts/Models/AdvancedCast.php @@ -0,0 +1,29 @@ + '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_array_object' => AsArrayObject::class, + ]; +} diff --git a/tests/Console/ModelsCommand/AdvancedCasts/Test.php b/tests/Console/ModelsCommand/AdvancedCasts/Test.php new file mode 100644 index 0000000..7856703 --- /dev/null +++ b/tests/Console/ModelsCommand/AdvancedCasts/Test.php @@ -0,0 +1,31 @@ +=')) { + $this->markTestSkipped( + 'This test only works in Laravel >= 8.28' + ); + } + + $command = $this->app->make(ModelsCommand::class); + + $tester = $this->runCommand($command, [ + '--write' => true, + ]); + + $this->assertSame(0, $tester->getStatusCode()); + $this->assertStringContainsString('Written new phpDocBlock to', $tester->getDisplay()); + $this->assertMatchesMockedSnapshot(); + } +} diff --git a/tests/Console/ModelsCommand/AdvancedCasts/__snapshots__/Test__test__1.php b/tests/Console/ModelsCommand/AdvancedCasts/__snapshots__/Test__test__1.php new file mode 100644 index 0000000..12a9f0d --- /dev/null +++ b/tests/Console/ModelsCommand/AdvancedCasts/__snapshots__/Test__test__1.php @@ -0,0 +1,65 @@ + '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_array_object' => AsArrayObject::class, + ]; +} diff --git a/tests/Console/ModelsCommand/migrations/____advanced_casts_table.php b/tests/Console/ModelsCommand/migrations/____advanced_casts_table.php new file mode 100644 index 0000000..3c6a392 --- /dev/null +++ b/tests/Console/ModelsCommand/migrations/____advanced_casts_table.php @@ -0,0 +1,30 @@ +string('cast_to_date_serialization'); + $table->string('cast_to_datetime_serialization'); + $table->string('cast_to_custom_datetime'); + $table->string('cast_to_immutable_date'); + $table->string('cast_to_immutable_custom_datetime'); + $table->string('cast_to_immutable_datetime'); + $table->string('cast_to_timestamp'); + $table->string('cast_to_encrypted'); + $table->string('cast_to_encrypted_array'); + $table->string('cast_to_encrypted_collection'); + $table->string('cast_to_encrypted_json'); + $table->string('cast_to_encrypted_object'); + $table->string('cast_to_as_collection'); + $table->string('cast_to_as_array_object'); + }); + } +}