Skip to content

Commit dda200f

Browse files
sforwardsforward
sforward
andauthored
Add support for CastsInboundAttributes (#1329)
* Add support for CastsInboundAttributes * Update changelog Co-authored-by: Simon Verwaard <[email protected]>
1 parent e8ecb3d commit dda200f

File tree

5 files changed

+32
-0
lines changed

5 files changed

+32
-0
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ All notable changes to this project will be documented in this file.
1111
- Fix issue where \Eloquent is not included when using write_mixin [#1352 / Jefemy](https://github.com/barryvdh/laravel-ide-helper/pull/1352)
1212
- Fix model factory method arguments for Laravel >= 9 [#1361 / wimski](https://github.com/barryvdh/laravel-ide-helper/pull/1361)
1313

14+
### Added
15+
- Add support for custom casts that implement `CastsInboundAttributes` [#1329 / sforward](https://github.com/barryvdh/laravel-ide-helper/pull/1329)
16+
1417
2022-03-06, 2.12.3
1518
------------------
1619

src/Console/ModelsCommand.php

+9
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Illuminate\Console\Command;
2323
use Illuminate\Contracts\Database\Eloquent\Castable;
2424
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
25+
use Illuminate\Contracts\Database\Eloquent\CastsInboundAttributes;
2526
use Illuminate\Database\Eloquent\Casts\Attribute;
2627
use Illuminate\Database\Eloquent\Factories\Factory;
2728
use Illuminate\Database\Eloquent\Model;
@@ -429,6 +430,9 @@ public function castPropertiesType($model)
429430
if (!isset($this->properties[$name])) {
430431
continue;
431432
}
433+
if ($this->isInboundCast($realType)) {
434+
continue;
435+
}
432436

433437
$realType = $this->checkForCastableCasts($realType, $params);
434438
$realType = $this->checkForCustomLaravelCasts($realType);
@@ -1295,6 +1299,11 @@ protected function getClassKeyword(ReflectionClass $reflection)
12951299
return $keyword;
12961300
}
12971301

1302+
protected function isInboundCast(string $type): bool
1303+
{
1304+
return class_exists($type) && is_subclass_of($type, CastsInboundAttributes::class);
1305+
}
1306+
12981307
protected function checkForCastableCasts(string $type, array $params = []): string
12991308
{
13001309
if (!class_exists($type) || !interface_exists(Castable::class)) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts;
6+
7+
use Illuminate\Contracts\Database\Eloquent\CastsInboundAttributes;
8+
9+
class InboundAttributeCaster implements CastsInboundAttributes
10+
{
11+
public function set($model, string $key, $value, array $attributes)
12+
{
13+
// TODO: Implement set() method.
14+
}
15+
}

tests/Console/ModelsCommand/LaravelCustomCasts/Models/CustomCast.php

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\CustomCasterWithReturnType;
1818
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\ExtendedSelfCastingCasterWithStaticDocblockReturn;
1919
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\ExtendedSelfCastingCasterWithThisDocblockReturn;
20+
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\InboundAttributeCaster;
2021
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\SelfCastingCasterWithStaticDocblockReturn;
2122
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\SelfCastingCasterWithThisDocblockReturn;
2223
use Illuminate\Database\Eloquent\Model;
@@ -41,5 +42,6 @@ class CustomCast extends Model
4142
'extended_casted_property_with_this_return_docblock' => ExtendedSelfCastingCasterWithThisDocblockReturn::class,
4243
'casted_property_with_static_return_docblock_and_param' => SelfCastingCasterWithStaticDocblockReturn::class . ':param',
4344
'cast_without_property' => CustomCasterWithReturnType::class,
45+
'cast_inbound_attribute' => InboundAttributeCaster::class,
4446
];
4547
}

tests/Console/ModelsCommand/LaravelCustomCasts/__snapshots__/Test__test__1.php

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\CustomCasterWithReturnType;
1818
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\ExtendedSelfCastingCasterWithStaticDocblockReturn;
1919
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\ExtendedSelfCastingCasterWithThisDocblockReturn;
20+
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\InboundAttributeCaster;
2021
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\SelfCastingCasterWithStaticDocblockReturn;
2122
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\SelfCastingCasterWithThisDocblockReturn;
2223
use Illuminate\Database\Eloquent\Model;
@@ -41,6 +42,7 @@
4142
* @property \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\CastedProperty $casted_property_with_anonymous_cast
4243
* @property mixed $casted_property_without_return_type
4344
* @property \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\CastedProperty $cast_without_property
45+
* @property mixed $cast_inbound_attribute
4446
* @method static \Illuminate\Database\Eloquent\Builder|CustomCast newModelQuery()
4547
* @method static \Illuminate\Database\Eloquent\Builder|CustomCast newQuery()
4648
* @method static \Illuminate\Database\Eloquent\Builder|CustomCast query()
@@ -79,5 +81,6 @@ class CustomCast extends Model
7981
'extended_casted_property_with_this_return_docblock' => ExtendedSelfCastingCasterWithThisDocblockReturn::class,
8082
'casted_property_with_static_return_docblock_and_param' => SelfCastingCasterWithStaticDocblockReturn::class . ':param',
8183
'cast_without_property' => CustomCasterWithReturnType::class,
84+
'cast_inbound_attribute' => InboundAttributeCaster::class,
8285
];
8386
}

0 commit comments

Comments
 (0)