diff --git a/CHANGELOG.md b/CHANGELOG.md index f2f1ad9..957ef79 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,9 @@ All notable changes to this project will be documented in this file. - Fix issue where \Eloquent is not included when using write_mixin [#1352 / Jefemy](https://github.com/barryvdh/laravel-ide-helper/pull/1352) - Fix model factory method arguments for Laravel >= 9 [#1361 / wimski](https://github.com/barryvdh/laravel-ide-helper/pull/1361) +### Added +- Add support for custom casts that implement `CastsInboundAttributes` [#1329 / sforward](https://github.com/barryvdh/laravel-ide-helper/pull/1329) + 2022-03-06, 2.12.3 ------------------ diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index d40b645..9d78583 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -22,6 +22,7 @@ use Doctrine\DBAL\Types\Type; 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\Attribute; use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Database\Eloquent\Model; @@ -429,6 +430,9 @@ class ModelsCommand extends Command if (!isset($this->properties[$name])) { continue; } + if ($this->isInboundCast($realType)) { + continue; + } $realType = $this->checkForCastableCasts($realType, $params); $realType = $this->checkForCustomLaravelCasts($realType); @@ -1295,6 +1299,11 @@ class ModelsCommand extends Command return $keyword; } + protected function isInboundCast(string $type): bool + { + return class_exists($type) && is_subclass_of($type, CastsInboundAttributes::class); + } + protected function checkForCastableCasts(string $type, array $params = []): string { if (!class_exists($type) || !interface_exists(Castable::class)) { diff --git a/tests/Console/ModelsCommand/LaravelCustomCasts/Casts/InboundAttributeCaster.php b/tests/Console/ModelsCommand/LaravelCustomCasts/Casts/InboundAttributeCaster.php new file mode 100644 index 0000000..cdbbf86 --- /dev/null +++ b/tests/Console/ModelsCommand/LaravelCustomCasts/Casts/InboundAttributeCaster.php @@ -0,0 +1,15 @@ + ExtendedSelfCastingCasterWithThisDocblockReturn::class, 'casted_property_with_static_return_docblock_and_param' => SelfCastingCasterWithStaticDocblockReturn::class . ':param', 'cast_without_property' => CustomCasterWithReturnType::class, + 'cast_inbound_attribute' => InboundAttributeCaster::class, ]; } diff --git a/tests/Console/ModelsCommand/LaravelCustomCasts/__snapshots__/Test__test__1.php b/tests/Console/ModelsCommand/LaravelCustomCasts/__snapshots__/Test__test__1.php index fd28b72..0438577 100644 --- a/tests/Console/ModelsCommand/LaravelCustomCasts/__snapshots__/Test__test__1.php +++ b/tests/Console/ModelsCommand/LaravelCustomCasts/__snapshots__/Test__test__1.php @@ -17,6 +17,7 @@ use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Cas use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\CustomCasterWithReturnType; use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\ExtendedSelfCastingCasterWithStaticDocblockReturn; use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\ExtendedSelfCastingCasterWithThisDocblockReturn; +use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\InboundAttributeCaster; use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\SelfCastingCasterWithStaticDocblockReturn; use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\SelfCastingCasterWithThisDocblockReturn; use Illuminate\Database\Eloquent\Model; @@ -41,6 +42,7 @@ use Illuminate\Database\Eloquent\Model; * @property \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\CastedProperty $casted_property_with_anonymous_cast * @property mixed $casted_property_without_return_type * @property \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\CastedProperty $cast_without_property + * @property mixed $cast_inbound_attribute * @method static \Illuminate\Database\Eloquent\Builder|CustomCast newModelQuery() * @method static \Illuminate\Database\Eloquent\Builder|CustomCast newQuery() * @method static \Illuminate\Database\Eloquent\Builder|CustomCast query() @@ -79,5 +81,6 @@ class CustomCast extends Model 'extended_casted_property_with_this_return_docblock' => ExtendedSelfCastingCasterWithThisDocblockReturn::class, 'casted_property_with_static_return_docblock_and_param' => SelfCastingCasterWithStaticDocblockReturn::class . ':param', 'cast_without_property' => CustomCasterWithReturnType::class, + 'cast_inbound_attribute' => InboundAttributeCaster::class, ]; }