Skip to content

Commit 9bdad1b

Browse files
committed
Fix issue when using DTOCast with Cast attribute
1 parent 3d3fa93 commit 9bdad1b

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

src/SimpleDTO.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use WendellAdriel\ValidatedDTO\Attributes\Rules;
2424
use WendellAdriel\ValidatedDTO\Casting\ArrayCast;
2525
use WendellAdriel\ValidatedDTO\Casting\Castable;
26+
use WendellAdriel\ValidatedDTO\Casting\DTOCast;
2627
use WendellAdriel\ValidatedDTO\Casting\EnumCast;
2728
use WendellAdriel\ValidatedDTO\Concerns\DataResolver;
2829
use WendellAdriel\ValidatedDTO\Concerns\DataTransformer;
@@ -311,9 +312,10 @@ protected function buildCasts(): array
311312
continue;
312313
}
313314

314-
$param = $cast->type === EnumCast::class
315-
? $cast->param
316-
: new $cast->param();
315+
$param = match (true) {
316+
in_array($cast->type, [EnumCast::class, DTOCast::class]) => $cast->param,
317+
default => new $cast->param(),
318+
};
317319

318320
$casts[$property] = new $cast->type($param);
319321
}

tests/Datasets/CallableCastingDTOInstance.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44

55
namespace WendellAdriel\ValidatedDTO\Tests\Datasets;
66

7+
use WendellAdriel\ValidatedDTO\Attributes\Cast;
78
use WendellAdriel\ValidatedDTO\Casting\DTOCast;
89
use WendellAdriel\ValidatedDTO\Exceptions\CastException;
910
use WendellAdriel\ValidatedDTO\SimpleDTO;
1011

1112
class CallableCastingDTOInstance extends SimpleDTO
1213
{
14+
#[Cast(DTOCast::class, SimpleNameDTO::class)]
1315
public SimpleNameDTO $name;
1416

1517
public ?int $age = null;
@@ -22,7 +24,6 @@ protected function defaults(): array
2224
protected function casts(): array
2325
{
2426
return [
25-
'name' => new DTOCast(SimpleNameDTO::class),
2627
'age' => function (string $property, mixed $value) {
2728
if (! is_numeric($value)) {
2829
throw new CastException($property);

0 commit comments

Comments
 (0)