Skip to content

Commit 3ea00fe

Browse files
authored
feat(validated-dto): support Cast attribute for DTOCast type (#958)
This change improves the handling of Cast attributes in SimpleDTO by treating DTOCast similarly to EnumCast - both now pass the class name directly as a parameter instead of instantiating it. Changes: - Updated SimpleDTO.php to handle DTOCast in the match expression alongside EnumCast - Modified CallableCastingDTOInstance test to use Cast attribute instead of manual casts() method - Ensures consistency in how different cast types are handled This refactor simplifies the code and allows Cast attributes to work correctly with DTOCast type. Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com>
1 parent 4b7eb8e commit 3ea00fe

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

src/SimpleDTO.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use FriendsOfHyperf\ValidatedDTO\Attributes\Rules;
2121
use FriendsOfHyperf\ValidatedDTO\Casting\ArrayCast;
2222
use FriendsOfHyperf\ValidatedDTO\Casting\Castable;
23+
use FriendsOfHyperf\ValidatedDTO\Casting\DTOCast;
2324
use FriendsOfHyperf\ValidatedDTO\Casting\EnumCast;
2425
use FriendsOfHyperf\ValidatedDTO\Concerns\DataResolver;
2526
use FriendsOfHyperf\ValidatedDTO\Concerns\DataTransformer;
@@ -324,9 +325,10 @@ protected function buildCasts(): array
324325
continue;
325326
}
326327

327-
$param = $cast->type === EnumCast::class
328-
? $cast->param
329-
: new $cast->param();
328+
$param = match (true) {
329+
in_array($cast->type, [EnumCast::class, DTOCast::class]) => $cast->param,
330+
default => new $cast->param(),
331+
};
330332

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

0 commit comments

Comments
 (0)