Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/SimpleDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use WendellAdriel\ValidatedDTO\Attributes\Rules;
use WendellAdriel\ValidatedDTO\Casting\ArrayCast;
use WendellAdriel\ValidatedDTO\Casting\Castable;
use WendellAdriel\ValidatedDTO\Casting\DTOCast;
use WendellAdriel\ValidatedDTO\Casting\EnumCast;
use WendellAdriel\ValidatedDTO\Concerns\DataResolver;
use WendellAdriel\ValidatedDTO\Concerns\DataTransformer;
Expand Down Expand Up @@ -311,9 +312,10 @@ protected function buildCasts(): array
continue;
}

$param = $cast->type === EnumCast::class
? $cast->param
: new $cast->param();
$param = match (true) {
in_array($cast->type, [EnumCast::class, DTOCast::class]) => $cast->param,
default => new $cast->param(),
};

$casts[$property] = new $cast->type($param);
}
Expand Down
3 changes: 2 additions & 1 deletion tests/Datasets/CallableCastingDTOInstance.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

namespace WendellAdriel\ValidatedDTO\Tests\Datasets;

use WendellAdriel\ValidatedDTO\Attributes\Cast;
use WendellAdriel\ValidatedDTO\Casting\DTOCast;
use WendellAdriel\ValidatedDTO\Exceptions\CastException;
use WendellAdriel\ValidatedDTO\SimpleDTO;

class CallableCastingDTOInstance extends SimpleDTO
{
#[Cast(DTOCast::class, SimpleNameDTO::class)]
public SimpleNameDTO $name;

public ?int $age = null;
Expand All @@ -22,7 +24,6 @@ protected function defaults(): array
protected function casts(): array
{
return [
'name' => new DTOCast(SimpleNameDTO::class),
'age' => function (string $property, mixed $value) {
if (! is_numeric($value)) {
throw new CastException($property);
Expand Down